refine log code

Former-commit-id: 96584eb602bacf90d6482cdef60608fa31f488f1
This commit is contained in:
groot 2019-04-16 16:50:34 +08:00
parent 18994f2aa9
commit 5a8b70b779
11 changed files with 69 additions and 56 deletions

View File

@ -5,7 +5,7 @@
******************************************************************************/ ******************************************************************************/
#include "ConfigNode.h" #include "ConfigNode.h"
#include "utils/Error.h" #include "utils/Error.h"
#include "utils/CommonUtil.h" #include "utils/Log.h"
#include <sstream> #include <sstream>
#include <string> #include <string>
@ -177,18 +177,18 @@ void ConfigNode::ClearSequences() {
void void
ConfigNode::PrintAll(const std::string& prefix) const { ConfigNode::PrintAll(const std::string& prefix) const {
for(auto& elem : config_) { for(auto& elem : config_) {
CommonUtil::PrintInfo(prefix + elem.first + ": " + elem.second); SERVER_LOG_INFO << prefix << elem.first + ": " << elem.second;
} }
for(auto& elem : sequences_) { for(auto& elem : sequences_) {
CommonUtil::PrintInfo(prefix + elem.first + ": "); SERVER_LOG_INFO << prefix << elem.first << ": ";
for(auto& str : elem.second) { for(auto& str : elem.second) {
CommonUtil::PrintInfo(prefix + " - " + str); SERVER_LOG_INFO << prefix << " - " << str;
} }
} }
for(auto& elem : children_) { for(auto& elem : children_) {
CommonUtil::PrintInfo(prefix + elem.first + ": "); SERVER_LOG_INFO << prefix << elem.first << ": ";
elem.second.PrintAll(prefix + " "); elem.second.PrintAll(prefix + " ");
} }
} }

View File

@ -4,7 +4,7 @@
* Proprietary and confidential. * Proprietary and confidential.
******************************************************************************/ ******************************************************************************/
#include "YamlConfigMgr.h" #include "YamlConfigMgr.h"
#include "utils/CommonUtil.h" #include "utils/Log.h"
#include <sys/stat.h> #include <sys/stat.h>
@ -16,7 +16,7 @@ ServerError YamlConfigMgr::LoadConfigFile(const std::string &filename) {
struct stat directoryStat; struct stat directoryStat;
int statOK = stat(filename.c_str(), &directoryStat); int statOK = stat(filename.c_str(), &directoryStat);
if (statOK != 0) { if (statOK != 0) {
CommonUtil::PrintError("File not found: " + filename); SERVER_LOG_ERROR << "File not found: " << filename;
return SERVER_UNEXPECTED_ERROR; return SERVER_UNEXPECTED_ERROR;
} }
@ -25,7 +25,7 @@ ServerError YamlConfigMgr::LoadConfigFile(const std::string &filename) {
LoadConfigNode(node_, config_); LoadConfigNode(node_, config_);
} }
catch (YAML::Exception& e) { 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; return SERVER_UNEXPECTED_ERROR;
} }
@ -33,7 +33,7 @@ ServerError YamlConfigMgr::LoadConfigFile(const std::string &filename) {
} }
void YamlConfigMgr::Print() const { void YamlConfigMgr::Print() const {
CommonUtil::PrintInfo("System config content:"); SERVER_LOG_INFO << "System config content:";
config_.PrintAll(); config_.PrintAll();
} }

View File

