Use gettid() to get thread ID for logger on Linux.

master
Chunting Gu 7 years ago
parent c7bcb07f17
commit afc1b37f37

@ -114,6 +114,11 @@
"command": "/usr/bin/c++ -DBOOST_ASIO_NO_DEPRECATED -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -std=c++11 -o CMakeFiles/webcc.dir/soap_client.cc.o -c /home/adam/github/webcc/webcc/soap_client.cc",
"file": "/home/adam/github/webcc/webcc/soap_client.cc"
},
{
"directory": "/home/adam/github/webcc/build/webcc",
"command": "/usr/bin/c++ -DBOOST_ASIO_NO_DEPRECATED -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -std=c++11 -o CMakeFiles/webcc.dir/soap_globals.cc.o -c /home/adam/github/webcc/webcc/soap_globals.cc",
"file": "/home/adam/github/webcc/webcc/soap_globals.cc"
},
{
"directory": "/home/adam/github/webcc/build/webcc",
"command": "/usr/bin/c++ -DBOOST_ASIO_NO_DEPRECATED -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -std=c++11 -o CMakeFiles/webcc.dir/soap_message.cc.o -c /home/adam/github/webcc/webcc/soap_message.cc",
@ -159,6 +164,11 @@
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_server.dir/__/common/book.cc.o -c /home/adam/github/webcc/example/common/book.cc",
"file": "/home/adam/github/webcc/example/common/book.cc"
},
{
"directory": "/home/adam/github/webcc/build/example/rest_book_server",
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_server.dir/__/common/book_json.cc.o -c /home/adam/github/webcc/example/common/book_json.cc",
"file": "/home/adam/github/webcc/example/common/book_json.cc"
},
{
"directory": "/home/adam/github/webcc/build/example/rest_book_server",
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_server.dir/services.cc.o -c /home/adam/github/webcc/example/rest_book_server/services.cc",
@ -169,11 +179,31 @@
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_server.dir/main.cc.o -c /home/adam/github/webcc/example/rest_book_server/main.cc",
"file": "/home/adam/github/webcc/example/rest_book_server/main.cc"
},
{
"directory": "/home/adam/github/webcc/build/example/rest_book_client",
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_client.dir/__/common/book.cc.o -c /home/adam/github/webcc/example/common/book.cc",
"file": "/home/adam/github/webcc/example/common/book.cc"
},
{
"directory": "/home/adam/github/webcc/build/example/rest_book_client",
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_client.dir/__/common/book_json.cc.o -c /home/adam/github/webcc/example/common/book_json.cc",
"file": "/home/adam/github/webcc/example/common/book_json.cc"
},
{
"directory": "/home/adam/github/webcc/build/example/rest_book_client",
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_client.dir/main.cc.o -c /home/adam/github/webcc/example/rest_book_client/main.cc",
"file": "/home/adam/github/webcc/example/rest_book_client/main.cc"
},
{
"directory": "/home/adam/github/webcc/build/example/rest_book_async_client",
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_async_client.dir/__/common/book.cc.o -c /home/adam/github/webcc/example/common/book.cc",
"file": "/home/adam/github/webcc/example/common/book.cc"
},
{
"directory": "/home/adam/github/webcc/build/example/rest_book_async_client",
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_async_client.dir/__/common/book_json.cc.o -c /home/adam/github/webcc/example/common/book_json.cc",
"file": "/home/adam/github/webcc/example/common/book_json.cc"
},
{
"directory": "/home/adam/github/webcc/build/example/rest_book_async_client",
"command": "/usr/bin/c++ -DWEBCC_ENABLE_LOG -DWEBCC_ENABLE_SOAP -DWEBCC_LOG_LEVEL=0 -I/usr/local/include -I/home/adam/github/webcc -I/home/adam/github/webcc/third_party/src -I/home/adam/github/webcc/third_party/src/jsoncpp -std=c++11 -o CMakeFiles/rest_book_async_client.dir/main.cc.o -c /home/adam/github/webcc/example/rest_book_async_client/main.cc",

@ -131,6 +131,8 @@ Error HttpClient::SendReqeust(const HttpRequest& request) {
return kSocketWriteError;
}
LOG_INFO("Request sent.");
return kNoError;
}

@ -11,6 +11,14 @@
#include <string>
#include <thread>
#if (defined(WIN32) || defined(_WIN64))
// Do nothing.
#else
// For getting thread ID.
#include <sys/syscall.h>
#include <sys/types.h>
#endif
#include "boost/filesystem.hpp"
namespace webcc {
@ -47,7 +55,7 @@ struct Logger {
// Global logger.
static Logger g_logger;
static std::thread::id g_main_thread_id;
static std::string g_main_thread_id;
static const char* kLevelNames[] = {
"VERB", "INFO", "WARN", "ERRO", "FATA"
@ -55,6 +63,28 @@ static const char* kLevelNames[] = {
namespace bfs = boost::filesystem;
// std::this_thread::get_id() returns a very long ID (same as pthread_self())
// on Linux, e.g., 140219133990656. syscall(SYS_gettid) is much prefered because
// it's shorter and the same as `ps -T -p <pid>` output.
static std::string DoGetThreadID() {
#if (defined(WIN32) || defined(_WIN64))
auto thread_id = std::this_thread::get_id();
std::stringstream ss;
ss << thread_id;
return ss.str();
#else
return std::to_string(syscall(SYS_gettid));
#endif
}
static std::string GetThreadID() {
std::string thread_id = DoGetThreadID();
if (thread_id == g_main_thread_id) {
return "main";
}
return thread_id;
}
static bfs::path InitLogPath(const std::string& dir) {
if (dir.empty()) {
return bfs::current_path() / WEBCC_LOG_FILE_NAME;
@ -81,7 +111,7 @@ void LogInit(const std::string& dir, int modes) {
}
// Suppose LogInit() is called from the main thread.
g_main_thread_id = std::this_thread::get_id();
g_main_thread_id = DoGetThreadID();
}
static std::string GetTimestamp() {
@ -109,18 +139,6 @@ static std::string GetTimestamp() {
return timestamp;
}
static std::string GetThreadID() {
std::thread::id thread_id = std::this_thread::get_id();
if (thread_id == g_main_thread_id) {
return "main";
}
std::stringstream ss;
ss << thread_id;
return ss.str();
}
void LogWrite(int level, const char* file, int line, const char* format, ...) {
assert(format != nullptr);

Loading…
Cancel
Save