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

logging.cpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50be16e450e8c488b3e29587bb5188e7dc82c03a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "base/logging.hpp"

#include "base/assert.hpp"
#include "base/macros.hpp"
#include "base/thread.hpp"
#include "base/timer.hpp"

#include "std/target_os.hpp"

#include <algorithm>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <mutex>
#include <sstream>

using namespace std;

namespace
{
mutex g_logMutex;
}  // namespace

namespace base
{
string ToString(LogLevel level)
{
  auto const & names = GetLogLevelNames();
  CHECK_LESS(level, names.size(), ());
  return names[level];
}

bool FromString(string const & s, LogLevel & level)
{
  auto const & names = GetLogLevelNames();
  auto it = find(names.begin(), names.end(), s);
  if (it == names.end())
    return false;
  level = static_cast<LogLevel>(distance(names.begin(), it));
  return true;
}

array<char const *, NUM_LOG_LEVELS> const & GetLogLevelNames()
{
  // If you're going to modify the behavior of the function, please,
  // check validity of LogHelper ctor.
  static array<char const *, NUM_LOG_LEVELS> const kNames = {
      {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}};
  return kNames;
}

class LogHelper
{
  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;
  }

  base::Timer m_timer;

  array<char const *, NUM_LOG_LEVELS> m_names;
  array<size_t, NUM_LOG_LEVELS> m_lens;

public:
  LogHelper() : m_threadsCount(0)
  {
    // This code highly depends on the fact that GetLogLevelNames()
    // always returns the same constant array of strings.

    m_names = GetLogLevelNames();
    for (size_t i = 0; i < m_lens.size(); ++i)
      m_lens[i] = strlen(m_names[i]);
  }

  void WriteProlog(ostream & s, LogLevel level)
  {
    s << "LOG";

    s << " TID(" << GetThreadID() << ")";
    s << " " << m_names[level];

    double const sec = m_timer.ElapsedSeconds();
    s << " " << setfill(' ') << setw(static_cast<int>(16 - m_lens[level])) << sec << " ";
  }
};

void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg)
{
  lock_guard<mutex> lock(g_logMutex);

  static LogHelper logger;

  ostringstream out;
  logger.WriteProlog(out, level);

  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 &, string const & msg)
{
  lock_guard<mutex> lock(g_logMutex);

  ostringstream out;
  out << msg << endl;
  cerr << out.str();

  CHECK_LESS(level, g_LogAbortLevel, ("Abort. Log level is too serious", level));
}

LogMessageFn LogMessage = &LogMessageDefault;

LogMessageFn SetLogMessageFn(LogMessageFn fn)
{
  swap(LogMessage, fn);
  return fn;
}

LogLevel GetDefaultLogLevel()
{
#if defined(DEBUG)
  return LDEBUG;
#else
  return LINFO;
#endif  // defined(DEBUG)
}

LogLevel GetDefaultLogAbortLevel()
{
#if defined(DEBUG)
  return LERROR;
#else
  return LCRITICAL;
#endif  // defined(DEBUG)
}

AtomicLogLevel g_LogLevel = {GetDefaultLogLevel()};
AtomicLogLevel g_LogAbortLevel = {GetDefaultLogAbortLevel()};
}  // namespace base