2 * SPDX-FileCopyrightText: 2023 Philippe Proulx <pproulx@efficios.com>
3 * SPDX-License-Identifier: MIT
6 #ifndef BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP
7 #define BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP
17 #include <babeltrace2/babeltrace.h>
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/vendor/fmt/core.h"
26 #include "cpp-common/vendor/wise-enum/wise_enum.h"
27 #include "logging/log-api.h"
32 * A logger contains an actor (self component class, self component,
33 * self message iterator, or simple module name), a current logging
34 * level, and a logging tag.
36 * It offers the log(), logMem(), logErrno(), logErrorAndThrow(),
37 * logErrorAndRethrow(), logErrorErrnoAndThrow(), and
38 * logErrorErrnoAndRethrow() method templates to log using a given
39 * level, optionally append a cause to the error of the current thread
40 * using the correct actor, and optionally throw or rethrow.
42 * The methods above expect a format string and zero or more arguments
43 * to be formatted with fmt::format().
48 /* clang-format off */
50 /* Available log levels */
51 WISE_ENUM_CLASS_MEMBER(Level,
52 (Trace, BT_LOG_TRACE),
53 (Debug, BT_LOG_DEBUG),
55 (Warning, BT_LOG_WARNING),
56 (Error, BT_LOG_ERROR),
57 (Fatal, BT_LOG_FATAL),
64 * Builds a logger from the self component class `selfCompCls` using
65 * the tag `tag` and the logging level of `privQueryExec`.
67 explicit Logger(const bt2::SelfComponentClass selfCompCls,
68 const bt2::PrivateQueryExecutor privQueryExec, std::string tag) noexcept :
69 _mSelfCompCls {selfCompCls},
70 _mLevel {static_cast<Level>(privQueryExec.loggingLevel())}, _mTag {std::move(tag)}
75 * Builds a logger from the self component `selfComp` using the tag
78 explicit Logger(const bt2::SelfComponent selfComp, std::string tag) noexcept :
79 _mSelfComp {selfComp}, _mLevel {static_cast<Level>(selfComp.loggingLevel())}, _mTag {
86 * Builds a logger from the self source component `selfComp` using
89 explicit Logger(const bt2::SelfSourceComponent selfComp, std::string tag) noexcept :
91 bt2::SelfComponent {bt_self_component_source_as_self_component(selfComp.libObjPtr())},
97 * Builds a logger from the self filter component `selfComp` using
100 explicit Logger(const bt2::SelfFilterComponent selfComp, std::string tag) noexcept :
102 bt2::SelfComponent {bt_self_component_filter_as_self_component(selfComp.libObjPtr())},
108 * Builds a logger from the self sink component `selfComp` using the
111 explicit Logger(const bt2::SelfSinkComponent selfComp, std::string tag) noexcept :
112 Logger {bt2::SelfComponent {bt_self_component_sink_as_self_component(selfComp.libObjPtr())},
118 * Builds a logger from the self message iterator `selfMsgIter`
119 * using the tag `tag`.
121 explicit Logger(const bt2::SelfMessageIterator selfMsgIter, std::string tag) noexcept :
122 _mSelfMsgIter {selfMsgIter},
123 _mLevel {static_cast<Level>(selfMsgIter.component().loggingLevel())}, _mTag {std::move(tag)}
128 * Builds a logger from the module named `moduleName` using the tag
129 * `tag` and logging level `logLevel`.
131 explicit Logger(std::string moduleName, std::string tag, const Level logLevel) noexcept :
132 _mModuleName {std::move(moduleName)}, _mLevel {logLevel}, _mTag {std::move(tag)}
137 * Builds a logger from another logger `other` using the new tag
140 explicit Logger(const Logger& other, std::string newTag) :
141 _mSelfCompCls {other._mSelfCompCls}, _mSelfComp {other._mSelfComp},
142 _mSelfMsgIter {other._mSelfMsgIter},
143 _mModuleName {other._mModuleName}, _mLevel {other._mLevel}, _mTag {std::move(newTag)}
148 * Current logging level.
150 Level level() const noexcept
156 * Current logging level converted to a `bt_log_level` value.
160 bt_log_level cLevel() const noexcept
162 return static_cast<bt_log_level>(_mLevel);
166 * Whether or not this logger would log at the level `level`.
168 bool wouldLog(const Level level) const noexcept
170 return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel));
174 * Whether or not this logger would log at the trace level.
176 bool wouldLogT() const noexcept
178 return this->wouldLog(Level::Trace);
182 * Whether or not this logger would log at the debug level.
184 bool wouldLogD() const noexcept
186 return this->wouldLog(Level::Debug);
190 * Whether or not this logger would log at the info level.
192 bool wouldLogI() const noexcept
194 return this->wouldLog(Level::Info);
198 * Whether or not this logger would log at the warning level.
200 bool wouldLogW() const noexcept
202 return this->wouldLog(Level::Warning);
206 * Whether or not this logger would log at the error level.
208 bool wouldLogE() const noexcept
210 return this->wouldLog(Level::Error);
214 * Whether or not this logger would log at the fatal level.
216 bool wouldLogF() const noexcept
218 return this->wouldLog(Level::Fatal);
224 const std::string& tag() const noexcept
230 * Self component class actor, or `bt2s::nullopt` if none.
232 const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept
234 return _mSelfCompCls;
238 * Self component actor, or `bt2s::nullopt` if none.
240 const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept
246 * Self message iterator actor, or `bt2s::nullopt` if none.
248 const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept
250 return _mSelfMsgIter;
254 * Name of module actor, or `bt2s::nullopt` if none.
256 const bt2s::optional<std::string>& moduleName() const noexcept
262 struct _StdLogWriter final
264 static void write(const char * const fileName, const char * const funcName,
265 const unsigned lineNo, const Level level, const char * const tag,
266 const void *, unsigned int, const char * const initMsg,
267 const char * const msg) noexcept
269 BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0);
270 bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg);
276 * Logs using the level `LevelV`.
278 * This method forwards `fmt` and `args` to fmt::format() to create
281 * If `AppendCauseV` is true, this method also appends a cause to
282 * the error of the current thread using the same message.
284 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
285 void log(const char * const fileName, const char * const funcName, const unsigned int lineNo,
286 const char * const fmt, ArgTs&&...args) const
288 this->_log<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0, "",
289 fmt, std::forward<ArgTs>(args)...);
293 * Logs `msg` using the level `LevelV`.
295 * If `AppendCauseV` is true, this method also appends a cause to
296 * the error of the current thread using the same message.
298 template <Level LevelV, bool AppendCauseV>
299 void logStr(const char * const fileName, const char * const funcName, const unsigned int lineNo,
300 const char * const msg) const
302 this->_logStr<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0,
307 * Like log() with the `Level::Error` level, but also throws a
308 * default-constructed instance of `ExcT`.
310 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
311 [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName,
312 const unsigned int lineNo, const char * const fmt,
313 ArgTs&&...args) const
315 this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt,
316 std::forward<ArgTs>(args)...);
321 * Like logStr() with the `Level::Error` level, but also throws a
322 * default-constructed instance of `ExcT`.
324 template <bool AppendCauseV, typename ExcT>
325 [[noreturn]] void logErrorStrAndThrow(const char * const fileName, const char * const funcName,
326 const unsigned int lineNo, const char * const msg) const
328 this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg);
333 * Like log() with the `Level::Error` level, but also rethrows.
335 template <bool AppendCauseV, typename... ArgTs>
336 [[noreturn]] void logErrorAndRethrow(const char * const fileName, const char * const funcName,
337 const unsigned int lineNo, const char * const fmt,
338 ArgTs&&...args) const
340 this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt,
341 std::forward<ArgTs>(args)...);
346 * Like logStr() with the `Level::Error` level, but also rethrows.
348 template <bool AppendCauseV>
349 [[noreturn]] void logErrorStrAndRethrow(const char * const fileName,
350 const char * const funcName, const unsigned int lineNo,
351 const char * const msg) const
353 this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg);
358 struct _InitMsgLogWriter final
360 static void write(const char * const fileName, const char * const funcName,
361 const unsigned lineNo, const Level level, const char * const tag,
362 const void *, unsigned int, const char * const initMsg,
363 const char * const msg) noexcept
365 bt_log_write_printf(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
366 "%s%s", initMsg, msg);
372 * Logs the message of `errno` using the level `LevelV`.
374 * The log message starts with `initMsg`, is followed with the
375 * message for `errno`, and then with what fmt::format() creates
376 * given `fmt` and `args`.
378 * If `AppendCauseV` is true, this method also appends a cause to
379 * the error of the current thread using the same message.
381 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
382 void logErrno(const char * const fileName, const char * const funcName,
383 const unsigned int lineNo, const char * const initMsg, const char * const fmt,
384 ArgTs&&...args) const
386 this->_log<_InitMsgLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0,
387 this->_errnoIntroStr(initMsg).c_str(),
388 fmt, std::forward<ArgTs>(args)...);
392 * Logs the message of `errno` using the level `LevelV`.
394 * The log message starts with `initMsg`, is followed with the
395 * message for `errno`, and then with `msg`.
397 * If `AppendCauseV` is true, this method also appends a cause to
398 * the error of the current thread using the same message.
400 template <Level LevelV, bool AppendCauseV>
401 void logErrnoStr(const char * const fileName, const char * const funcName,
402 const unsigned int lineNo, const char * const initMsg,
403 const char * const msg) const
405 this->_logStr<_InitMsgLogWriter, LevelV, AppendCauseV>(
406 fileName, funcName, lineNo, nullptr, 0, this->_errnoIntroStr(initMsg).c_str(), msg);
410 * Like logErrno() with the `Level::Error` level, but also throws a
411 * default-constructed instance of `ExcT`.
413 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
414 [[noreturn]] void logErrorErrnoAndThrow(const char * const fileName,
415 const char * const funcName, const unsigned int lineNo,
416 const char * const initMsg, const char * const fmt,
417 ArgTs&&...args) const
419 this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
420 std::forward<ArgTs>(args)...);
425 * Like logErrnoStr() with the `Level::Error` level, but also throws
426 * a default-constructed instance of `ExcT`.
428 template <bool AppendCauseV, typename ExcT>
430 logErrorErrnoStrAndThrow(const char * const fileName, const char * const funcName,
431 const unsigned int lineNo, const char * const initMsg,
432 const char * const msg) const
434 this->logErrnoStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, msg);
439 * Like logErrno() with the `Level::Error` level, but also rethrows.
441 template <bool AppendCauseV, typename... ArgTs>
442 [[noreturn]] void logErrorErrnoAndRethrow(const char * const fileName,
443 const char * const funcName,
444 const unsigned int lineNo, const char * const initMsg,
445 const char * const fmt, ArgTs&&...args) const
447 this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
448 std::forward<ArgTs>(args)...);
453 * Like logErrnoStr() with the `Level::Error` level, but also
456 template <bool AppendCauseV>
458 logErrorErrnoStrAndRethrow(const char * const fileName, const char * const funcName,
459 const unsigned int lineNo, const char * const initMsg,
460 const char * const msg) const
462 this->logErrnoStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, msg);
467 struct _MemLogWriter final
469 static void write(const char * const fileName, const char * const funcName,
470 const unsigned lineNo, const Level level, const char * const tag,
471 const void * const memData, const unsigned int memLen, const char *,
472 const char * const msg) noexcept
474 bt_log_write_mem(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
475 memData, memLen, msg);
481 * Logs memory data using the level `LevelV`.
483 * This method forwards `fmt` and `args` to fmt::format() to create
486 template <Level LevelV, typename... ArgTs>
487 void logMem(const char * const fileName, const char * const funcName, const unsigned int lineNo,
488 const void * const memData, const unsigned int memLen, const char * const fmt,
489 ArgTs&&...args) const
491 this->_log<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, "",
492 fmt, std::forward<ArgTs>(args)...);
496 * Logs memory data using the level `LevelV`, starting with the
499 template <Level LevelV>
500 void logMemStr(const char * const fileName, const char * const funcName,
501 const unsigned int lineNo, const void * const memData, const unsigned int memLen,
502 const char * const msg) const
504 this->_logStr<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, "",
510 * Formats a log message with fmt::format() given `fmt` and `args`,
511 * and then forwards everything to _logStr().
513 template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs>
514 void _log(const char * const fileName, const char * const funcName, const unsigned int lineNo,
515 const void * const memData, const std::size_t memLen, const char * const initMsg,
516 const char * const fmt, ArgTs&&...args) const
518 /* Only format arguments if logging or appending an error cause */
519 if (G_UNLIKELY(this->wouldLog(LevelV) || AppendCauseV)) {
521 * Format arguments to our buffer (fmt::format_to() doesn't
522 * append a null character).
525 fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...);
526 _mBuf.push_back('\0');
529 this->_logStr<LogWriterT, LevelV, AppendCauseV>(fileName, funcName, lineNo, memData, memLen,
530 initMsg, _mBuf.data());
534 * Calls LogWriterT::write() with its arguments to log using the
537 * If `AppendCauseV` is true, this method also appends a cause to
538 * the error of the current thread using the concatenation of
539 * `initMsg` and `msg` as the message.
541 template <typename LogWriterT, Level LevelV, bool AppendCauseV>
542 void _logStr(const char * const fileName, const char * const funcName,
543 const unsigned int lineNo, const void * const memData, const std::size_t memLen,
544 const char * const initMsg, const char * const msg) const
546 /* Initial message and main message are required */
551 if (this->wouldLog(LevelV)) {
552 LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, memLen,
556 /* Append an error cause if needed */
559 bt_current_thread_error_append_cause_from_message_iterator(
560 _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
561 } else if (_mSelfComp) {
562 bt_current_thread_error_append_cause_from_component(
563 _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
564 } else if (_mSelfCompCls) {
565 bt_current_thread_error_append_cause_from_component_class(
566 _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
568 BT_ASSERT(_mModuleName);
569 bt_current_thread_error_append_cause_from_unknown(_mModuleName->data(), fileName,
570 lineNo, "%s%s", initMsg, msg);
575 static std::string _errnoIntroStr(const char * const initMsg)
577 BT_ASSERT(errno != 0);
578 return fmt::format("{}: {}", initMsg, g_strerror(errno));
581 /* Exactly one of the following four members has a value */
582 bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls;
583 bt2s::optional<bt2::SelfComponent> _mSelfComp;
584 bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter;
585 bt2s::optional<std::string> _mModuleName;
587 /* Current logging level */
593 /* Formatting buffer */
594 mutable std::vector<char> _mBuf;
598 * Returns `s` if it's not `nullptr`, or the `(null)` string otherwise.
600 inline const char *maybeNull(const char * const s) noexcept
602 return s ? s : "(null)";
605 } /* namespace bt2c */
607 /* Internal: default logger name */
608 #define _BT_CPPLOG_DEF_LOGGER _mLogger
611 * Calls log() on `_logger` to log using the level `_lvl`.
613 #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \
615 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
616 (_logger).log<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__); \
621 * BT_CPPLOG_EX() with specific logging levels.
623 #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \
624 BT_CPPLOG_EX(bt2c::Logger::Level::Trace, (_logger), (_fmt), ##__VA_ARGS__)
625 #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \
626 BT_CPPLOG_EX(bt2c::Logger::Level::Debug, (_logger), (_fmt), ##__VA_ARGS__)
627 #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \
628 BT_CPPLOG_EX(bt2c::Logger::Level::Info, (_logger), (_fmt), ##__VA_ARGS__)
629 #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \
630 BT_CPPLOG_EX(bt2c::Logger::Level::Warning, (_logger), (_fmt), ##__VA_ARGS__)
631 #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \
632 BT_CPPLOG_EX(bt2c::Logger::Level::Error, (_logger), (_fmt), ##__VA_ARGS__)
633 #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \
634 BT_CPPLOG_EX(bt2c::Logger::Level::Fatal, (_logger), (_fmt), ##__VA_ARGS__)
637 * BT_CPPLOG_EX() with specific logging levels and using the default
640 #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
641 #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
642 #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
643 #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
644 #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
645 #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
648 * Calls logStr() on `_logger` to log using the level `_lvl`.
650 #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \
651 (_logger).logStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg))
654 * BT_CPPLOG_STR_EX() with specific logging levels.
656 #define BT_CPPLOGT_STR_SPEC(_logger, _msg) \
657 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_msg))
658 #define BT_CPPLOGD_STR_SPEC(_logger, _msg) \
659 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_msg))
660 #define BT_CPPLOGI_STR_SPEC(_logger, _msg) \
661 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Info, (_logger), (_msg))
662 #define BT_CPPLOGW_STR_SPEC(_logger, _msg) \
663 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_msg))
664 #define BT_CPPLOGE_STR_SPEC(_logger, _msg) \
665 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Error, (_logger), (_msg))
666 #define BT_CPPLOGF_STR_SPEC(_logger, _msg) \
667 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_msg))
670 * BT_CPPLOG_STR_EX() with specific logging levels and using the default
673 #define BT_CPPLOGT_STR(_msg) BT_CPPLOGT_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
674 #define BT_CPPLOGD_STR(_msg) BT_CPPLOGD_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
675 #define BT_CPPLOGI_STR(_msg) BT_CPPLOGI_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
676 #define BT_CPPLOGW_STR(_msg) BT_CPPLOGW_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
677 #define BT_CPPLOGE_STR(_msg) BT_CPPLOGE_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
678 #define BT_CPPLOGF_STR(_msg) BT_CPPLOGF_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
681 * Calls logMem() on `_logger` to log using the level `_lvl`.
683 #define BT_CPPLOG_MEM_EX(_lvl, _logger, _mem_data, _mem_len, _fmt, ...) \
685 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
686 (_logger).logMem<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \
687 (_fmt), ##__VA_ARGS__); \
692 * BT_CPPLOG_MEM_EX() with specific logging levels.
694 #define BT_CPPLOGT_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
695 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Trace, (_logger), (_mem_data), (_mem_len), (_fmt), \
697 #define BT_CPPLOGD_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
698 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Debug, (_logger), (_mem_data), (_mem_len), (_fmt), \
700 #define BT_CPPLOGI_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
701 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Info, (_logger), (_mem_data), (_mem_len), (_fmt), \
703 #define BT_CPPLOGW_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
704 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Warning, (_logger), (_mem_data), (_mem_len), (_fmt), \
706 #define BT_CPPLOGE_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
707 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_fmt), \
709 #define BT_CPPLOGF_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
710 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Fatal, (_logger), (_mem_data), (_mem_len), (_fmt), \
714 * BT_CPPLOG_MEM_EX() with specific logging levels and using the default
717 #define BT_CPPLOGT_MEM(_mem_data, _mem_len, _fmt, ...) \
718 BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
719 #define BT_CPPLOGD_MEM(_mem_data, _mem_len, _fmt, ...) \
720 BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
721 #define BT_CPPLOGI_MEM(_mem_data, _mem_len, _fmt, ...) \
722 BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
723 #define BT_CPPLOGW_MEM(_mem_data, _mem_len, _fmt, ...) \
724 BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
725 #define BT_CPPLOGE_MEM(_mem_data, _mem_len, _fmt, ...) \
726 BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
727 #define BT_CPPLOGF_MEM(_mem_data, _mem_len, _fmt, ...) \
728 BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
731 * Calls logMemStr() on `_logger` to log using the level `_lvl`.
733 #define BT_CPPLOG_MEM_STR_EX(_lvl, _logger, _mem_data, _mem_len, _msg) \
734 (_logger).logMemStr<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), (_msg))
737 * BT_CPPLOG_MEM_STR_EX() with specific logging levels.
739 #define BT_CPPLOGT_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
740 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_mem_data), (_mem_len), (_msg))
741 #define BT_CPPLOGD_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
742 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_mem_data), (_mem_len), (_msg))
743 #define BT_CPPLOGI_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
744 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_mem_data), (_mem_len), (_msg))
745 #define BT_CPPLOGW_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
746 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_mem_data), (_mem_len), (_msg))
747 #define BT_CPPLOGE_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
748 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_msg))
749 #define BT_CPPLOGF_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
750 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_mem_data), (_mem_len), (_msg))
753 * BT_CPPLOG_MEM_STR_EX() with specific logging levels and using the
756 #define BT_CPPLOGT_MEM_STR(_mem_data, _mem_len, _msg) \
757 BT_CPPLOGT_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
758 #define BT_CPPLOGD_MEM_STR(_mem_data, _mem_len, _msg) \
759 BT_CPPLOGD_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
760 #define BT_CPPLOGI_MEM_STR(_mem_data, _mem_len, _msg) \
761 BT_CPPLOGI_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
762 #define BT_CPPLOGW_MEM_STR(_mem_data, _mem_len, _msg) \
763 BT_CPPLOGW_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
764 #define BT_CPPLOGE_MEM_STR(_mem_data, _mem_len, _msg) \
765 BT_CPPLOGE_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
766 #define BT_CPPLOGF_MEM_STR(_mem_data, _mem_len, _msg) \
767 BT_CPPLOGF_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
770 * Calls logErrno() on `_logger` to log using the level `_lvl` and
771 * initial message `_init_msg`.
773 #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _init_msg, _fmt, ...) \
775 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
776 (_logger).logErrno<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
782 * BT_CPPLOG_ERRNO_EX() with specific logging levels.
784 #define BT_CPPLOGT_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
785 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
786 #define BT_CPPLOGD_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
787 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
788 #define BT_CPPLOGI_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
789 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
790 #define BT_CPPLOGW_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
791 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
792 #define BT_CPPLOGE_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
793 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
794 #define BT_CPPLOGF_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
795 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
798 * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the
801 #define BT_CPPLOGT_ERRNO(_init_msg, _fmt, ...) \
802 BT_CPPLOGT_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
803 #define BT_CPPLOGD_ERRNO(_init_msg, _fmt, ...) \
804 BT_CPPLOGD_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
805 #define BT_CPPLOGI_ERRNO(_init_msg, _fmt, ...) \
806 BT_CPPLOGI_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
807 #define BT_CPPLOGW_ERRNO(_init_msg, _fmt, ...) \
808 BT_CPPLOGW_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
809 #define BT_CPPLOGE_ERRNO(_init_msg, _fmt, ...) \
810 BT_CPPLOGE_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
811 #define BT_CPPLOGF_ERRNO(_init_msg, _fmt, ...) \
812 BT_CPPLOGF_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
815 * Calls logErrnoStr() on `_logger` to log using the level `_lvl` and
816 * initial message `_init_msg`.
818 #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _init_msg, _msg) \
819 (_logger).logErrnoStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
822 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels.
824 #define BT_CPPLOGT_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
825 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_msg))
826 #define BT_CPPLOGD_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
827 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_msg))
828 #define BT_CPPLOGI_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
829 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_msg))
830 #define BT_CPPLOGW_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
831 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_msg))
832 #define BT_CPPLOGE_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
833 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_msg))
834 #define BT_CPPLOGF_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
835 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_msg))
838 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels and using the
841 #define BT_CPPLOGT_ERRNO_STR(_init_msg, _msg) \
842 BT_CPPLOGT_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
843 #define BT_CPPLOGD_ERRNO_STR(_init_msg, _msg) \
844 BT_CPPLOGD_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
845 #define BT_CPPLOGI_ERRNO_STR(_init_msg, _msg) \
846 BT_CPPLOGI_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
847 #define BT_CPPLOGW_ERRNO_STR(_init_msg, _msg) \
848 BT_CPPLOGW_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
849 #define BT_CPPLOGE_ERRNO_STR(_init_msg, _msg) \
850 BT_CPPLOGE_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
851 #define BT_CPPLOGF_ERRNO_STR(_init_msg, _msg) \
852 BT_CPPLOGF_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
855 * Calls log() on `_logger` with the `Error` level to log an error and
856 * append a cause to the error of the current thread.
858 #define BT_CPPLOGE_APPEND_CAUSE_SPEC(_logger, _fmt, ...) \
859 (_logger).log<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_fmt), \
863 * BT_CPPLOGE_APPEND_CAUSE_SPEC() using the default logger.
865 #define BT_CPPLOGE_APPEND_CAUSE(_fmt, ...) \
866 BT_CPPLOGE_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
869 * Calls logStr() on `_logger` with the `Error` level to log an error and
870 * append a cause to the error of the current thread.
872 #define BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_logger, _msg) \
873 (_logger).logStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_msg))
876 * BT_CPPLOGE_STR_APPEND_CAUSE_SPEC() using the default logger.
878 #define BT_CPPLOGE_STR_APPEND_CAUSE(_msg) \
879 BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
882 * Calls logErrorAndThrow() on `_logger` to log an error, append a cause
883 * to the error of the current thread, and throw an instance of
886 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \
887 (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
890 * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger.
892 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \
893 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_fmt), ##__VA_ARGS__)
896 * Calls logErrorStrAndThrow() on `_logger` to log an error, append a
897 * cause to the error of the current thread, and throw an instance of
900 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _msg) \
901 (_logger).logErrorStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_msg))
904 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
907 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _msg) \
908 BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_msg))
911 * Calls logErrorAndRethrow() on `_logger` to log an error, append a
912 * cause to the error of the current thread, and throw an instance of
915 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \
916 (_logger).logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
919 * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger.
921 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \
922 BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
925 * Calls logErrorStrAndRethrow() on `_logger` to log an error, append a
926 * cause to the error of the current thread, and throw an instance of
929 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \
930 (_logger).logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg))
933 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
936 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \
937 BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
940 * Calls logErrno() on `_logger` with the `Level::Error` level to log an
941 * error and append a cause to the error of the current thread.
943 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_logger, _init_msg, _fmt, ...) \
944 (_logger).logErrno<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \
945 (_init_msg), (_fmt), ##__VA_ARGS__)
948 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC() using the default logger.
950 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE(_init_msg, _fmt, ...) \
951 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
954 * Calls logErrnoStr() on `_logger` with the `Level::Error` level to log
955 * an error and append a cause to the error of the current thread.
957 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_logger, _init_msg, _msg) \
958 (_logger).logErrnoStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \
962 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC() using the default logger.
964 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE(_init_msg, _msg) \
965 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
968 * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a
969 * cause to the error of the current thread, and throw an instance of
972 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _fmt, ...) \
973 (_logger).logErrorErrnoAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
974 (_fmt), ##__VA_ARGS__)
977 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default
980 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \
981 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
982 (_fmt), ##__VA_ARGS__)
985 * Calls logErrorErrnoStrAndThrow() on `_logger` to log an error, append
986 * a cause to the error of the current thread, and throw an instance of
989 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _msg) \
990 (_logger).logErrorErrnoStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
994 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
997 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _msg) \
998 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
1002 * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append
1003 * a cause to the error of the current thread, and throw an instance of
1006 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _fmt, ...) \
1007 (_logger).logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
1011 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
1014 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_init_msg, _fmt, ...) \
1015 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), \
1019 * Calls logErrorErrnoStrAndRethrow() on `_logger` to log an error,
1020 * append a cause to the error of the current thread, and throw an
1021 * instance of `_exc_cls`.
1023 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _msg) \
1024 (_logger).logErrorErrnoStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
1027 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the
1030 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW(_init_msg, _msg) \
1031 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
1033 #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */