-
Notifications
You must be signed in to change notification settings - Fork 12
/
log.h
105 lines (86 loc) · 3.48 KB
/
log.h
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
#pragma once
#include <cerrno>
#include <chrono>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
namespace NLogging {
////////////////////////////////////////////////////////////////////////////////
enum class EVerbosity
{
MIN = 0,
ERROR = 1,
WARN = 2,
INFO = 3,
DEBUG = 4,
MAX,
};
struct LoggingEnv
{
EVerbosity current_verbosity = EVerbosity::INFO;
LoggingEnv()
{
if (auto value = std::getenv("VERBOSITY")) {
auto v = atoi(value);
if (v <= static_cast<int>(EVerbosity::MIN)
|| v >= static_cast<int>(EVerbosity::MAX)) {
abort();
}
current_verbosity = static_cast<EVerbosity>(v);
}
}
};
const LoggingEnv& logging_env();
////////////////////////////////////////////////////////////////////////////////
#define LOG(level, tag, message) \
{ \
if (NLogging::level <= NLogging::logging_env().current_verbosity) { \
const auto now = std::chrono::system_clock::now(); \
const auto tt = std::chrono::system_clock::to_time_t(now); \
static const auto format = "%Y-%m-%d %H:%M:%S"; \
\
std::cerr << "[" << std::put_time(std::localtime(&tt), format) \
<< " " << tag << "] " << (message) << "\n"; \
} \
} \
// LOG
////////////////////////////////////////////////////////////////////////////////
class LogMessage
{
private:
std::stringstream Data;
public:
template <typename T>
LogMessage& operator<<(const T& t)
{
Data << t;
return *this;
}
std::string extract()
{
return std::move(Data).str();
}
};
////////////////////////////////////////////////////////////////////////////////
#define LOG_S(level, tag, message) \
LOG(level, tag, (NLogging::LogMessage() << message).extract()); \
// LOG_S
#define LOG_DEBUG(message) LOG(EVerbosity::DEBUG, "DEBUG", message);
#define LOG_DEBUG_S(message) LOG_S(EVerbosity::DEBUG, "DEBUG", message);
#define LOG_WARN(message) LOG(EVerbosity::WARN, "WARN", message);
#define LOG_WARN_S(message) LOG_S(EVerbosity::WARN, "WARN", message);
#define LOG_INFO(message) LOG(EVerbosity::INFO, "INFO", message);
#define LOG_INFO_S(message) LOG_S(EVerbosity::INFO, "INFO", message);
#define LOG_ERROR(message) LOG(EVerbosity::ERROR, "ERROR", message);
#define LOG_ERROR_S(message) LOG_S(EVerbosity::ERROR, "ERROR", message);
#define LOG_PERROR(message) LOG_ERROR_S(message << ": " << std::strerror(errno));
#define VERIFY(condition, message) \
if (!(condition)) { \
LOG_ERROR(message); \
\
abort(); \
} \
// VERIFY
} // namespace NLogging