diff --git a/cpp/src/config/ConfigNode.cpp b/cpp/src/config/ConfigNode.cpp index d67630db95..752d2f9545 100644 --- a/cpp/src/config/ConfigNode.cpp +++ b/cpp/src/config/ConfigNode.cpp @@ -5,7 +5,7 @@ ******************************************************************************/ #include "ConfigNode.h" #include "utils/Error.h" -#include "utils/CommonUtil.h" +#include "utils/Log.h" #include #include @@ -177,18 +177,18 @@ void ConfigNode::ClearSequences() { void ConfigNode::PrintAll(const std::string& prefix) const { for(auto& elem : config_) { - CommonUtil::PrintInfo(prefix + elem.first + ": " + elem.second); + SERVER_LOG_INFO << prefix << elem.first + ": " << elem.second; } for(auto& elem : sequences_) { - CommonUtil::PrintInfo(prefix + elem.first + ": "); + SERVER_LOG_INFO << prefix << elem.first << ": "; for(auto& str : elem.second) { - CommonUtil::PrintInfo(prefix + " - " + str); + SERVER_LOG_INFO << prefix << " - " << str; } } for(auto& elem : children_) { - CommonUtil::PrintInfo(prefix + elem.first + ": "); + SERVER_LOG_INFO << prefix << elem.first << ": "; elem.second.PrintAll(prefix + " "); } } diff --git a/cpp/src/config/YamlConfigMgr.cpp b/cpp/src/config/YamlConfigMgr.cpp index afcd0c74fa..276db4ee9e 100644 --- a/cpp/src/config/YamlConfigMgr.cpp +++ b/cpp/src/config/YamlConfigMgr.cpp @@ -4,7 +4,7 @@ * Proprietary and confidential. ******************************************************************************/ #include "YamlConfigMgr.h" -#include "utils/CommonUtil.h" +#include "utils/Log.h" #include @@ -16,7 +16,7 @@ ServerError YamlConfigMgr::LoadConfigFile(const std::string &filename) { struct stat directoryStat; int statOK = stat(filename.c_str(), &directoryStat); if (statOK != 0) { - CommonUtil::PrintError("File not found: " + filename); + SERVER_LOG_ERROR << "File not found: " << filename; return SERVER_UNEXPECTED_ERROR; } @@ -25,7 +25,7 @@ ServerError YamlConfigMgr::LoadConfigFile(const std::string &filename) { LoadConfigNode(node_, config_); } catch (YAML::Exception& e) { - CommonUtil::PrintError("Failed to load config file: " + std::string(e.what ())); + SERVER_LOG_ERROR << "Failed to load config file: " << std::string(e.what ()); return SERVER_UNEXPECTED_ERROR; } @@ -33,7 +33,7 @@ ServerError YamlConfigMgr::LoadConfigFile(const std::string &filename) { } void YamlConfigMgr::Print() const { - CommonUtil::PrintInfo("System config content:"); + SERVER_LOG_INFO << "System config content:"; config_.PrintAll(); } diff --git a/cpp/src/server/Server.cpp b/cpp/src/server/Server.cpp index 1536c7bf56..2dca04ad72 100644 --- a/cpp/src/server/Server.cpp +++ b/cpp/src/server/Server.cpp @@ -6,7 +6,7 @@ #include "Server.h" #include "ServerConfig.h" #include "ServiceWrapper.h" -#include "utils/CommonUtil.h" +#include "utils/Log.h" #include "utils/SignalUtil.h" #include "utils/TimeRecorder.h" #include "utils/LogUtil.h" @@ -50,11 +50,11 @@ Server::Daemonize() { return; } - CommonUtil::PrintInfo("Vecwise server run in daemonize mode"); + SERVER_LOG_INFO << "Vecwise server run in daemonize mode"; // std::string log_path(GetLogDirFullPath()); // log_path += "zdb_server.(INFO/WARNNING/ERROR/CRITICAL)"; -// CommonUtil::PrintInfo("Log will be exported to: " + log_path); +// SERVER_LOG_INFO << "Log will be exported to: " + log_path); pid_t pid = 0; @@ -106,7 +106,7 @@ Server::Daemonize() { close(fd); } - CommonUtil::PrintInfo("Redirect stdin/stdout/stderr to /dev/null"); + SERVER_LOG_INFO << "Redirect stdin/stdout/stderr to /dev/null"; // Redirect stdin/stdout/stderr to /dev/null stdin = fopen("/dev/null", "r"); @@ -116,11 +116,11 @@ Server::Daemonize() { if (!pid_filename_.empty()) { pid_fd = open(pid_filename_.c_str(), O_RDWR | O_CREAT, 0640); if (pid_fd < 0) { - CommonUtil::PrintInfo("Can't open filename: " + pid_filename_ + ", Error: " + strerror(errno)); + SERVER_LOG_INFO << "Can't open filename: " + pid_filename_ + ", Error: " + strerror(errno); exit(EXIT_FAILURE); } if (lockf(pid_fd, F_TLOCK, 0) < 0) { - CommonUtil::PrintInfo("Can't lock filename: " + pid_filename_ + ", Error: " + strerror(errno)); + SERVER_LOG_INFO << "Can't lock filename: " + pid_filename_ + ", Error: " + strerror(errno); exit(EXIT_FAILURE); } @@ -159,14 +159,12 @@ Server::Start() { signal(SIGHUP, SignalUtil::HandleSignal); signal(SIGTERM, SignalUtil::HandleSignal); - CommonUtil::PrintInfo("Vecwise server is running..."); + SERVER_LOG_INFO << "Vecwise server is running..."; StartService(); } catch(std::exception& ex){ - std::string info = "Vecwise server encounter exception: " + std::string(ex.what()); - CommonUtil::PrintError(info); - - CommonUtil::PrintInfo("Is another server instance running?"); + SERVER_LOG_ERROR << "Vecwise server encounter exception: " << std::string(ex.what()) + << "Is another server instance running?"; break; } } while(false); @@ -177,7 +175,7 @@ Server::Start() { void Server::Stop() { - CommonUtil::PrintInfo("Vecwise server will be closed"); + SERVER_LOG_INFO << "Vecwise server will be closed"; // Unlock and close lockfile if (pid_fd != -1) { @@ -204,7 +202,7 @@ Server::Stop() { StopService(); - CommonUtil::PrintInfo("Vecwise server closed"); + SERVER_LOG_INFO << "Vecwise server closed"; } diff --git a/cpp/src/server/ServiceWrapper.cpp b/cpp/src/server/ServiceWrapper.cpp index 9bf047dabe..d28e09b1d0 100644 --- a/cpp/src/server/ServiceWrapper.cpp +++ b/cpp/src/server/ServiceWrapper.cpp @@ -6,7 +6,7 @@ #include "ServiceWrapper.h" #include "ServerConfig.h" -#include "utils/CommonUtil.h" +#include "utils/Log.h" #include "thrift/gen-cpp/VecService.h" #include "thrift/gen-cpp/VectorService_types.h" @@ -40,7 +40,7 @@ public: void dummy() { // Your implementation goes here - printf("dummy\n"); + printf("dummy() called\n"); } /** @@ -127,7 +127,7 @@ void ServiceWrapper::StartService() { } else if(protocol == "json") { protocolFactory.reset(new TJSONProtocolFactory()); } else { - CommonUtil::PrintError("Service protocol: " + protocol + " is not supported currently"); + SERVER_LOG_INFO << "Service protocol: " << protocol << " is not supported currently"; return; } @@ -143,7 +143,7 @@ void ServiceWrapper::StartService() { s_server.reset(new TThreadPoolServer(processor, serverTransport, transportFactory, protocolFactory, threadManager)); s_server->serve(); } else { - CommonUtil::PrintError("Service mode: " + mode + " is not supported currently"); + SERVER_LOG_INFO << "Service mode: " << mode << " is not supported currently"; return; } } diff --git a/cpp/src/utils/CommonUtil.cpp b/cpp/src/utils/CommonUtil.cpp index 985a7ce761..d8bb4dc3d3 100644 --- a/cpp/src/utils/CommonUtil.cpp +++ b/cpp/src/utils/CommonUtil.cpp @@ -31,16 +31,6 @@ namespace server { namespace fs = boost::filesystem; -void CommonUtil::PrintInfo(const std::string& info){ - SERVER_LOG_INFO << info; - std::cout << "Info:" << info << std::endl; -} - -void CommonUtil::PrintError(const std::string& info){ - SERVER_LOG_ERROR << info; - std::cout << "Error:" << info << std::endl; -} - bool CommonUtil::GetSystemMemInfo(unsigned long &totalMem, unsigned long &freeMem) { struct sysinfo info; int ret = sysinfo(&info); diff --git a/cpp/src/utils/CommonUtil.h b/cpp/src/utils/CommonUtil.h index 9ac96fd333..798ad62817 100755 --- a/cpp/src/utils/CommonUtil.h +++ b/cpp/src/utils/CommonUtil.h @@ -15,9 +15,6 @@ namespace server { class CommonUtil { public: - static void PrintInfo(const std::string& info); - static void PrintError(const std::string& info); - static bool GetSystemMemInfo(unsigned long &totalMem, unsigned long &freeMem); static bool GetSystemAvailableThreads(unsigned int &threadCnt); diff --git a/cpp/src/utils/SignalUtil.cpp b/cpp/src/utils/SignalUtil.cpp index 93b8ed4341..77b7a4ee2d 100644 --- a/cpp/src/utils/SignalUtil.cpp +++ b/cpp/src/utils/SignalUtil.cpp @@ -4,7 +4,6 @@ // Proprietary and confidential. //////////////////////////////////////////////////////////////////////////////// #include "SignalUtil.h" -#include "CommonUtil.h" #include "server/Server.h" #include "utils/Log.h" @@ -16,7 +15,7 @@ namespace vecwise { namespace server { void SignalUtil::HandleSignal(int signum){ - CommonUtil::PrintInfo("Server received signal:" + std::to_string(signum)); + SERVER_LOG_INFO << "Server received signal:" << std::to_string(signum); switch(signum){ case SIGINT: @@ -33,7 +32,7 @@ void SignalUtil::HandleSignal(int signum){ info += std::to_string(signum); // SendSignalMessage(signum, info); - CommonUtil::PrintInfo(info); + SERVER_LOG_INFO << info; server::Server* server_ptr = server::Server::Instance(); server_ptr->Stop(); @@ -44,7 +43,7 @@ void SignalUtil::HandleSignal(int signum){ } void SignalUtil::PrintStacktrace() { - CommonUtil::PrintInfo("Call stack:"); + SERVER_LOG_INFO << "Call stack:"; const int size = 32; void* array[size]; @@ -52,7 +51,7 @@ void SignalUtil::PrintStacktrace() { char ** stacktrace = backtrace_symbols(array, stack_num); for (int i = 0; i < stack_num; ++i) { std::string info = stacktrace[i]; - CommonUtil::PrintInfo(info); + SERVER_LOG_INFO << info; } free(stacktrace); } diff --git a/cpp/src/utils/TimeRecorder.cpp b/cpp/src/utils/TimeRecorder.cpp index da768a3cff..5d16a4c639 100644 --- a/cpp/src/utils/TimeRecorder.cpp +++ b/cpp/src/utils/TimeRecorder.cpp @@ -4,7 +4,7 @@ // Proprietary and confidential. //////////////////////////////////////////////////////////////////////////////// #include "TimeRecorder.h" -#include "utils/CommonUtil.h" +#include "utils/Log.h" namespace zilliz { @@ -87,31 +87,31 @@ TimeRecorder::PrintTimeRecord(const std::string &msg, double span) { switch (log_level_) { case 0: { - CommonUtil::PrintInfo(strLog); + SERVER_LOG_TRACE << strLog; break; } case 1: { - CommonUtil::PrintInfo(strLog); + SERVER_LOG_DEBUG << strLog; break; } case 2: { - CommonUtil::PrintInfo(strLog); + SERVER_LOG_INFO << strLog; break; } case 3: { - CommonUtil::PrintInfo(strLog); + SERVER_LOG_WARNING << strLog; break; } case 4: { - CommonUtil::PrintInfo(strLog); + SERVER_LOG_ERROR << strLog; break; } case 5: { - CommonUtil::PrintInfo(strLog); + SERVER_LOG_FATAL << strLog; break; } default: { - CommonUtil::PrintInfo(strLog); + SERVER_LOG_INFO << strLog; break; } } diff --git a/cpp/test_client/main.cpp b/cpp/test_client/main.cpp index 8b68d1c1da..f99780119d 100644 --- a/cpp/test_client/main.cpp +++ b/cpp/test_client/main.cpp @@ -42,7 +42,6 @@ main(int argc, char *argv[]) { char *config_filename_ptr = strdup(optarg); config_filename = config_filename_ptr; free(config_filename_ptr); - printf("Loading configuration from: %s\n", config_filename.c_str()); break; } case 'h': diff --git a/cpp/test_client/src/ClientApp.cpp b/cpp/test_client/src/ClientApp.cpp index fe9f368d83..72456c34df 100644 --- a/cpp/test_client/src/ClientApp.cpp +++ b/cpp/test_client/src/ClientApp.cpp @@ -5,7 +5,7 @@ ******************************************************************************/ #include "ClientApp.h" #include "server/ServerConfig.h" -#include "utils/CommonUtil.h" +#include "Log.h" #include #include @@ -36,13 +36,15 @@ void ClientApp::Run(const std::string &config_file) { server::ServerConfig& config = server::ServerConfig::GetInstance(); config.LoadConfigFile(config_file); + CLIENT_LOG_INFO << "Load config file:" << config_file; + server::ConfigNode server_config = config.GetConfig(server::CONFIG_SERVER); std::string address = server_config.GetValue(server::CONFIG_SERVER_ADDRESS, "127.0.0.1"); int32_t port = server_config.GetInt32Value(server::CONFIG_SERVER_PORT, 33001); std::string protocol = server_config.GetValue(server::CONFIG_SERVER_PROTOCOL, "binary"); std::string mode = server_config.GetValue(server::CONFIG_SERVER_MODE, "thread_pool"); - + CLIENT_LOG_INFO << "Connect to server: " << address << ":" << std::to_string(port); ::apache::thrift::stdcxx::shared_ptr socket_ptr(new ::apache::thrift::transport::TSocket(address, port)); ::apache::thrift::stdcxx::shared_ptr transport_ptr(new TBufferedTransport(socket_ptr)); ::apache::thrift::stdcxx::shared_ptr protocol_ptr; @@ -51,7 +53,7 @@ void ClientApp::Run(const std::string &config_file) { } else if(protocol == "json") { protocol_ptr.reset(new TJSONProtocol(transport_ptr)); } else { - server::CommonUtil::PrintError("Service protocol: " + protocol + " is not supported currently"); + CLIENT_LOG_ERROR << "Service protocol: " << protocol << " is not supported currently"; return; } @@ -70,6 +72,8 @@ void ClientApp::Run(const std::string &config_file) { } transport_ptr->close(); + + CLIENT_LOG_INFO << "Test finished"; } } diff --git a/cpp/test_client/src/Log.h b/cpp/test_client/src/Log.h new file mode 100644 index 0000000000..34af7319b4 --- /dev/null +++ b/cpp/test_client/src/Log.h @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ +#pragma once + +#include + +namespace zilliz { +namespace vecwise { +namespace client { + +#define CLIENT_DOMAIN_NAME "[CLIENT] " +#define CLIENT_ERROR_TEXT "CLIENT Error:" + +#define CLIENT_LOG_TRACE LOG(TRACE) << CLIENT_DOMAIN_NAME +#define CLIENT_LOG_DEBUG LOG(DEBUG) << CLIENT_DOMAIN_NAME +#define CLIENT_LOG_INFO LOG(INFO) << CLIENT_DOMAIN_NAME +#define CLIENT_LOG_WARNING LOG(WARNING) << CLIENT_DOMAIN_NAME +#define CLIENT_LOG_ERROR LOG(ERROR) << CLIENT_DOMAIN_NAME +#define CLIENT_LOG_FATAL LOG(FATAL) << CLIENT_DOMAIN_NAME + +} // namespace sql +} // namespace zilliz +} // namespace server