Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorvng <viktor.govako@gmail.com>2012-10-01 21:27:50 +0400
committerAlex Zolotarev <alex@maps.me>2015-09-23 01:44:10 +0300
commitd5cb7e74eafc81292fbe30a6a56f862960d11bc1 (patch)
tree0f1a0bf824fa742e20b5396cfbc9cb0c4c90cbcd /base
parentab0c326bea3e028420dfe55f12bdbd689182d52f (diff)
Make one static instance to initialize logging.
Diffstat (limited to 'base')
-rw-r--r--base/logging.cpp71
1 files changed, 47 insertions, 24 deletions
diff --git a/base/logging.cpp b/base/logging.cpp
index 6567f4f424..9d5765a4f6 100644
--- a/base/logging.cpp
+++ b/base/logging.cpp
@@ -32,42 +32,65 @@ namespace my
LogCheckIfErrorLevel(level);
}
#else
- void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg)
+
+ class LogHelper
{
- // TODO: Make LogMessageDefault() thread-safe?
- static threads::Mutex m;
- threads::MutexGuard g(m);
+ int m_threadsCount;
+ map<threads::ThreadID, int> m_threadID;
+
+ int GetThreadID()
+ {
+ int & id = m_threadID[threads::GetCurrentThreadID()];
+ if (id == 0)
+ id = ++m_threadsCount;
+ return id;
+ }
- static int threadsCount = 1;
- static map<threads::ThreadID, int> m_shortThreadID;
+ my::Timer m_timer;
- int & threadNumber = m_shortThreadID[threads::GetCurrentThreadID()];
- if (threadNumber == 0)
- threadNumber = threadsCount++;
+ char const * m_names[5];
+ size_t m_lens[5];
- ostringstream out;
- out << "LOG";
+ public:
+ threads::Mutex m_mutex;
+
+ LogHelper() : m_threadsCount(0)
+ {
+ m_names[0] = "DEBUG"; m_lens[0] = 5;
+ m_names[1] = "INFO"; m_lens[1] = 4;
+ m_names[2] = "WARNING"; m_lens[2] = 7;
+ m_names[3] = "ERROR"; m_lens[3] = 5;
+ m_names[4] = "CRITICAL"; m_lens[4] = 8;
+ }
+
+ void WriteProlog(ostream & s, LogLevel level)
+ {
+ s << "LOG";
+
+ s << " TID(" << GetThreadID() << ")";
+ s << " " << m_names[level];
- out << " TID(" << threadNumber << ")";
+ double const sec = m_timer.ElapsedSeconds();
+ s << " " << std::setfill(' ') << std::setw(16 - m_lens[level]) << sec << " ";
+ }
+ };
- static Timer s_Timer;
- static char const * names[] = { "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL" };
- static size_t const len[] = { 5, 4, 7, 5, 8 };
+ void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg)
+ {
+ static LogHelper logger;
- out << " " << names[level];
+ threads::MutexGuard guard(logger.m_mutex);
+ UNUSED_VALUE(guard);
- //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);
+ ostringstream out;
+ logger.WriteProlog(out, level);
- double const sec = s_Timer.ElapsedSeconds();
- out << " " << std::setfill(' ') << std::setw(16 - len[level]) << sec;
+ out << DebugPrint(srcPoint) << msg << endl;
- out << " " << DebugPrint(srcPoint) << msg << endl;
+ std::cerr << out.str();
- string const outString = out.str();
- std::cerr << outString;
#ifdef OMIM_OS_WINDOWS
- OutputDebugStringA(outString.c_str());
+ OutputDebugStringA(out.str().c_str());
#endif
LogCheckIfErrorLevel(level);
}