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
|
#pragma once
#include "base/base.hpp"
#include "base/internal/message.hpp"
#include "base/src_point.hpp"
#include <atomic>
namespace my
{
enum LogLevel
{
LDEBUG,
LINFO,
LWARNING,
LERROR,
LCRITICAL
};
using TLogLevel = std::atomic<LogLevel>;
typedef void (*LogMessageFn)(LogLevel level, SrcPoint const &, std::string const &);
extern LogMessageFn LogMessage;
extern TLogLevel g_LogLevel;
extern TLogLevel 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; }
};
}
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) {} \
else { ::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) {} \
else { ::my::LogMessage(level, my::SrcPoint(), ::my::impl::Message msg);} } while (false)
|