// Hello World server #include #include #include #include #include #include #include #include #include #include #define DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__) #define LOG(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__) #define WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__) #define ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__) std::string getCurrentTimeFormatted() { // 获取当前时间点 std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); // 转换为时间戳(秒) auto timestamp = std::chrono::system_clock::to_time_t(now); // 创建时间戳字符串(不包含毫秒) std::stringstream ss; char buf[100] = {0}; if (std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", std::localtime(×tamp))) { ss << buf; } // 添加毫秒部分 auto duration = now.time_since_epoch(); auto millis = std::chrono::duration_cast(duration).count() % 1000; ss << "." << std::setfill('0') << std::setw(3) << millis; return ss.str(); } using namespace std; int main(int argc, char *argv[]) { if (argc != 2) { cout << "Usage: " << argv[0] << " " << endl; cout << "Example: " << argv[0] << " localhost:44242 " << endl; return 1; } auto file_logger = spdlog::rotating_logger_mt("datahubs_logger", "/data01/logs/datahubs.log", 1024 * 1024 * 10, 10); file_logger->set_level(spdlog::level::info); file_logger->flush_on(spdlog::level::info); std::string ip = argv[1]; const string endpoint = "tcp://" + ip; // initialize the 0MQ context zmqpp::context context; // generate a pull socket zmqpp::socket_type type = zmqpp::socket_type::reply; zmqpp::socket socket(context, type); // bind to the socket socket.bind(endpoint); while (1) { // receive the message zmqpp::message message; // decompose the message socket.receive(message); string text; message >> text; // Do some 'work' std::this_thread::sleep_for(std::chrono::milliseconds(500)); file_logger->info("Received {} at {}", text, getCurrentTimeFormatted()); //cout << "Received " << text << " at " << getCurrentTimeFormatted() << endl; std::string response = "Hello, " + text + "!"; // send the response zmqpp::message response_message(response); socket.send(response_message); } }