From 1fae3baaa3270081c68c645c1db03ecce9dc6d24 Mon Sep 17 00:00:00 2001 From: hagen Date: Mon, 28 Mar 2016 14:00:00 +0000 Subject: [PATCH] * logger: print also thread id --- Log.cpp | 16 +++++++++++++--- Log.h | 4 +++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Log.cpp b/Log.cpp index 06aabac5..36fcebf3 100644 --- a/Log.cpp +++ b/Log.cpp @@ -92,22 +92,32 @@ namespace log { */ void Log::Process() { std::unique_lock l(m_OutputLock); + std::hash hasher; + unsigned short short_tid; while (1) { auto msg = m_Queue.GetNextWithTimeout (1); if (!msg) break; + short_tid = (short) (hasher(msg->tid) % 1000); switch (m_Destination) { #ifndef _WIN32 case eLogSyslog: - syslog(GetSyslogPrio(msg->level), "%s", msg->text.c_str()); + syslog(GetSyslogPrio(msg->level), "[%03u] %s", short_tid, msg->text.c_str()); break; #endif case eLogFile: case eLogStream: - *m_LogStream << TimeAsString(msg->timestamp) << "/" << g_LogLevelStr[msg->level] << " - " << msg->text << std::endl; + *m_LogStream << TimeAsString(msg->timestamp) + << "@" << short_tid + << "/" << g_LogLevelStr[msg->level] + << " - " << msg->text << std::endl; break; + case eLogStdout: default: - std::cout << TimeAsString(msg->timestamp) << "/" << g_LogLevelStr[msg->level] << " - " << msg->text << std::endl; + std::cout << TimeAsString(msg->timestamp) + << "@" << short_tid + << "/" << g_LogLevelStr[msg->level] + << " - " << msg->text << std::endl; break; } // switch } // while diff --git a/Log.h b/Log.h index 6cda62f6..ebb73a12 100644 --- a/Log.h +++ b/Log.h @@ -138,8 +138,9 @@ namespace log { std::time_t timestamp; std::string text; /**< message text as single string */ LogLevel level; /**< message level */ + std::thread::id tid; /**< id of thread that generated message */ - LogMsg (LogLevel lvl, std::time_t ts, const std::string & txt): timestamp(ts), text(txt), level(lvl) {}; + LogMsg (LogLevel lvl, std::time_t ts, const std::string & txt): timestamp(ts), text(txt), level(lvl), tid(0) {}; }; Log & Logger(); @@ -178,6 +179,7 @@ void LogPrint (LogLevel level, TArgs... args) LogPrint (ss, args ...); auto msg = std::make_shared(level, std::time(nullptr), ss.str()); + msg->tid = std::this_thread::get_id(); log.Append(msg); }