diff options
author | rachytski <siarhei.rachytski@gmail.com> | 2012-04-23 19:16:15 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:37:54 +0300 |
commit | 962930907f8bfeb0dab2fd36b87be5d651fd637f (patch) | |
tree | 27de187ef30e18eabb9e083b338834703e41d4a9 /base | |
parent | 62110150402dd7e1c82efda9ab7e63197ef5f045 (diff) |
made logging thread-safe and adding ThreadID tag at the beginning of every string.
Diffstat (limited to 'base')
-rw-r--r-- | base/logging.cpp | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/base/logging.cpp b/base/logging.cpp index 7a3b3b9073..8e9bddb6e8 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -2,6 +2,8 @@ #include "logging.hpp" #include "macros.hpp" #include "timer.hpp" +#include "thread.hpp" +#include "mutex.hpp" #include "../std/iostream.hpp" #include "../std/iomanip.hpp" @@ -33,12 +35,25 @@ namespace my void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg) { // TODO: Make LogMessageDefault() thread-safe? + static threads::Mutex m; + threads::MutexGuard g(m); + + static int threadsCount = 1; + static map<threads::ThreadID, int> m_shortThreadID; + threads::ThreadID id = threads::GetCurrentThreadID(); + if (m_shortThreadID[id] == 0) + m_shortThreadID[id] = threadsCount++; + + ostringstream out; + out << "LOG"; + + out << " TID(" << m_shortThreadID[id] << ")"; + static Timer s_Timer; static char const * names[] = { "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL" }; static size_t const len[] = { 5, 4, 7, 5, 8 }; - ostringstream out; - out << "LOG "; - out << names[level]; + + out << " " << names[level]; //int64_t const milliseconds = static_cast<int64_t>(s_Timer.ElapsedSeconds() * 1000 + 0.5); //std::cerr << " " << std::setw(6) << milliseconds / 1000 << "." << std::setw(4) << std::setiosflags(std::ios::left) << (milliseconds % 1000) << std::resetiosflags(std::ios::left); |