-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.h
441 lines (374 loc) · 11.8 KB
/
logging.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* logging.h
* Copyright (C) 2022 youfa.song <[email protected]>
*
* Distributed under terms of the GPLv2 license.
*/
#ifndef AVE_LOGGING_H
#define AVE_LOGGING_H
#include <atomic>
#include <cstdint>
#include <sstream>
#include <string>
#include <utility>
#include "base/constructor_magic.h"
#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
#define AVE_DLOG_IS_ON 1
#else
#define AVE_DLOG_IS_ON 0
#endif
#if defined(AVE_DISABLE_LOGGING)
#define AVE_LOG_ENABLED() 0
#else
#define AVE_LOG_ENABLED() 1
#endif
namespace ave {
namespace base {
enum LogSeverity {
LS_VERBOSE,
LS_DEBUG,
LS_INFO,
LS_WARNING,
LS_ERROR,
LS_NONE,
};
enum LogErrorContext {
ERRCTX_NONE,
ERRCTX_ERRNO,
};
class LogMessage;
class LogSink {
public:
LogSink() = default;
virtual ~LogSink() = default;
virtual void OnLogMessage(const std::string& msg,
LogSeverity severity,
const char* tag);
virtual void OnLogMessage(const std::string& msg, LogSeverity severity);
virtual void OnLogMessage(const std::string& msg) = 0;
private:
friend class ::ave::base::LogMessage;
#if AVE_LOG_ENABLED()
LogSink* next_ = nullptr;
LogSeverity min_severity_ = LS_VERBOSE;
#endif
};
namespace logging_impl {
class LogMetadata {
public:
LogMetadata(const char* file, int line, LogSeverity severity)
: file_(file),
line_and_sev_(static_cast<uint32_t>(static_cast<uint32_t>(line) << 3 |
static_cast<uint32_t>(severity))) {}
LogMetadata() = default;
const char* File() const { return file_; }
int Line() const { return static_cast<int>(line_and_sev_ >> 3); }
LogSeverity Severity() const {
return static_cast<LogSeverity>(line_and_sev_ & 0x7);
}
private:
const char* file_;
// Line number and severity, the former in the most significant 29 bits, the
// latter in the least significant 3 bits. (This is an optimization; since
// both numbers are usually compile-time constants, this way we can load them
// both with a single instruction.)
uint32_t line_and_sev_;
};
static_assert(std::is_trivial_v<LogMetadata>);
struct LogMetadataErr {
LogMetadata meta;
LogErrorContext err_ctx;
int err;
};
enum class LogArgType : uint8_t {
kEnd = 0,
kInt,
kLong,
kLongLong,
kUInt,
kULong,
kULongLong,
kDouble,
kLongDouble,
kCharP,
kStdString,
kVoidP,
kLogMetadata,
kLogMetadataErr,
};
template <LogArgType N, typename T>
struct Val {
static constexpr LogArgType Type() { return N; }
T GetVal() const { return val; }
T val;
};
struct ToStringVal {
static constexpr LogArgType Type() { return LogArgType::kStdString; }
const std::string* GetVal() const { return &val; }
std::string val;
};
inline Val<LogArgType::kInt, int> MakeVal(int x) {
return {x};
}
inline Val<LogArgType::kLong, long> MakeVal(long x) {
return {x};
}
inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
return {x};
}
inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
return {x};
}
inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
return {x};
}
inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
unsigned long long x) {
return {x};
}
inline Val<LogArgType::kDouble, double> MakeVal(double x) {
return {x};
}
inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
return {x};
}
inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
return {x};
}
inline Val<LogArgType::kStdString, const std::string*> MakeVal(
const std::string& x) {
return {&x};
}
inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
return {x};
}
inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
const LogMetadata& x) {
return {x};
}
inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
const LogMetadataErr& x) {
return {x};
}
// The enum class types are not implicitly convertible to arithmetic types.
template <
typename T,
std::enable_if_t<std::is_enum_v<T> && !std::is_arithmetic_v<T>>* = nullptr>
inline decltype(MakeVal(std::declval<std::underlying_type_t<T>>())) MakeVal(
T x) {
return {static_cast<std::underlying_type_t<T>>(x)};
}
template <typename T, class = std::void_t<>>
struct has_to_log_string : std::false_type {};
template <typename T>
struct has_to_log_string<T,
std::void_t<decltype(ToLogString(std::declval<T>()))>>
: std::true_type {};
template <
typename T,
typename T1 = std::decay_t<T>,
typename = std::enable_if_t<
std::is_class_v<T1> && !std::is_same_v<T1, std::string> &&
!std::is_same_v<T1, LogMetadata> && !has_to_log_string<T1>::value>>
ToStringVal MakeVal(const T& x) {
std::ostringstream os;
os << x;
return {os.str()};
}
template <typename T, typename = std::enable_if_t<has_to_log_string<T>::value>>
ToStringVal MakeVal(const T& x) {
return {ToLogString(x)};
}
void Log(const LogArgType* fmt, ...);
template <typename... Ts>
class LogStreamer;
template <>
class LogStreamer<> final {
public:
template <
typename U,
typename V = decltype(MakeVal(std::declval<U>())),
std::enable_if_t<std::is_arithmetic_v<U> || std::is_enum_v<U>>* = nullptr>
inline LogStreamer<V> operator<<(U arg) const {
return LogStreamer<V>(MakeVal(arg), this);
}
template <typename U,
typename V = decltype(MakeVal(std::declval<U>())),
std::enable_if_t<!std::is_arithmetic_v<U> && !std::is_enum_v<U>>* =
nullptr>
inline LogStreamer<V> operator<<(const U& arg) const {
return LogStreamer<V>(MakeVal(arg), this);
}
template <typename... Us>
inline static void Call(const Us&... args) {
// NOLINTBEGIN(modernize-avoid-c-arrays)
static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
// NOLINTEND(modernize-avoid-c-arrays)
Log(t, args.GetVal()...);
}
};
template <typename T, typename... Ts>
class LogStreamer<T, Ts...> final {
public:
inline LogStreamer(T arg, const LogStreamer<Ts...>* prior)
: arg_(arg), prior_(prior) {}
template <
typename U,
typename V = decltype(MakeVal(std::declval<U>())),
std::enable_if_t<std::is_arithmetic_v<U> || std::is_enum_v<U>>* = nullptr>
inline LogStreamer<V, T, Ts...> operator<<(U arg) const {
return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
}
template <typename U,
typename V = decltype(MakeVal(std::declval<U>())),
std::enable_if_t<!std::is_arithmetic_v<U> && !std::is_enum_v<U>>* =
nullptr>
inline LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
}
template <typename... Us>
inline void Call(const Us&... args) const {
prior_->Call(arg_, args...);
}
private:
T arg_;
const LogStreamer<Ts...>* prior_;
};
class Logger final {
public:
template <typename... Ts>
inline bool operator&(const LogStreamer<Ts...>& streamer) {
streamer.Call();
return true;
}
};
// This class is used to explicitly ignore values in the conditional
// logging macros. This avoids compiler warnings like "value computed
// is not used" and "statement has no effect".
class LogMessageVoidify {
public:
LogMessageVoidify() = default;
// This has to be an operator with a precedence lower than << but
// higher than ?:
template <typename... Ts>
void operator&(LogStreamer<Ts...>&& streamer) {}
};
} /* namespace logging_impl */
class LogMessage {
public:
template <LogSeverity S>
inline LogMessage(const char* file,
int line,
std::integral_constant<LogSeverity, S>)
: LogMessage(file, line, S) {}
#if AVE_LOG_ENABLED()
LogMessage(const char* file, int line, LogSeverity sev);
LogMessage(const char* file,
int line,
LogSeverity sev,
LogErrorContext err_ctx,
int err);
~LogMessage();
void AddTag(const char* tag);
std::stringstream& stream();
static int64_t LogStartTime();
static uint32_t WallClockStartTime();
static void LogThreads(bool on = true);
static void LogTimestamps(bool on = true);
static void LogToDebug(LogSeverity min_sev);
static LogSeverity GetLogToDebug();
static void SetLogToStderr(bool log_to_stderr);
static void AddLogToStream(LogSink* stream, LogSeverity min_sev);
static void RemoveLogToStream(LogSink* stream);
static int GetLogToStream(LogSink* stream = nullptr);
static int GetMinLogSeverity();
static bool IsNoop(LogSeverity severity);
template <LogSeverity S>
inline static bool IsNoop() {
return IsNoop(S);
}
#else
LogMessage(const char* file, int line, LogSeverity sev) {}
LogMessage(const char* file,
int line,
LogSeverity sev,
LogErrorContext err_ctx,
int err) {}
~LogMessage() = default;
inline void AddTag(const char* tag) {}
inline std::stringstream& stream() { return print_stream_; }
inline static int64_t LogStartTime() { return 0; }
inline static uint32_t WallClockStartTime() { return 0; }
inline static void LogThreads(bool on = true) {}
inline static void LogTimestamps(bool on = true) {}
inline static void LogToDebug(LogSeverity min_sev) {}
inline static LogSeverity GetLogToDebug() { return LS_INFO; }
inline static void SetLogToStderr(bool log_to_stderr) {}
inline static void AddLogToStream(LogSink* stream, LogSeverity min_sev) {}
inline static void RemoveLogToStream(LogSink* stream) {}
inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
inline static int GetMinLogSeverity() { return 0; }
static constexpr bool IsNoop(LogSeverity severity) { return true; }
template <LogSeverity S>
static constexpr bool IsNoop() {
return IsNoop(S);
}
#endif
private:
#if AVE_LOG_ENABLED()
static void UpdateMinLogSeverity();
static void OutputToDebug(const std::string& msg, LogSeverity severity);
void FinishPrintStream();
LogSeverity severity_;
std::string extra_;
static LogSink* streams_;
static std::atomic<bool> streams_empty_;
static bool thread_, timestamp_;
static bool log_to_stderr_;
#else
inline static void UpdateMinLogSeverity() {}
inline static void OutputToDebug(const std::string& msg,
LogSeverity severity) {}
inline void FinishPrintStream() {}
#endif
std::stringstream print_stream_;
AVE_DISALLOW_COPY_AND_ASSIGN(LogMessage);
};
#define AVE_LOG_FILE_LINE(sev, file, line) \
::ave::base::logging_impl::Logger() & \
::ave::base::logging_impl::LogStreamer<>() \
<< ::ave::base::logging_impl::LogMetadata(file, line, sev)
#define AVE_LOG(sev) \
!ave::base::LogMessage::IsNoop<sev>() && \
AVE_LOG_FILE_LINE(sev, __FILE__, __LINE__)
#if AVE_DLOG_IS_ON
#define AVE_DLOG(sev) AVE_LOG(sev)
#define AVE_DLOG_IF(sev, condition) AVE_LOG_IF(sev, condition)
#define AVE_DLOG_V(sev) AVE_LOG_V(sev)
#define AVE_DLOG_F(sev) AVE_LOG_F(sev)
#define AVE_DLOG_IF_F(sev, condition) AVE_LOG_IF_F(sev, condition)
#else
#define AVE_DLOG_EAT_STREAM_PARAMS() \
while (false) \
::ave::base::logging_impl::LogMessageVoidify() & \
(::ave::base::logging_impl::LogStreamer<>())
#define AVE_DLOG(sev) AVE_DLOG_EAT_STREAM_PARAMS()
#define AVE_DLOG_IF(sev, condition) AVE_DLOG_EAT_STREAM_PARAMS()
#define AVE_DLOG_V(sev) AVE_DLOG_EAT_STREAM_PARAMS()
#define AVE_DLOG_F(sev) AVE_DLOG_EAT_STREAM_PARAMS()
#define AVE_DLOG_IF_F(sev, condition) AVE_DLOG_EAT_STREAM_PARAMS()
#endif
} // namespace base
// LS_VERBOSE, LS_DEBUG, LS_INFO, LS_WARNING, LS_ERROR
using base::LS_DEBUG;
using base::LS_ERROR;
using base::LS_INFO;
using base::LS_VERBOSE;
using base::LS_WARNING;
} // namespace ave
using ave::base::LS_DEBUG;
using ave::base::LS_ERROR;
using ave::base::LS_INFO;
using ave::base::LS_VERBOSE;
using ave::base::LS_WARNING;
#endif /* !LOGGING_H */