-
Notifications
You must be signed in to change notification settings - Fork 41
/
Log.h
552 lines (495 loc) · 16.7 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* Copyright (C) 2019-2021 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
#pragma once
#include <olp/core/logging/Format.h>
#include <olp/core/logging/Level.h>
#include <olp/core/CoreApi.h>
#include <olp/core/utils/WarningWorkarounds.h>
#include <boost/none.hpp>
#include <boost/optional.hpp>
#include <cinttypes>
#include <cstdlib>
#include <sstream>
#include <string>
/**
* @file
* @brief Provides the main interface for the logging library.
*/
/**
* @brief Gets the current function signature for different compilers.
*/
#if __GNUC__ >= 3 || defined(__clang__)
#define OLP_SDK_LOG_FUNCTION_SIGNATURE __PRETTY_FUNCTION__
#elif defined(_MSC_VER)
#define OLP_SDK_LOG_FUNCTION_SIGNATURE __FUNCSIG__
#else
#define OLP_SDK_LOG_FUNCTION_SIGNATURE __FUNCTION__
#endif
/**
* @brief Logs a message using C++ style streams.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param level The log level.
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_DO_LOG(level, tag, message) \
do { \
std::stringstream __strm; \
__strm << message; \
::olp::logging::Log::logMessage(level, tag, __strm.str(), __FILE__, \
__LINE__, __FUNCTION__, \
OLP_SDK_LOG_FUNCTION_SIGNATURE); \
} \
OLP_SDK_CORE_LOOP_ONCE()
/**
* @brief Logs a "Critical" message using C++ style streams.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param level The log level.
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_CRITICAL(level, tag, message) \
OLP_SDK_DO_LOG(level, tag, message)
/**
* @brief Logs a "Critical info" message using C++ style streams.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_CRITICAL_INFO(tag, message) \
OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Info, tag, message)
/**
* @brief Logs a "Critical warning" message using C++ style streams.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_CRITICAL_WARNING(tag, message) \
OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Warning, tag, message)
/**
* @brief Logs a "Critical error" message using C++ style streams.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_CRITICAL_ERROR(tag, message) \
OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Error, tag, message)
/**
* @brief Logs a "Fatal error" message using C++ style streams.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_FATAL(tag, message) \
OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Fatal, tag, message)
/**
* @brief Logs a "Critical fatal error" message using C++ style streams,
* and then aborts the program.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_ABORT(tag, message) \
do { \
OLP_SDK_LOG_FATAL(tag, message); \
std::abort(); \
} \
OLP_SDK_CORE_LOOP_ONCE()
/**
* @brief Logs a message using the printf-style formatting.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param level The log level.
* @param tag The tag for the log component.
*/
#define OLP_SDK_DO_LOG_F(level, tag, ...) \
do { \
std::string __message = ::olp::logging::format(__VA_ARGS__); \
::olp::logging::Log::logMessage(level, tag, __message, __FILE__, __LINE__, \
__FUNCTION__, \
OLP_SDK_LOG_FUNCTION_SIGNATURE); \
} \
OLP_SDK_CORE_LOOP_ONCE()
/**
* @brief Logs a "Critical" message using the printf-style formatting.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param level The log level.
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_CRITICAL_F(level, tag, ...) \
OLP_SDK_DO_LOG_F(level, tag, __VA_ARGS__)
/**
* @brief Logs a "Critical info" message using the printf-style formatting.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_CRITICAL_INFO_F(tag, ...) \
OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Info, tag, __VA_ARGS__)
/**
* @brief Logs a "Critical warning" message using the printf-style formatting.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_CRITICAL_WARNING_F(tag, ...) \
OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Warning, tag, __VA_ARGS__)
/**
* @brief Logs a "Critical error" message using the printf-style formatting.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_CRITICAL_ERROR_F(tag, ...) \
OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Error, tag, __VA_ARGS__)
/**
* @brief Logs a "Critical fatal error" message using the printf-style formatting.
*
* `OLP_SDK_LOGGING_DISABLED` does not disable this functionality.
* Additionally, it does not check to see if the tag is disabled.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_FATAL_F(tag, ...) \
OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Fatal, tag, __VA_ARGS__)
/**
* @brief Logs a "Critical fatal error" message using the printf-style formatting,
* and then abort sthe program.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_ABORT_F(tag, ...) \
do { \
OLP_SDK_LOG_FATAL_F(tag, __VA_ARGS__); \
std::abort(); \
} \
OLP_SDK_CORE_LOOP_ONCE()
#ifdef OLP_SDK_LOGGING_DISABLED
#define OLP_SDK_LOG(level, tag, message) \
do { \
} \
OLP_SDK_CORE_LOOP_ONCE()
#else
/**
* @brief Logs a message using C++ style streams.
*
* @param level The log level.
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG(level, tag, message) \
do { \
if (::olp::logging::Log::isEnabled(level, tag)) { \
OLP_SDK_DO_LOG(level, tag, message); \
} \
} \
OLP_SDK_CORE_LOOP_ONCE()
#endif // OLP_SDK_LOGGING_DISABLED
#ifdef LOGGING_DISABLE_DEBUG_LEVEL
#define OLP_SDK_LOG_TRACE(tag, message) \
do { \
::olp::logging::NullLogStream __strm; \
__strm << message; \
OLP_SDK_CORE_UNUSED(tag, __strm); \
} \
OLP_SDK_CORE_LOOP_ONCE()
#define OLP_SDK_LOG_DEBUG(tag, message) \
do { \
::olp::logging::NullLogStream __strm; \
__strm << message; \
OLP_SDK_CORE_UNUSED(tag, __strm); \
} \
OLP_SDK_CORE_LOOP_ONCE()
#else
/**
* @brief Logs a "Trace" message using C++ style streams.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_TRACE(tag, message) \
OLP_SDK_LOG(::olp::logging::Level::Trace, tag, message)
/**
* @brief Logs a "Debug" message using C++ style streams.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_DEBUG(tag, message) \
OLP_SDK_LOG(::olp::logging::Level::Debug, tag, message)
#endif // LOGGING_DISABLE_DEBUG_LEVEL
/**
* @brief Logs an "Info" message using C++ style streams.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_INFO(tag, message) \
OLP_SDK_LOG(::olp::logging::Level::Info, tag, message)
/**
* @brief Logs a "Warning" message using C++ style streams.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_WARNING(tag, message) \
OLP_SDK_LOG(::olp::logging::Level::Warning, tag, message)
/**
* @brief Logs an "Error" message using C++ style streams.
*
* @param tag The tag for the log component.
* @param message The log message.
*/
#define OLP_SDK_LOG_ERROR(tag, message) \
OLP_SDK_LOG(::olp::logging::Level::Error, tag, message)
#ifdef OLP_SDK_LOGGING_DISABLED
#define OLP_SDK_LOG_F(level, tag, ...) \
do { \
} \
OLP_SDK_CORE_LOOP_ONCE()
#else
/**
* @brief Logs a message using the printf style formatting.
*
* @param level The log level.
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_F(level, tag, ...) \
do { \
if (::olp::logging::Log::isEnabled(level, tag)) { \
OLP_SDK_DO_LOG_F(level, tag, __VA_ARGS__); \
} \
} \
OLP_SDK_CORE_LOOP_ONCE()
#endif // OLP_SDK_LOGGING_DISABLED
#ifdef LOGGING_DISABLE_DEBUG_LEVEL
#define OLP_SDK_LOG_TRACE_F(tag, ...) OLP_SDK_CORE_UNUSED(tag, __VA_ARGS__)
#define OLP_SDK_LOG_DEBUG_F(tag, ...) OLP_SDK_CORE_UNUSED(tag, __VA_ARGS__)
#else
/**
* @brief Logs a "Trace" message using the printf style formatting.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_TRACE_F(tag, ...) \
OLP_SDK_LOG_F(::olp::logging::Level::Trace, tag, __VA_ARGS__)
/**
* @brief Logs a "Debug" message using the printf style formatting.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_DEBUG_F(tag, ...) \
OLP_SDK_LOG_F(::olp::logging::Level::Debug, tag, __VA_ARGS__)
#endif // LOGGING_DISABLE_DEBUG_LEVEL
/**
* @brief Logs an "Info" message using the printf style formatting.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_INFO_F(tag, ...) \
OLP_SDK_LOG_F(::olp::logging::Level::Info, tag, __VA_ARGS__)
/**
* @brief Logs a "Warning" message using the printf style formatting.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_WARNING_F(tag, ...) \
OLP_SDK_LOG_F(::olp::logging::Level::Warning, tag, __VA_ARGS__)
/**
* @brief Logs an "Eror" message using the printf style formatting.
*
* @param tag The tag for the log component.
*/
#define OLP_SDK_LOG_ERROR_F(tag, ...) \
OLP_SDK_LOG_F(::olp::logging::Level::Error, tag, __VA_ARGS__)
/**
* @brief A namespace for the logging library.
*/
namespace olp {
namespace logging {
class Configuration;
class FilterGroup;
/**
* @brief Used for disabled logs at compile time.
*/
class NullLogStream {
public:
template <typename T>
/**
* @brief The stream operator to print or serialize the given log stream.
*/
NullLogStream& operator<<(const T&) {
return *this;
}
};
/**
* @brief A primary interface for log messages.
*/
class CORE_API Log {
public:
/**
* @brief Configures the log system.
*
* It overrides the previous configuration.
*
* @return False if the configuration is invalid.
* The configuration does not change in this case.
*/
static bool configure(Configuration configuration);
/**
* @brief Gets a copy of the current configuration.
*
* Use it to add an appender and reconfigure the system.
*
* @return A copy of the current configuration.
*/
static Configuration getConfiguration();
/**
* @brief Sets the default log level.
*
* No messages below this level are displayed unless overridden by
* specific log tags.
*
* @param level The log level.
*/
static void setLevel(Level level);
/**
* @brief Gets the default log level.
*
* @return The log level.
*/
static Level getLevel();
/**
* @brief Sets the log level for a tag.
*
* It overrides the default configurations.
*
* @param level The log level.
* @param tag The tag for the log component. If empty, it sets the
* default level.
*/
static void setLevel(Level level, const std::string& tag);
/**
* @brief Sets the log level for a tag.
*
* It overrides the default configurations.
*
* @param level The log level.
* @param tag The tag for the log component. If empty, it sets the
* default level.
*/
static void setLevel(const std::string& level, const std::string& tag);
/**
* @brief Gets the log level for a tag.
*
* @param tag The tag for the log component. If empty, it sets the
* default level.
*
* @return The log level for the tag or `core::None` if the log level is unset.
*/
static boost::optional<Level> getLevel(const std::string& tag);
/**
* @brief Clears the log level for a tag and sets it to
* the default value.
*
* @param tag The tag for the log component.
*/
static void clearLevel(const std::string& tag);
/**
* @brief Clears the log levels for all tags and sets them to
* the default value.
*/
static void clearLevels();
/**
* @brief Applies a filter group.
*
* It clears all the log levels for tags and replaces them
* with the levels set in the filter group. If the default log level is set in
* the filter group, it is also applied.
*
* @param filters The filter group to apply.
*/
static void applyFilterGroup(const FilterGroup& filters);
/**
* @brief Checks whether a level is enabled by default.
*
* @param level The log level.
*
* @return True if the log is enabled; false otherwise.
*/
static bool isEnabled(Level level);
/**
* @brief Checks whether a log tag is enabled for a level.
*
* @param tag The tag for the log component.
* @param level The log level.
*
* @return True if the log is enabled; false otherwise.
*/
static bool isEnabled(Level level, const std::string& tag);
/**
* @brief Logs a message to the registered appenders.
*
* Outputting to the appender depends on whether
* the appender is enabled for this level.
*
* @param level The log level.
* @param tag The tag for the log component.
* @param message The log message.
* @param file The file that generated the message.
* @param line The line in the file where the message was logged.
* @param function The function that generated the message.
* @param fullFunction The fully qualified function that generated the message.
*/
static void logMessage(Level level, const std::string& tag,
const std::string& message, const char* file,
unsigned int line, const char* function,
const char* fullFunction);
};
} // namespace logging
} // namespace olp