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
diff options
context:
space:
mode:
Diffstat (limited to 'base/logging.cpp')
-rw-r--r--base/logging.cpp81
1 files changed, 44 insertions, 37 deletions
diff --git a/base/logging.cpp b/base/logging.cpp
index 1ceb63e2f7..ae1415458b 100644
--- a/base/logging.cpp
+++ b/base/logging.cpp
@@ -1,51 +1,60 @@
#include "base/logging.hpp"
+
#include "base/assert.hpp"
#include "base/macros.hpp"
-#include "base/mutex.hpp"
#include "base/thread.hpp"
#include "base/timer.hpp"
#include "std/target_os.hpp"
-//#include "std/windows.hpp"
#include <algorithm>
+#include <cassert>
#include <iomanip>
#include <iostream>
#include <iterator>
+#include <map>
#include <mutex>
#include <sstream>
#include <vector>
+using namespace std;
+
+namespace
+{
+mutex g_logMutex;
+} // namespace
+
namespace my
{
-std::string ToString(LogLevel level)
+string ToString(LogLevel level)
{
auto const & names = GetLogLevelNames();
CHECK_LESS(level, names.size(), ());
return names[level];
}
-bool FromString(std::string const & s, LogLevel & level)
+bool FromString(string const & s, LogLevel & level)
{
auto const & names = GetLogLevelNames();
- auto it = std::find(names.begin(), names.end(), s);
+ auto it = find(names.begin(), names.end(), s);
if (it == names.end())
return false;
- level = static_cast<LogLevel>(std::distance(names.begin(), it));
+ level = static_cast<LogLevel>(distance(names.begin(), it));
return true;
}
-std::vector<std::string> const & GetLogLevelNames()
+vector<string> const & GetLogLevelNames()
{
- static std::vector<std::string> const kNames = {
- {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}};
+ // If you're going to modify the behavior of the function, please,
+ // check validity of LogHelper ctor.
+ static vector<string> const kNames = {{"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}};
return kNames;
}
class LogHelper
{
int m_threadsCount;
- std::map<threads::ThreadID, int> m_threadID;
+ map<threads::ThreadID, int> m_threadID;
int GetThreadID()
{
@@ -63,19 +72,19 @@ class LogHelper
public:
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;
+ // This code highly depends on the fact that GetLogLevelNames()
+ // always returns the same constant vector of strings.
+ auto const & names = GetLogLevelNames();
+
+ assert(names.size() == 5);
+ for (size_t i = 0; i < 5; ++i)
+ {
+ m_names[i] = names[i].c_str();
+ m_lens[i] = names[i].size();
+ }
}
- void WriteProlog(std::ostream & s, LogLevel level)
+ void WriteProlog(ostream & s, LogLevel level)
{
s << "LOG";
@@ -83,34 +92,32 @@ public:
s << " " << m_names[level];
double const sec = m_timer.ElapsedSeconds();
- s << " " << std::setfill(' ') << std::setw(static_cast<int>(16 - m_lens[level])) << sec << " ";
+ s << " " << setfill(' ') << setw(static_cast<int>(16 - m_lens[level])) << sec << " ";
}
};
-std::mutex g_logMutex;
-
-void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, std::string const & msg)
+void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg)
{
- std::lock_guard<std::mutex> lock(g_logMutex);
+ lock_guard<mutex> lock(g_logMutex);
static LogHelper logger;
- std::ostringstream out;
+ ostringstream out;
logger.WriteProlog(out, level);
- out << DebugPrint(srcPoint) << msg << std::endl;
- std::cerr << out.str();
+ out << DebugPrint(srcPoint) << msg << endl;
+ cerr << out.str();
CHECK_LESS(level, g_LogAbortLevel, ("Abort. Log level is too serious", level));
}
-void LogMessageTests(LogLevel level, SrcPoint const &, std::string const & msg)
+void LogMessageTests(LogLevel level, SrcPoint const &, string const & msg)
{
- std::lock_guard<std::mutex> lock(g_logMutex);
+ lock_guard<mutex> lock(g_logMutex);
- std::ostringstream out;
- out << msg << std::endl;
- std::cerr << out.str();
+ ostringstream out;
+ out << msg << endl;
+ cerr << out.str();
CHECK_LESS(level, g_LogAbortLevel, ("Abort. Log level is too serious", level));
}
@@ -119,7 +126,7 @@ LogMessageFn LogMessage = &LogMessageDefault;
LogMessageFn SetLogMessageFn(LogMessageFn fn)
{
- std::swap(LogMessage, fn);
+ swap(LogMessage, fn);
return fn;
}
@@ -141,6 +148,6 @@ LogLevel GetDefaultLogAbortLevel()
#endif // defined(DEBUG)
}
-TLogLevel g_LogLevel = {GetDefaultLogLevel()};
-TLogLevel g_LogAbortLevel = {GetDefaultLogAbortLevel()};
+AtomicLogLevel g_LogLevel = {GetDefaultLogLevel()};
+AtomicLogLevel g_LogAbortLevel = {GetDefaultLogAbortLevel()};
} // namespace my