@ -6,7 +6,7 @@
#include "Server.h" #include "Server.h"
#include "ServerConfig.h" #include "ServerConfig.h"
#include "ServiceWrapper.h" #include "ServiceWrapper.h"
#include "utils/CommonUtil.h" #include "utils/Log.h"
#include "utils/SignalUtil.h" #include "utils/SignalUtil.h"
#include "utils/TimeRecorder.h" #include "utils/TimeRecorder.h"
#include "utils/LogUtil.h" #include "utils/LogUtil.h"
@ -50,11 +50,11 @@ Server::Daemonize() {
return; return;
} }
CommonUtil::PrintInfo("Vecwise server run in daemonize mode"); SERVER_LOG_INFO << "Vecwise server run in daemonize mode";
// std::string log_path(GetLogDirFullPath()); // std::string log_path(GetLogDirFullPath());
// log_path += "zdb_server.(INFO/WARNNING/ERROR/CRITICAL)"; // 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; pid_t pid = 0;
@ -106,7 +106,7 @@ Server::Daemonize() {
close(fd); 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 // Redirect stdin/stdout/stderr to /dev/null
stdin = fopen("/dev/null", "r"); stdin = fopen("/dev/null", "r");
@ -116,11 +116,11 @@ Server::Daemonize() {
if (!pid_filename_.empty()) { if (!pid_filename_.empty()) {
pid_fd = open(pid_filename_.c_str(), O_RDWR | O_CREAT, 0640); pid_fd = open(pid_filename_.c_str(), O_RDWR | O_CREAT, 0640);
if (pid_fd < 0) { 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); exit(EXIT_FAILURE);
} }
if (lockf(pid_fd, F_TLOCK, 0) < 0) { 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); exit(EXIT_FAILURE);
} }
@ -159,14 +159,12 @@ Server::Start() {
signal(SIGHUP, SignalUtil::HandleSignal); signal(SIGHUP, SignalUtil::HandleSignal);
signal(SIGTERM, SignalUtil::HandleSignal); signal(SIGTERM, SignalUtil::HandleSignal);
CommonUtil::PrintInfo("Vecwise server is running..."); SERVER_LOG_INFO << "Vecwise server is running...";
StartService(); StartService();
} catch(std::exception& ex){ } catch(std::exception& ex){
std::string info = "Vecwise server encounter exception: " + std::string(ex.what()); SERVER_LOG_ERROR << "Vecwise server encounter exception: " << std::string(ex.what())
CommonUtil::PrintError(info); << "Is another server instance running?";
CommonUtil::PrintInfo("Is another server instance running?");
break; break;
} }
} while(false); } while(false);
@ -177,7 +175,7 @@ Server::Start() {
void void
Server::Stop() { Server::Stop() {
CommonUtil::PrintInfo("Vecwise server will be closed"); SERVER_LOG_INFO << "Vecwise server will be closed";
// Unlock and close lockfile // Unlock and close lockfile
if (pid_fd != -1) { if (pid_fd != -1) {
@ -204,7 +202,7 @@ Server::Stop() {
StopService(); StopService();
CommonUtil::PrintInfo("Vecwise server closed"); SERVER_LOG_INFO << "Vecwise server closed";
} }

View File

@ -6,7 +6,7 @@
#include "ServiceWrapper.h" #include "ServiceWrapper.h"
#include "ServerConfig.h" #include "ServerConfig.h"
#include "utils/CommonUtil.h" #include "utils/Log.h"
#include "thrift/gen-cpp/VecService.h" #include "thrift/gen-cpp/VecService.h"
#include "thrift/gen-cpp/VectorService_types.h" #include "thrift/gen-cpp/VectorService_types.h"
@ -40,7 +40,7 @@ public:
void dummy() { void dummy() {
// Your implementation goes here // Your implementation goes here
printf("dummy\n"); printf("dummy() called\n");
} }
/** /**
@ -127,7 +127,7 @@ void ServiceWrapper::StartService() {
} else if(protocol == "json") { } else if(protocol == "json") {
protocolFactory.reset(new TJSONProtocolFactory()); protocolFactory.reset(new TJSONProtocolFactory());
} else { } else {
CommonUtil::PrintError("Service protocol: " + protocol + " is not supported currently"); SERVER_LOG_INFO << "Service protocol: " << protocol << " is not supported currently";
return; return;
} }
@ -143,7 +143,7 @@ void ServiceWrapper::StartService() {
s_server.reset(new TThreadPoolServer(processor, serverTransport, transportFactory, protocolFactory, threadManager)); s_server.reset(new TThreadPoolServer(processor, serverTransport, transportFactory, protocolFactory, threadManager));
s_server->serve(); s_server->serve();
} else { } else {
CommonUtil::PrintError("Service mode: " + mode + " is not supported currently"); SERVER_LOG_INFO << "Service mode: " << mode << " is not supported currently";
return; return;
} }
} }

View File

@ -31,16 +31,6 @@ namespace server {
namespace fs = boost::filesystem; 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) { bool CommonUtil::GetSystemMemInfo(unsigned long &totalMem, unsigned long &freeMem) {
struct sysinfo info; struct sysinfo info;
int ret = sysinfo(&info); int ret = sysinfo(&info);

View File

@ -15,9 +15,6 @@ namespace server {
class CommonUtil { class CommonUtil {
public: 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 GetSystemMemInfo(unsigned long &totalMem, unsigned long &freeMem);
static bool GetSystemAvailableThreads(unsigned int &threadCnt); static bool GetSystemAvailableThreads(unsigned int &threadCnt);

View File

@ -4,7 +4,6 @@
// Proprietary and confidential. // Proprietary and confidential.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include "SignalUtil.h" #include "SignalUtil.h"
#include "CommonUtil.h"
#include "server/Server.h" #include "server/Server.h"
#include "utils/Log.h" #include "utils/Log.h"
@ -16,7 +15,7 @@ namespace vecwise {
namespace server { namespace server {
void SignalUtil::HandleSignal(int signum){ 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){ switch(signum){
case SIGINT: case SIGINT:
@ -33,7 +32,7 @@ void SignalUtil::HandleSignal(int signum){
info += std::to_string(signum); info += std::to_string(signum);
// SendSignalMessage(signum, info); // SendSignalMessage(signum, info);
CommonUtil::PrintInfo(info); SERVER_LOG_INFO << info;
server::Server* server_ptr = server::Server::Instance(); server::Server* server_ptr = server::Server::Instance();
server_ptr->Stop(); server_ptr->Stop();
@ -44,7 +43,7 @@ void SignalUtil::HandleSignal(int signum){
} }
void SignalUtil::PrintStacktrace() { void SignalUtil::PrintStacktrace() {
CommonUtil::PrintInfo("Call stack:"); SERVER_LOG_INFO << "Call stack:";
const int size = 32; const int size = 32;
void* array[size]; void* array[size];
@ -52,7 +51,7 @@ void SignalUtil::PrintStacktrace() {
char ** stacktrace = backtrace_symbols(array, stack_num); char ** stacktrace = backtrace_symbols(array, stack_num);
for (int i = 0; i < stack_num; ++i) { for (int i = 0; i < stack_num; ++i) {
std::string info = stacktrace[i]; std::string info = stacktrace[i];
CommonUtil::PrintInfo(info); SERVER_LOG_INFO << info;
} }
free(stacktrace); free(stacktrace);
} }

View File

@ -4,7 +4,7 @@
// Proprietary and confidential. // Proprietary and confidential.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include "TimeRecorder.h" #include "TimeRecorder.h"
#include "utils/CommonUtil.h" #include "utils/Log.h"
namespace zilliz { namespace zilliz {
@ -87,31 +87,31 @@ TimeRecorder::PrintTimeRecord(const std::string &msg, double span) {
switch (log_level_) { switch (log_level_) {
case 0: { case 0: {
CommonUtil::PrintInfo(strLog); SERVER_LOG_TRACE << strLog;
break; break;
} }
case 1: { case 1: {
CommonUtil::PrintInfo(strLog); SERVER_LOG_DEBUG << strLog;
break; break;
} }
case 2: { case 2: {
CommonUtil::PrintInfo(strLog); SERVER_LOG_INFO << strLog;
break; break;
} }
case 3: { case 3: {
CommonUtil::PrintInfo(strLog); SERVER_LOG_WARNING << strLog;
break; break;
} }
case 4: { case 4: {
CommonUtil::PrintInfo(strLog); SERVER_LOG_ERROR << strLog;
break; break;
} }
case 5: { case 5: {
CommonUtil::PrintInfo(strLog); SERVER_LOG_FATAL << strLog;
break; break;
} }
default: { default: {
CommonUtil::PrintInfo(strLog); SERVER_LOG_INFO << strLog;
break; break;
} }
} }

View File

@ -42,7 +42,6 @@ main(int argc, char *argv[]) {
char *config_filename_ptr = strdup(optarg); char *config_filename_ptr = strdup(optarg);
config_filename = config_filename_ptr; config_filename = config_filename_ptr;
free(config_filename_ptr); free(config_filename_ptr);
printf("Loading configuration from: %s\n", config_filename.c_str());
break; break;
} }
case 'h': case 'h':

View File

@ -5,7 +5,7 @@
******************************************************************************/ ******************************************************************************/
#include "ClientApp.h" #include "ClientApp.h"
#include "server/ServerConfig.h" #include "server/ServerConfig.h"
#include "utils/CommonUtil.h" #include "Log.h"
#include <iostream> #include <iostream>
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
@ -36,13 +36,15 @@ void ClientApp::Run(const std::string &config_file) {
server::ServerConfig& config = server::ServerConfig::GetInstance(); server::ServerConfig& config = server::ServerConfig::GetInstance();
config.LoadConfigFile(config_file); config.LoadConfigFile(config_file);
CLIENT_LOG_INFO << "Load config file:" << config_file;
server::ConfigNode server_config = config.GetConfig(server::CONFIG_SERVER); server::ConfigNode server_config = config.GetConfig(server::CONFIG_SERVER);
std::string address = server_config.GetValue(server::CONFIG_SERVER_ADDRESS, "127.0.0.1"); 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); int32_t port = server_config.GetInt32Value(server::CONFIG_SERVER_PORT, 33001);
std::string protocol = server_config.GetValue(server::CONFIG_SERVER_PROTOCOL, "binary"); std::string protocol = server_config.GetValue(server::CONFIG_SERVER_PROTOCOL, "binary");
std::string mode = server_config.GetValue(server::CONFIG_SERVER_MODE, "thread_pool"); 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<TSocket> socket_ptr(new ::apache::thrift::transport::TSocket(address, port)); ::apache::thrift::stdcxx::shared_ptr<TSocket> socket_ptr(new ::apache::thrift::transport::TSocket(address, port));
::apache::thrift::stdcxx::shared_ptr<TTransport> transport_ptr(new TBufferedTransport(socket_ptr)); ::apache::thrift::stdcxx::shared_ptr<TTransport> transport_ptr(new TBufferedTransport(socket_ptr));
::apache::thrift::stdcxx::shared_ptr<TProtocol> protocol_ptr; ::apache::thrift::stdcxx::shared_ptr<TProtocol> protocol_ptr;
@ -51,7 +53,7 @@ void ClientApp::Run(const std::string &config_file) {
} else if(protocol == "json") { } else if(protocol == "json") {
protocol_ptr.reset(new TJSONProtocol(transport_ptr)); protocol_ptr.reset(new TJSONProtocol(transport_ptr));
} else { } else {
server::CommonUtil::PrintError("Service protocol: " + protocol + " is not supported currently"); CLIENT_LOG_ERROR << "Service protocol: " << protocol << " is not supported currently";
return; return;
} }
@ -70,6 +72,8 @@ void ClientApp::Run(const std::string &config_file) {
} }
transport_ptr->close(); transport_ptr->close();
CLIENT_LOG_INFO << "Test finished";
} }
} }

26
cpp/test_client/src/Log.h Normal file
View File

@ -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 <easylogging++.h>
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