970ff9b77d85a8782e147b0a6884422995e9d981
[babeltrace.git] / src / cpp-common / bt2c / logging.hpp
1 /*
2 * SPDX-FileCopyrightText: 2023 Philippe Proulx <pproulx@efficios.com>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP
7 #define BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP
8
9 #include <cstring>
10 #include <iterator>
11 #include <string>
12 #include <utility>
13 #include <vector>
14
15 #include <glib.h>
16
17 #include <babeltrace2/babeltrace.h>
18
19 #include "common/assert.h"
20 #include "cpp-common/bt2/private-query-executor.hpp"
21 #include "cpp-common/bt2/self-component-class.hpp"
22 #include "cpp-common/bt2/self-component-port.hpp"
23 #include "cpp-common/bt2/self-message-iterator.hpp"
24 #include "cpp-common/bt2s/optional.hpp"
25 #include "cpp-common/bt2s/span.hpp"
26 #include "cpp-common/vendor/fmt/core.h"
27 #include "cpp-common/vendor/wise-enum/wise_enum.h"
28 #include "logging/log-api.h"
29
30 namespace bt2c {
31
32 /*
33 * A logger contains an actor (self component class, self component,
34 * self message iterator, or simple module name), a current logging
35 * level, and a logging tag.
36 *
37 * It offers the log(), logMem(), logErrno(), logErrorAndThrow(),
38 * logErrorAndRethrow(), logErrorErrnoAndThrow(), and
39 * logErrorErrnoAndRethrow() method templates to log using a given
40 * level, optionally append a cause to the error of the current thread
41 * using the correct actor, and optionally throw or rethrow.
42 *
43 * The methods above expect a format string and zero or more arguments
44 * to be formatted with fmt::format().
45 */
46 class Logger final
47 {
48 public:
49 using MemData = bt2s::span<const std::uint8_t>;
50
51 /* clang-format off */
52
53 /* Available log levels */
54 WISE_ENUM_CLASS_MEMBER(Level,
55 (Trace, BT_LOG_TRACE),
56 (Debug, BT_LOG_DEBUG),
57 (Info, BT_LOG_INFO),
58 (Warning, BT_LOG_WARNING),
59 (Error, BT_LOG_ERROR),
60 (Fatal, BT_LOG_FATAL),
61 (None, BT_LOG_NONE)
62 )
63
64 /* clang-format on */
65
66 /*
67 * Builds a logger from the self component class `selfCompCls` using
68 * the tag `tag` and the logging level of `privQueryExec`.
69 */
70 explicit Logger(const bt2::SelfComponentClass selfCompCls,
71 const bt2::PrivateQueryExecutor privQueryExec, std::string tag) noexcept :
72 _mSelfCompCls {selfCompCls},
73 _mLevel {static_cast<Level>(privQueryExec.loggingLevel())}, _mTag {std::move(tag)}
74 {
75 }
76
77 /*
78 * Builds a logger from the self component `selfComp` using the tag
79 * `tag`.
80 */
81 explicit Logger(const bt2::SelfComponent selfComp, std::string tag) noexcept :
82 _mSelfComp {selfComp}, _mLevel {static_cast<Level>(selfComp.loggingLevel())}, _mTag {
83 std::move(
84 tag)}
85 {
86 }
87
88 /*
89 * Builds a logger from the self source component `selfComp` using
90 * the tag `tag`.
91 */
92 explicit Logger(const bt2::SelfSourceComponent selfComp, std::string tag) noexcept :
93 Logger {
94 bt2::SelfComponent {bt_self_component_source_as_self_component(selfComp.libObjPtr())},
95 std::move(tag)}
96 {
97 }
98
99 /*
100 * Builds a logger from the self filter component `selfComp` using
101 * the tag `tag`.
102 */
103 explicit Logger(const bt2::SelfFilterComponent selfComp, std::string tag) noexcept :
104 Logger {
105 bt2::SelfComponent {bt_self_component_filter_as_self_component(selfComp.libObjPtr())},
106 std::move(tag)}
107 {
108 }
109
110 /*
111 * Builds a logger from the self sink component `selfComp` using the
112 * tag `tag`.
113 */
114 explicit Logger(const bt2::SelfSinkComponent selfComp, std::string tag) noexcept :
115 Logger {bt2::SelfComponent {bt_self_component_sink_as_self_component(selfComp.libObjPtr())},
116 std::move(tag)}
117 {
118 }
119
120 /*
121 * Builds a logger from the self message iterator `selfMsgIter`
122 * using the tag `tag`.
123 */
124 explicit Logger(const bt2::SelfMessageIterator selfMsgIter, std::string tag) noexcept :
125 _mSelfMsgIter {selfMsgIter},
126 _mLevel {static_cast<Level>(selfMsgIter.component().loggingLevel())}, _mTag {std::move(tag)}
127 {
128 }
129
130 /*
131 * Builds a logger from the module named `moduleName` using the tag
132 * `tag` and logging level `logLevel`.
133 */
134 explicit Logger(std::string moduleName, std::string tag, const Level logLevel) noexcept :
135 _mModuleName {std::move(moduleName)}, _mLevel {logLevel}, _mTag {std::move(tag)}
136 {
137 }
138
139 /*
140 * Builds a logger from another logger `other` using the new tag
141 * `newTag`.
142 */
143 explicit Logger(const Logger& other, std::string newTag) :
144 _mSelfCompCls {other._mSelfCompCls}, _mSelfComp {other._mSelfComp},
145 _mSelfMsgIter {other._mSelfMsgIter},
146 _mModuleName {other._mModuleName}, _mLevel {other._mLevel}, _mTag {std::move(newTag)}
147 {
148 }
149
150 /*
151 * Current logging level.
152 */
153 Level level() const noexcept
154 {
155 return _mLevel;
156 }
157
158 /*
159 * Current logging level converted to a `bt_log_level` value.
160 *
161 * For legacy code.
162 */
163 bt_log_level cLevel() const noexcept
164 {
165 return static_cast<bt_log_level>(_mLevel);
166 }
167
168 /*
169 * Whether or not this logger would log at the level `level`.
170 */
171 bool wouldLog(const Level level) const noexcept
172 {
173 return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel));
174 }
175
176 /*
177 * Whether or not this logger would log at the trace level.
178 */
179 bool wouldLogT() const noexcept
180 {
181 return this->wouldLog(Level::Trace);
182 }
183
184 /*
185 * Whether or not this logger would log at the debug level.
186 */
187 bool wouldLogD() const noexcept
188 {
189 return this->wouldLog(Level::Debug);
190 }
191
192 /*
193 * Whether or not this logger would log at the info level.
194 */
195 bool wouldLogI() const noexcept
196 {
197 return this->wouldLog(Level::Info);
198 }
199
200 /*
201 * Whether or not this logger would log at the warning level.
202 */
203 bool wouldLogW() const noexcept
204 {
205 return this->wouldLog(Level::Warning);
206 }
207
208 /*
209 * Whether or not this logger would log at the error level.
210 */
211 bool wouldLogE() const noexcept
212 {
213 return this->wouldLog(Level::Error);
214 }
215
216 /*
217 * Whether or not this logger would log at the fatal level.
218 */
219 bool wouldLogF() const noexcept
220 {
221 return this->wouldLog(Level::Fatal);
222 }
223
224 /*
225 * Logging tag.
226 */
227 const std::string& tag() const noexcept
228 {
229 return _mTag;
230 }
231
232 /*
233 * Self component class actor, or `bt2s::nullopt` if none.
234 */
235 const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept
236 {
237 return _mSelfCompCls;
238 }
239
240 /*
241 * Self component actor, or `bt2s::nullopt` if none.
242 */
243 const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept
244 {
245 return _mSelfComp;
246 }
247
248 /*
249 * Self message iterator actor, or `bt2s::nullopt` if none.
250 */
251 const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept
252 {
253 return _mSelfMsgIter;
254 }
255
256 /*
257 * Name of module actor, or `bt2s::nullopt` if none.
258 */
259 const bt2s::optional<std::string>& moduleName() const noexcept
260 {
261 return _mModuleName;
262 }
263
264 private:
265 struct _StdLogWriter final
266 {
267 static void write(const char * const fileName, const char * const funcName,
268 const unsigned lineNo, const Level level, const char * const tag, MemData,
269 const char * const initMsg, const char * const msg) noexcept
270 {
271 BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0);
272 bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg);
273 }
274 };
275
276 public:
277 /*
278 * Logs using the level `LevelV`.
279 *
280 * This method forwards `fmt` and `args` to fmt::format() to create
281 * the log message.
282 *
283 * If `AppendCauseV` is true, this method also appends a cause to
284 * the error of the current thread using the same message.
285 */
286 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
287 void log(const char * const fileName, const char * const funcName, const unsigned int lineNo,
288 const char * const fmt, ArgTs&&...args) const
289 {
290 this->_log<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, {}, "", fmt,
291 std::forward<ArgTs>(args)...);
292 }
293
294 /*
295 * Like log() with the `Level::Error` level, but also throws a
296 * default-constructed instance of `ExcT`.
297 */
298 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
299 [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName,
300 const unsigned int lineNo, const char * const fmt,
301 ArgTs&&...args) const
302 {
303 this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt,
304 std::forward<ArgTs>(args)...);
305 throw ExcT {};
306 }
307
308 /*
309 * Like log() with the `Level::Error` level, but also rethrows.
310 */
311 template <bool AppendCauseV, typename... ArgTs>
312 [[noreturn]] void logErrorAndRethrow(const char * const fileName, const char * const funcName,
313 const unsigned int lineNo, const char * const fmt,
314 ArgTs&&...args) const
315 {
316 this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt,
317 std::forward<ArgTs>(args)...);
318 throw;
319 }
320
321 private:
322 struct _InitMsgLogWriter final
323 {
324 static void write(const char * const fileName, const char * const funcName,
325 const unsigned lineNo, const Level level, const char * const tag, MemData,
326 const char * const initMsg, const char * const msg) noexcept
327 {
328 bt_log_write_printf(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
329 "%s%s", initMsg, msg);
330 }
331 };
332
333 public:
334 /*
335 * Logs the message of `errno` using the level `LevelV`.
336 *
337 * The log message starts with `initMsg`, is followed with the
338 * message for `errno`, and then with what fmt::format() creates
339 * given `fmt` and `args`.
340 *
341 * If `AppendCauseV` is true, this method also appends a cause to
342 * the error of the current thread using the same message.
343 */
344 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
345 void logErrno(const char * const fileName, const char * const funcName,
346 const unsigned int lineNo, const char * const initMsg, const char * const fmt,
347 ArgTs&&...args) const
348 {
349 this->_log<_InitMsgLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, {},
350 this->_errnoIntroStr(initMsg).c_str(),
351 fmt, std::forward<ArgTs>(args)...);
352 }
353
354 /*
355 * Like logErrno() with the `Level::Error` level, but also throws a
356 * default-constructed instance of `ExcT`.
357 */
358 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
359 [[noreturn]] void logErrorErrnoAndThrow(const char * const fileName,
360 const char * const funcName, const unsigned int lineNo,
361 const char * const initMsg, const char * const fmt,
362 ArgTs&&...args) const
363 {
364 this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
365 std::forward<ArgTs>(args)...);
366 throw ExcT {};
367 }
368
369 /*
370 * Like logErrno() with the `Level::Error` level, but also rethrows.
371 */
372 template <bool AppendCauseV, typename... ArgTs>
373 [[noreturn]] void logErrorErrnoAndRethrow(const char * const fileName,
374 const char * const funcName,
375 const unsigned int lineNo, const char * const initMsg,
376 const char * const fmt, ArgTs&&...args) const
377 {
378 this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
379 std::forward<ArgTs>(args)...);
380 throw;
381 }
382
383 private:
384 struct _MemLogWriter final
385 {
386 static void write(const char * const fileName, const char * const funcName,
387 const unsigned lineNo, const Level level, const char * const tag,
388 const MemData memData, const char *, const char * const msg) noexcept
389 {
390 bt_log_write_mem(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
391 memData.data(), memData.size(), msg);
392 }
393 };
394
395 public:
396 /*
397 * Logs memory data using the level `LevelV`.
398 *
399 * This method forwards `fmt` and `args` to fmt::format() to create
400 * the log message.
401 */
402 template <Level LevelV, typename... ArgTs>
403 void logMem(const char * const fileName, const char * const funcName, const unsigned int lineNo,
404 const MemData memData, const char * const fmt, ArgTs&&...args) const
405 {
406 this->_log<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, "", fmt,
407 std::forward<ArgTs>(args)...);
408 }
409
410 private:
411 /*
412 * Formats a log message with fmt::format() given `fmt` and `args`,
413 * and then:
414 *
415 * 1. Calls LogWriterT::write() with its arguments to log using the
416 * level `LevelV`.
417 *
418 * 2. If `AppendCauseV` is true, this method also appends a cause to
419 * the error of the current thread using the concatenation of
420 * `initMsg` and `msg` as the message.
421 */
422 template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs>
423 void _log(const char * const fileName, const char * const funcName, const unsigned int lineNo,
424 const MemData memData, const char * const initMsg, const char * const fmt,
425 ArgTs&&...args) const
426 {
427 const auto wouldLog = this->wouldLog(LevelV);
428
429 /* Only format arguments if logging or appending an error cause */
430 if (G_UNLIKELY(wouldLog || AppendCauseV)) {
431 /*
432 * Format arguments to our buffer (fmt::format_to() doesn't
433 * append a null character).
434 */
435 _mBuf.clear();
436 BT_ASSERT(fmt);
437 fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...);
438 _mBuf.push_back('\0');
439 }
440
441 /* Initial message is required */
442 BT_ASSERT(initMsg);
443
444 /* Log if needed */
445 if (wouldLog) {
446 LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, initMsg,
447 _mBuf.data());
448 }
449
450 /* Append an error cause if needed */
451 if (AppendCauseV) {
452 if (_mSelfMsgIter) {
453 bt_current_thread_error_append_cause_from_message_iterator(
454 _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data());
455 } else if (_mSelfComp) {
456 bt_current_thread_error_append_cause_from_component(
457 _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data());
458 } else if (_mSelfCompCls) {
459 bt_current_thread_error_append_cause_from_component_class(
460 _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data());
461 } else {
462 BT_ASSERT(_mModuleName);
463 bt_current_thread_error_append_cause_from_unknown(
464 _mModuleName->data(), fileName, lineNo, "%s%s", initMsg, _mBuf.data());
465 }
466 }
467 }
468
469 static std::string _errnoIntroStr(const char * const initMsg)
470 {
471 BT_ASSERT(errno != 0);
472 return fmt::format("{}: {}", initMsg, g_strerror(errno));
473 }
474
475 /* Exactly one of the following four members has a value */
476 bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls;
477 bt2s::optional<bt2::SelfComponent> _mSelfComp;
478 bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter;
479 bt2s::optional<std::string> _mModuleName;
480
481 /* Current logging level */
482 Level _mLevel;
483
484 /* Logging tag */
485 std::string _mTag;
486
487 /* Formatting buffer */
488 mutable std::vector<char> _mBuf;
489 };
490
491 /*
492 * Returns `s` if it's not `nullptr`, or the `(null)` string otherwise.
493 */
494 inline const char *maybeNull(const char * const s) noexcept
495 {
496 return s ? s : "(null)";
497 }
498
499 } /* namespace bt2c */
500
501 /* Internal: default logger name */
502 #define _BT_CPPLOG_DEF_LOGGER _mLogger
503
504 /*
505 * Calls log() on `_logger` to log using the level `_lvl`.
506 */
507 #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \
508 do { \
509 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
510 (_logger).template log<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), \
511 ##__VA_ARGS__); \
512 } \
513 } while (0)
514
515 /*
516 * BT_CPPLOG_EX() with specific logging levels.
517 */
518 #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \
519 BT_CPPLOG_EX(bt2c::Logger::Level::Trace, (_logger), (_fmt), ##__VA_ARGS__)
520 #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \
521 BT_CPPLOG_EX(bt2c::Logger::Level::Debug, (_logger), (_fmt), ##__VA_ARGS__)
522 #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \
523 BT_CPPLOG_EX(bt2c::Logger::Level::Info, (_logger), (_fmt), ##__VA_ARGS__)
524 #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \
525 BT_CPPLOG_EX(bt2c::Logger::Level::Warning, (_logger), (_fmt), ##__VA_ARGS__)
526 #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \
527 BT_CPPLOG_EX(bt2c::Logger::Level::Error, (_logger), (_fmt), ##__VA_ARGS__)
528 #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \
529 BT_CPPLOG_EX(bt2c::Logger::Level::Fatal, (_logger), (_fmt), ##__VA_ARGS__)
530
531 /*
532 * BT_CPPLOG_EX() with specific logging levels and using the default
533 * logger.
534 */
535 #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
536 #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
537 #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
538 #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
539 #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
540 #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
541
542 /*
543 * Calls logMem() on `_logger` to log using the level `_lvl`.
544 */
545 #define BT_CPPLOG_MEM_EX(_lvl, _logger, _memData, _fmt, ...) \
546 do { \
547 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
548 (_logger).template logMem<(_lvl)>(__FILE__, __func__, __LINE__, (_memData), (_fmt), \
549 ##__VA_ARGS__); \
550 } \
551 } while (0)
552
553 /*
554 * BT_CPPLOG_MEM_EX() with specific logging levels.
555 */
556 #define BT_CPPLOGT_MEM_SPEC(_logger, _memData, _fmt, ...) \
557 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Trace, (_logger), (_memData), (_fmt), ##__VA_ARGS__)
558 #define BT_CPPLOGD_MEM_SPEC(_logger, _memData, _fmt, ...) \
559 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Debug, (_logger), (_memData), (_fmt), ##__VA_ARGS__)
560 #define BT_CPPLOGI_MEM_SPEC(_logger, _memData, _fmt, ...) \
561 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Info, (_logger), (_memData), (_fmt), ##__VA_ARGS__)
562 #define BT_CPPLOGW_MEM_SPEC(_logger, _memData, _fmt, ...) \
563 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Warning, (_logger), (_memData), (_fmt), ##__VA_ARGS__)
564 #define BT_CPPLOGE_MEM_SPEC(_logger, _memData, _fmt, ...) \
565 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Error, (_logger), (_memData), (_fmt), ##__VA_ARGS__)
566 #define BT_CPPLOGF_MEM_SPEC(_logger, _memData, _fmt, ...) \
567 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Fatal, (_logger), (_memData), (_fmt), ##__VA_ARGS__)
568
569 /*
570 * BT_CPPLOG_MEM_EX() with specific logging levels and using the default
571 * logger.
572 */
573 #define BT_CPPLOGT_MEM(_memData, _fmt, ...) \
574 BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__)
575 #define BT_CPPLOGD_MEM(_memData, _fmt, ...) \
576 BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__)
577 #define BT_CPPLOGI_MEM(_memData, _fmt, ...) \
578 BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__)
579 #define BT_CPPLOGW_MEM(_memData, _fmt, ...) \
580 BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__)
581 #define BT_CPPLOGE_MEM(_memData, _fmt, ...) \
582 BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__)
583 #define BT_CPPLOGF_MEM(_memData, _fmt, ...) \
584 BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__)
585
586 /*
587 * Calls logErrno() on `_logger` to log using the level `_lvl` and
588 * initial message `_initMsg`.
589 */
590 #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _initMsg, _fmt, ...) \
591 do { \
592 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
593 (_logger).template logErrno<(_lvl), false>(__FILE__, __func__, __LINE__, (_initMsg), \
594 (_fmt), ##__VA_ARGS__); \
595 } \
596 } while (0)
597
598 /*
599 * BT_CPPLOG_ERRNO_EX() with specific logging levels.
600 */
601 #define BT_CPPLOGT_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \
602 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Trace, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__)
603 #define BT_CPPLOGD_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \
604 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Debug, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__)
605 #define BT_CPPLOGI_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \
606 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Info, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__)
607 #define BT_CPPLOGW_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \
608 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Warning, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__)
609 #define BT_CPPLOGE_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \
610 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Error, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__)
611 #define BT_CPPLOGF_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \
612 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Fatal, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__)
613
614 /*
615 * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the
616 * default logger.
617 */
618 #define BT_CPPLOGT_ERRNO(_initMsg, _fmt, ...) \
619 BT_CPPLOGT_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__)
620 #define BT_CPPLOGD_ERRNO(_initMsg, _fmt, ...) \
621 BT_CPPLOGD_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__)
622 #define BT_CPPLOGI_ERRNO(_initMsg, _fmt, ...) \
623 BT_CPPLOGI_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__)
624 #define BT_CPPLOGW_ERRNO(_initMsg, _fmt, ...) \
625 BT_CPPLOGW_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__)
626 #define BT_CPPLOGE_ERRNO(_initMsg, _fmt, ...) \
627 BT_CPPLOGE_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__)
628 #define BT_CPPLOGF_ERRNO(_initMsg, _fmt, ...) \
629 BT_CPPLOGF_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__)
630
631 /*
632 * Calls log() on `_logger` with the `Error` level to log an error and
633 * append a cause to the error of the current thread.
634 */
635 #define BT_CPPLOGE_APPEND_CAUSE_SPEC(_logger, _fmt, ...) \
636 (_logger).template log<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_fmt), \
637 ##__VA_ARGS__)
638
639 /*
640 * BT_CPPLOGE_APPEND_CAUSE_SPEC() using the default logger.
641 */
642 #define BT_CPPLOGE_APPEND_CAUSE(_fmt, ...) \
643 BT_CPPLOGE_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
644
645 /*
646 * Calls logErrorAndThrow() on `_logger` to log an error, append a cause
647 * to the error of the current thread, and throw an instance of
648 * `_excCls`.
649 */
650 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _excCls, _fmt, ...) \
651 (_logger).template logErrorAndThrow<true, _excCls>(__FILE__, __func__, __LINE__, (_fmt), \
652 ##__VA_ARGS__)
653
654 /*
655 * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger.
656 */
657 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_excCls, _fmt, ...) \
658 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _excCls, (_fmt), ##__VA_ARGS__)
659
660 /*
661 * Calls logErrorAndRethrow() on `_logger` to log an error, append a
662 * cause to the error of the current thread, and throw an instance of
663 * `_excCls`.
664 */
665 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \
666 (_logger).template logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
667
668 /*
669 * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger.
670 */
671 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \
672 BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
673
674 /*
675 * Calls logErrno() on `_logger` with the `Level::Error` level to log an
676 * error and append a cause to the error of the current thread.
677 */
678 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_logger, _initMsg, _fmt, ...) \
679 (_logger).template logErrno<bt2c::Logger::Level::Error, true>( \
680 __FILE__, __func__, __LINE__, (_initMsg), (_fmt), ##__VA_ARGS__)
681
682 /*
683 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC() using the default logger.
684 */
685 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE(_initMsg, _fmt, ...) \
686 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__)
687
688 /*
689 * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a
690 * cause to the error of the current thread, and throw an instance of
691 * `_excCls`.
692 */
693 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _excCls, _initMsg, _fmt, ...) \
694 (_logger).template logErrorErrnoAndThrow<true, _excCls>(__FILE__, __func__, __LINE__, \
695 (_initMsg), (_fmt), ##__VA_ARGS__)
696
697 /*
698 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default
699 * logger.
700 */
701 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_excCls, _initMsg, _fmt, ...) \
702 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _excCls, (_initMsg), \
703 (_fmt), ##__VA_ARGS__)
704
705 /*
706 * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append
707 * a cause to the error of the current thread, and throw an instance of
708 * `_excCls`.
709 */
710 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _initMsg, _fmt, ...) \
711 (_logger).template logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_initMsg), \
712 (_fmt), ##__VA_ARGS__)
713
714 /*
715 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
716 * logger.
717 */
718 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_initMsg, _fmt, ...) \
719 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), \
720 ##__VA_ARGS__)
721
722 #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */
This page took 0.04321 seconds and 3 git commands to generate.