diff --git a/services/lib/src/GlobalTools.cpp b/services/lib/src/GlobalTools.cpp index 0a01a14c4..3efd2655b 100644 --- a/services/lib/src/GlobalTools.cpp +++ b/services/lib/src/GlobalTools.cpp @@ -1,62 +1,74 @@ #include "GlobalTools.h" #include #include #include #include #include #include #include #include #include namespace comm { namespace network { namespace tools { uint64_t getCurrentTimestamp() { using namespace std::chrono; return duration_cast(system_clock::now().time_since_epoch()) .count(); } bool hasEnvFlag(const std::string &flag) { if (std::getenv(flag.c_str()) == nullptr) { return false; } return std::string(std::getenv(flag.c_str())) == "1"; } std::string decorateTableName(const std::string &baseName) { std::string suffix = ""; if (hasEnvFlag("COMM_TEST_SERVICES")) { suffix = "-test"; } return baseName + suffix; } bool isSandbox() { return hasEnvFlag("COMM_SERVICES_SANDBOX"); } std::string generateUUID() { thread_local boost::uuids::random_generator random_generator; return boost::uuids::to_string(random_generator()); } bool validateUUIDv4(const std::string &uuid) { const std::regex uuidV4RegexFormat( "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"); try { return std::regex_match(uuid, uuidV4RegexFormat); } catch (const std::exception &e) { LOG(ERROR) << "Tools: " << "Got an exception at `validateUUID`: " << e.what(); return false; } } +void InitLogging(const std::string &programName) { + FLAGS_logtostderr = true; + FLAGS_colorlogtostderr = true; + if (comm::network::tools::isSandbox()) { + // Log levels INFO, WARNING, ERROR, FATAL are 0, 1, 2, 3, respectively + FLAGS_minloglevel = 0; + } else { + FLAGS_minloglevel = 1; + } + google::InitGoogleLogging(programName.c_str()); +} + } // namespace tools } // namespace network } // namespace comm diff --git a/services/lib/src/GlobalTools.h b/services/lib/src/GlobalTools.h index ddb4f7ac8..bd61df7a9 100644 --- a/services/lib/src/GlobalTools.h +++ b/services/lib/src/GlobalTools.h @@ -1,26 +1,28 @@ #pragma once #include #include namespace comm { namespace network { namespace tools { const std::string ID_SEPARATOR = ":"; uint64_t getCurrentTimestamp(); bool hasEnvFlag(const std::string &flag); std::string decorateTableName(const std::string &baseName); bool isSandbox(); std::string generateUUID(); bool validateUUIDv4(const std::string &uuid); +void InitLogging(const std::string &programName); + } // namespace tools } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/server.cpp b/services/tunnelbroker/src/server.cpp index 984b5d52e..8802f16dc 100644 --- a/services/tunnelbroker/src/server.cpp +++ b/services/tunnelbroker/src/server.cpp @@ -1,67 +1,55 @@ #include "AmqpManager.h" #include "ConfigManager.h" #include "Constants.h" #include "GlobalTools.h" #include "TunnelbrokerServiceImpl.h" #include #include #include #include #include namespace comm { namespace network { void RunServer() { TunnelBrokerServiceImpl service; grpc::EnableDefaultHealthCheckService(true); grpc::ServerBuilder builder; // Listen on the given address without any authentication mechanism. builder.AddListeningPort( SERVER_LISTEN_ADDRESS, grpc::InsecureServerCredentials()); // Register "service" as the instance through which we'll communicate with // clients. In this case it corresponds to an *synchronous* service. builder.RegisterService(&service); std::unique_ptr server(builder.BuildAndStart()); LOG(INFO) << "gRPC Server listening at :" << SERVER_LISTEN_ADDRESS; // Wait for the server to shutdown. Note that some other thread must be // responsible for shutting down the server for this call to ever return. server->Wait(); } void RunAmqpClient() { AmqpManager::getInstance().connect(); } -void InitLogging(const char *programName) { - FLAGS_logtostderr = true; - FLAGS_colorlogtostderr = true; - if (comm::network::tools::isSandbox()) { - // Log levels INFO, WARNING, ERROR, FATAL are 0, 1, 2, 3, respectively - FLAGS_minloglevel = 0; - } else { - FLAGS_minloglevel = 1; - } - google::InitGoogleLogging(programName); -} - } // namespace network } // namespace comm int main(int argc, char **argv) { - comm::network::InitLogging(argv[0]); + comm::network::tools::InitLogging("tunnelbroker"); if (comm::network::tools::isSandbox()) { comm::network::config::ConfigManager::getInstance().load( comm::network::DEV_CONFIG_FILE_PATH); } else { comm::network::config::ConfigManager::getInstance().load( comm::network::CONFIG_FILE_PATH); } std::thread amqpThread(comm::network::RunAmqpClient); std::thread grpcThread(comm::network::RunServer); amqpThread.join(); grpcThread.join(); return 0; }