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

logging.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 163041e0fbf53d4c6411e25782fa907bb408fc9d (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
#pragma once

#include "base/base.hpp"
#include "base/internal/message.hpp"
#include "base/src_point.hpp"

#include <array>
#include <atomic>
#include <string>

namespace my
{
enum LogLevel
{
  LDEBUG,
  LINFO,
  LWARNING,
  LERROR,
  LCRITICAL,

  NUM_LOG_LEVELS
};

std::string ToString(LogLevel level);
bool FromString(std::string const & s, LogLevel & level);
std::array<char const *, NUM_LOG_LEVELS> const & GetLogLevelNames();

using AtomicLogLevel = std::atomic<LogLevel>;
using LogMessageFn = void (*)(LogLevel level, SrcPoint const &, std::string const &);

LogLevel GetDefaultLogLevel();
LogLevel GetDefaultLogAbortLevel();

extern LogMessageFn LogMessage;
extern AtomicLogLevel g_LogLevel;
extern AtomicLogLevel g_LogAbortLevel;

/// @return Pointer to previous message function.
LogMessageFn SetLogMessageFn(LogMessageFn fn);

void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, std::string const & msg);
void LogMessageTests(LogLevel level, SrcPoint const & srcPoint, std::string const & msg);

/// Scope Guard to temporarily suppress specific log level, for example, in unit tests:
/// ...
/// {
///   LogLevelSuppressor onlyLERRORAndLCriticalLogsAreEnabled;
///   TEST(SomeFunctionWhichHasDebugOrInfoOrWarningLogs(), ());
/// }
struct ScopedLogLevelChanger
{
  LogLevel m_old = g_LogLevel;
  ScopedLogLevelChanger(LogLevel temporaryLogLevel = LERROR) { g_LogLevel = temporaryLogLevel; }
  ~ScopedLogLevelChanger() { g_LogLevel = m_old; }
};

struct ScopedLogAbortLevelChanger
{
  LogLevel m_old = g_LogAbortLevel;
  ScopedLogAbortLevelChanger(LogLevel temporaryLogAbortLevel = LCRITICAL)
  {
    g_LogAbortLevel = temporaryLogAbortLevel;
  }
  ~ScopedLogAbortLevelChanger() { g_LogAbortLevel = m_old; }
};
}  // namespace my

using ::my::LDEBUG;
using ::my::LINFO;
using ::my::LWARNING;
using ::my::LERROR;
using ::my::LCRITICAL;

// Logging macro.
// Example usage: LOG(LINFO, (Calc(), m_Var, "Some string constant"));
#define LOG(level, msg)                                        \
  do                                                           \
  {                                                            \
    if ((level) >= ::my::g_LogLevel)                           \
      ::my::LogMessage(level, SRC(), ::my::impl::Message msg); \
  } while (false)

// Logging macro with short info (without entry point)
#define LOG_SHORT(level, msg)                                           \
  do                                                                    \
  {                                                                     \
    if ((level) >= ::my::g_LogLevel)                                    \
      ::my::LogMessage(level, my::SrcPoint(), ::my::impl::Message msg); \
  } while (false)