Use gettid() to get thread ID for logger on Linux.
This commit is contained in:
@@ -131,6 +131,8 @@ Error HttpClient::SendReqeust(const HttpRequest& request) {
|
||||
return kSocketWriteError;
|
||||
}
|
||||
|
||||
LOG_INFO("Request sent.");
|
||||
|
||||
return kNoError;
|
||||
}
|
||||
|
||||
|
||||
+32
-14
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user