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 "logging/log-api.h"
31 * A logger contains an actor (self component class, self component,
32 * self message iterator, or simple module name), a current logging
33 * level, and a logging tag.
35 * It offers the log(), logMem(), logErrno(), logErrorAndThrow(),
36 * logErrorAndRethrow(), logErrorErrnoAndThrow(), and
37 * logErrorErrnoAndRethrow() method templates to log using a given
38 * level, optionally append a cause to the error of the current thread
39 * using the correct actor, and optionally throw or rethrow.
41 * The methods above expect a format string and zero or more arguments
42 * to be formatted with fmt::format().
47 /* Available log levels */
53 Warning = BT_LOG_WARNING,
60 * Builds a logger from the self component class `selfCompCls` using
61 * the tag `tag` and the logging level of `privQueryExec`.
63 explicit Logger(const bt2::SelfComponentClass selfCompCls,
64 const bt2::PrivateQueryExecutor privQueryExec, std::string tag) noexcept :
65 _mSelfCompCls {selfCompCls},
66 _mLevel {static_cast<Level>(privQueryExec.loggingLevel())}, _mTag {std::move(tag)}
71 * Builds a logger from the self component `selfComp` using the tag
74 explicit Logger(const bt2::SelfComponent selfComp, std::string tag) noexcept :
75 _mSelfComp {selfComp}, _mLevel {static_cast<Level>(selfComp.loggingLevel())}, _mTag {
82 * Builds a logger from the self source component `selfComp` using
85 explicit Logger(const bt2::SelfSourceComponent selfComp, std::string tag) noexcept :
87 bt2::SelfComponent {bt_self_component_source_as_self_component(selfComp.libObjPtr())},
93 * Builds a logger from the self filter component `selfComp` using
96 explicit Logger(const bt2::SelfFilterComponent selfComp, std::string tag) noexcept :
98 bt2::SelfComponent {bt_self_component_filter_as_self_component(selfComp.libObjPtr())},
104 * Builds a logger from the self sink component `selfComp` using the
107 explicit Logger(const bt2::SelfSinkComponent selfComp, std::string tag) noexcept :
108 Logger {bt2::SelfComponent {bt_self_component_sink_as_self_component(selfComp.libObjPtr())},
114 * Builds a logger from the self message iterator `selfMsgIter`
115 * using the tag `tag`.
117 explicit Logger(const bt2::SelfMessageIterator selfMsgIter, std::string tag) noexcept :
118 _mSelfMsgIter {selfMsgIter},
119 _mLevel {static_cast<Level>(selfMsgIter.component().loggingLevel())}, _mTag {std::move(tag)}
124 * Builds a logger from the module named `moduleName` using the tag
125 * `tag` and logging level `logLevel`.
127 explicit Logger(std::string moduleName, std::string tag, const Level logLevel) noexcept :
128 _mModuleName {std::move(moduleName)}, _mLevel {logLevel}, _mTag {std::move(tag)}
133 * Builds a logger from another logger `other` using the new tag
136 explicit Logger(const Logger& other, std::string newTag) :
137 _mSelfCompCls {other._mSelfCompCls}, _mSelfComp {other._mSelfComp},
138 _mSelfMsgIter {other._mSelfMsgIter},
139 _mModuleName {other._mModuleName}, _mLevel {other._mLevel}, _mTag {std::move(newTag)}
144 * Current logging level.
146 Level level() const noexcept
152 * Current logging level converted to a `bt_log_level` value.
156 bt_log_level cLevel() const noexcept
158 return static_cast<bt_log_level>(_mLevel);
162 * Whether or not this logger would log at the level `level`.
164 bool wouldLog(const Level level) const noexcept
166 return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel));
170 * Whether or not this logger would log at the trace level.
172 bool wouldLogT() const noexcept
174 return this->wouldLog(Level::Trace);
178 * Whether or not this logger would log at the debug level.
180 bool wouldLogD() const noexcept
182 return this->wouldLog(Level::Debug);
186 * Whether or not this logger would log at the info level.
188 bool wouldLogI() const noexcept
190 return this->wouldLog(Level::Info);
194 * Whether or not this logger would log at the warning level.
196 bool wouldLogW() const noexcept
198 return this->wouldLog(Level::Warning);
202 * Whether or not this logger would log at the error level.
204 bool wouldLogE() const noexcept
206 return this->wouldLog(Level::Error);
210 * Whether or not this logger would log at the fatal level.
212 bool wouldLogF() const noexcept
214 return this->wouldLog(Level::Fatal);
220 const std::string& tag() const noexcept
226 * Self component class actor, or `bt2s::nullopt` if none.
228 const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept
230 return _mSelfCompCls;
234 * Self component actor, or `bt2s::nullopt` if none.
236 const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept
242 * Self message iterator actor, or `bt2s::nullopt` if none.
244 const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept
246 return _mSelfMsgIter;
250 * Name of module actor, or `bt2s::nullopt` if none.
252 const bt2s::optional<std::string>& moduleName() const noexcept
258 struct _StdLogWriter final
260 static void write(const char * const fileName, const char * const funcName,
261 const unsigned lineNo, const Level level, const char * const tag,
262 const void *, unsigned int, const char * const initMsg,
263 const char * const msg) noexcept
265 BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0);
266 bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg);
272 * Logs using the level `LevelV`.
274 * This method forwards `fmt` and `args` to fmt::format() to create
277 * If `AppendCauseV` is true, this method also appends a cause to
278 * the error of the current thread using the same message.
280 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
281 void log(const char * const fileName, const char * const funcName, const unsigned int lineNo,
282 const char * const fmt, ArgTs&&...args) const
284 this->_log<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0, "",
285 fmt, std::forward<ArgTs>(args)...);
289 * Logs `msg` using the level `LevelV`.
291 * If `AppendCauseV` is true, this method also appends a cause to
292 * the error of the current thread using the same message.
294 template <Level LevelV, bool AppendCauseV>
295 void logStr(const char * const fileName, const char * const funcName, const unsigned int lineNo,
296 const char * const msg) const
298 this->_logStr<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0,
303 * Like log() with the `Level::Error` level, but also throws a
304 * default-constructed instance of `ExcT`.
306 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
307 [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName,
308 const unsigned int lineNo, const char * const fmt,
309 ArgTs&&...args) const
311 this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt,
312 std::forward<ArgTs>(args)...);
317 * Like logStr() with the `Level::Error` level, but also throws a
318 * default-constructed instance of `ExcT`.
320 template <bool AppendCauseV, typename ExcT>
321 [[noreturn]] void logErrorStrAndThrow(const char * const fileName, const char * const funcName,
322 const unsigned int lineNo, const char * const msg) const
324 this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg);
329 * Like log() with the `Level::Error` level, but also rethrows.
331 template <bool AppendCauseV, typename... ArgTs>
332 [[noreturn]] void logErrorAndRethrow(const char * const fileName, const char * const funcName,
333 const unsigned int lineNo, const char * const fmt,
334 ArgTs&&...args) const
336 this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt,
337 std::forward<ArgTs>(args)...);
342 * Like logStr() with the `Level::Error` level, but also rethrows.
344 template <bool AppendCauseV>
345 [[noreturn]] void logErrorStrAndRethrow(const char * const fileName,
346 const char * const funcName, const unsigned int lineNo,
347 const char * const msg) const
349 this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg);
354 struct _InitMsgLogWriter final
356 static void write(const char * const fileName, const char * const funcName,
357 const unsigned lineNo, const Level level, const char * const tag,
358 const void *, unsigned int, const char * const initMsg,
359 const char * const msg) noexcept
361 bt_log_write_printf(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
362 "%s%s", initMsg, msg);
368 * Logs the message of `errno` using the level `LevelV`.
370 * The log message starts with `initMsg`, is followed with the
371 * message for `errno`, and then with what fmt::format() creates
372 * given `fmt` and `args`.
374 * If `AppendCauseV` is true, this method also appends a cause to
375 * the error of the current thread using the same message.
377 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
378 void logErrno(const char * const fileName, const char * const funcName,
379 const unsigned int lineNo, const char * const initMsg, const char * const fmt,
380 ArgTs&&...args) const
382 this->_log<_InitMsgLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0,
383 this->_errnoIntroStr(initMsg).c_str(),
384 fmt, std::forward<ArgTs>(args)...);
388 * Logs the message of `errno` using the level `LevelV`.
390 * The log message starts with `initMsg`, is followed with the
391 * message for `errno`, and then with `msg`.
393 * If `AppendCauseV` is true, this method also appends a cause to
394 * the error of the current thread using the same message.
396 template <Level LevelV, bool AppendCauseV>
397 void logErrnoStr(const char * const fileName, const char * const funcName,
398 const unsigned int lineNo, const char * const initMsg,
399 const char * const msg) const
401 this->_logStr<_InitMsgLogWriter, LevelV, AppendCauseV>(
402 fileName, funcName, lineNo, nullptr, 0, this->_errnoIntroStr(initMsg).c_str(), msg);
406 * Like logErrno() with the `Level::Error` level, but also throws a
407 * default-constructed instance of `ExcT`.
409 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
410 [[noreturn]] void logErrorErrnoAndThrow(const char * const fileName,
411 const char * const funcName, const unsigned int lineNo,
412 const char * const initMsg, const char * const fmt,
413 ArgTs&&...args) const
415 this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
416 std::forward<ArgTs>(args)...);
421 * Like logErrnoStr() with the `Level::Error` level, but also throws
422 * a default-constructed instance of `ExcT`.
424 template <bool AppendCauseV, typename ExcT>
426 logErrorErrnoStrAndThrow(const char * const fileName, const char * const funcName,
427 const unsigned int lineNo, const char * const initMsg,
428 const char * const msg) const
430 this->logErrnoStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, msg);
435 * Like logErrno() with the `Level::Error` level, but also rethrows.
437 template <bool AppendCauseV, typename... ArgTs>
438 [[noreturn]] void logErrorErrnoAndRethrow(const char * const fileName,
439 const char * const funcName,
440 const unsigned int lineNo, const char * const initMsg,
441 const char * const fmt, ArgTs&&...args) const
443 this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
444 std::forward<ArgTs>(args)...);
449 * Like logErrnoStr() with the `Level::Error` level, but also
452 template <bool AppendCauseV>
454 logErrorErrnoStrAndRethrow(const char * const fileName, const char * const funcName,
455 const unsigned int lineNo, const char * const initMsg,
456 const char * const msg) const
458 this->logErrnoStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, msg);
463 struct _MemLogWriter final
465 static void write(const char * const fileName, const char * const funcName,
466 const unsigned lineNo, const Level level, const char * const tag,
467 const void * const memData, const unsigned int memLen, const char *,
468 const char * const msg) noexcept
470 bt_log_write_mem(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
471 memData, memLen, msg);
477 * Logs memory data using the level `LevelV`.
479 * This method forwards `fmt` and `args` to fmt::format() to create
482 template <Level LevelV, typename... ArgTs>
483 void logMem(const char * const fileName, const char * const funcName, const unsigned int lineNo,
484 const void * const memData, const unsigned int memLen, const char * const fmt,
485 ArgTs&&...args) const
487 this->_log<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, "",
488 fmt, std::forward<ArgTs>(args)...);
492 * Logs memory data using the level `LevelV`, starting with the
495 template <Level LevelV>
496 void logMemStr(const char * const fileName, const char * const funcName,
497 const unsigned int lineNo, const void * const memData, const unsigned int memLen,
498 const char * const msg) const
500 this->_logStr<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, "",
506 * Formats a log message with fmt::format() given `fmt` and `args`,
507 * and then forwards everything to _logStr().
509 template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs>
510 void _log(const char * const fileName, const char * const funcName, const unsigned int lineNo,
511 const void * const memData, const std::size_t memLen, const char * const initMsg,
512 const char * const fmt, ArgTs&&...args) const
514 /* Only format arguments if logging or appending an error cause */
515 if (G_UNLIKELY(this->wouldLog(LevelV) || AppendCauseV)) {
517 * Format arguments to our buffer (fmt::format_to() doesn't
518 * append a null character).
521 fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...);
522 _mBuf.push_back('\0');
525 this->_logStr<LogWriterT, LevelV, AppendCauseV>(fileName, funcName, lineNo, memData, memLen,
526 initMsg, _mBuf.data());
530 * Calls LogWriterT::write() with its arguments to log using the
533 * If `AppendCauseV` is true, this method also appends a cause to
534 * the error of the current thread using the concatenation of
535 * `initMsg` and `msg` as the message.
537 template <typename LogWriterT, Level LevelV, bool AppendCauseV>
538 void _logStr(const char * const fileName, const char * const funcName,
539 const unsigned int lineNo, const void * const memData, const std::size_t memLen,
540 const char * const initMsg, const char * const msg) const
542 /* Initial message and main message are required */
547 if (this->wouldLog(LevelV)) {
548 LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, memLen,
552 /* Append an error cause if needed */
555 bt_current_thread_error_append_cause_from_message_iterator(
556 _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
557 } else if (_mSelfComp) {
558 bt_current_thread_error_append_cause_from_component(
559 _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
560 } else if (_mSelfCompCls) {
561 bt_current_thread_error_append_cause_from_component_class(
562 _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
564 BT_ASSERT(_mModuleName);
565 bt_current_thread_error_append_cause_from_unknown(_mModuleName->data(), fileName,
566 lineNo, "%s%s", initMsg, msg);
571 static std::string _errnoIntroStr(const char * const initMsg)
573 BT_ASSERT(errno != 0);
574 return fmt::format("{}: {}", initMsg, g_strerror(errno));
577 /* Exactly one of the following four members has a value */
578 bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls;
579 bt2s::optional<bt2::SelfComponent> _mSelfComp;
580 bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter;
581 bt2s::optional<std::string> _mModuleName;
583 /* Current logging level */
589 /* Formatting buffer */
590 mutable std::vector<char> _mBuf;
593 } /* namespace bt2c */
595 /* Internal: default logger name */
596 #define _BT_CPPLOG_DEF_LOGGER _mLogger
599 * Calls log() on `_logger` to log using the level `_lvl`.
601 #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \
603 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
604 (_logger).log<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__); \
609 * BT_CPPLOG_EX() with specific logging levels.
611 #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \
612 BT_CPPLOG_EX(bt2c::Logger::Level::Trace, (_logger), (_fmt), ##__VA_ARGS__)
613 #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \
614 BT_CPPLOG_EX(bt2c::Logger::Level::Debug, (_logger), (_fmt), ##__VA_ARGS__)
615 #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \
616 BT_CPPLOG_EX(bt2c::Logger::Level::Info, (_logger), (_fmt), ##__VA_ARGS__)
617 #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \
618 BT_CPPLOG_EX(bt2c::Logger::Level::Warning, (_logger), (_fmt), ##__VA_ARGS__)
619 #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \
620 BT_CPPLOG_EX(bt2c::Logger::Level::Error, (_logger), (_fmt), ##__VA_ARGS__)
621 #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \
622 BT_CPPLOG_EX(bt2c::Logger::Level::Fatal, (_logger), (_fmt), ##__VA_ARGS__)
625 * BT_CPPLOG_EX() with specific logging levels and using the default
628 #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
629 #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
630 #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
631 #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
632 #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
633 #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
636 * Calls logStr() on `_logger` to log using the level `_lvl`.
638 #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \
639 (_logger).logStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg))
642 * BT_CPPLOG_STR_EX() with specific logging levels.
644 #define BT_CPPLOGT_STR_SPEC(_logger, _msg) \
645 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_msg))
646 #define BT_CPPLOGD_STR_SPEC(_logger, _msg) \
647 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_msg))
648 #define BT_CPPLOGI_STR_SPEC(_logger, _msg) \
649 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Info, (_logger), (_msg))
650 #define BT_CPPLOGW_STR_SPEC(_logger, _msg) \
651 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_msg))
652 #define BT_CPPLOGE_STR_SPEC(_logger, _msg) \
653 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Error, (_logger), (_msg))
654 #define BT_CPPLOGF_STR_SPEC(_logger, _msg) \
655 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_msg))
658 * BT_CPPLOG_STR_EX() with specific logging levels and using the default
661 #define BT_CPPLOGT_STR(_msg) BT_CPPLOGT_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
662 #define BT_CPPLOGD_STR(_msg) BT_CPPLOGD_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
663 #define BT_CPPLOGI_STR(_msg) BT_CPPLOGI_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
664 #define BT_CPPLOGW_STR(_msg) BT_CPPLOGW_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
665 #define BT_CPPLOGE_STR(_msg) BT_CPPLOGE_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
666 #define BT_CPPLOGF_STR(_msg) BT_CPPLOGF_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
669 * Calls logMem() on `_logger` to log using the level `_lvl`.
671 #define BT_CPPLOG_MEM_EX(_lvl, _logger, _mem_data, _mem_len, _fmt, ...) \
673 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
674 (_logger).logMem<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \
675 (_fmt), ##__VA_ARGS__); \
680 * BT_CPPLOG_MEM_EX() with specific logging levels.
682 #define BT_CPPLOGT_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
683 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Trace, (_logger), (_mem_data), (_mem_len), (_fmt), \
685 #define BT_CPPLOGD_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
686 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Debug, (_logger), (_mem_data), (_mem_len), (_fmt), \
688 #define BT_CPPLOGI_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
689 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Info, (_logger), (_mem_data), (_mem_len), (_fmt), \
691 #define BT_CPPLOGW_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
692 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Warning, (_logger), (_mem_data), (_mem_len), (_fmt), \
694 #define BT_CPPLOGE_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
695 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_fmt), \
697 #define BT_CPPLOGF_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
698 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Fatal, (_logger), (_mem_data), (_mem_len), (_fmt), \
702 * BT_CPPLOG_MEM_EX() with specific logging levels and using the default
705 #define BT_CPPLOGT_MEM(_mem_data, _mem_len, _fmt, ...) \
706 BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
707 #define BT_CPPLOGD_MEM(_mem_data, _mem_len, _fmt, ...) \
708 BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
709 #define BT_CPPLOGI_MEM(_mem_data, _mem_len, _fmt, ...) \
710 BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
711 #define BT_CPPLOGW_MEM(_mem_data, _mem_len, _fmt, ...) \
712 BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
713 #define BT_CPPLOGE_MEM(_mem_data, _mem_len, _fmt, ...) \
714 BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
715 #define BT_CPPLOGF_MEM(_mem_data, _mem_len, _fmt, ...) \
716 BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
719 * Calls logMemStr() on `_logger` to log using the level `_lvl`.
721 #define BT_CPPLOG_MEM_STR_EX(_lvl, _logger, _mem_data, _mem_len, _msg) \
722 (_logger).logMemStr<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), (_msg))
725 * BT_CPPLOG_MEM_STR_EX() with specific logging levels.
727 #define BT_CPPLOGT_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
728 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_mem_data), (_mem_len), (_msg))
729 #define BT_CPPLOGD_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
730 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_mem_data), (_mem_len), (_msg))
731 #define BT_CPPLOGI_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
732 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_mem_data), (_mem_len), (_msg))
733 #define BT_CPPLOGW_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
734 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_mem_data), (_mem_len), (_msg))
735 #define BT_CPPLOGE_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
736 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_msg))
737 #define BT_CPPLOGF_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
738 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_mem_data), (_mem_len), (_msg))
741 * BT_CPPLOG_MEM_STR_EX() with specific logging levels and using the
744 #define BT_CPPLOGT_MEM_STR(_mem_data, _mem_len, _msg) \
745 BT_CPPLOGT_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
746 #define BT_CPPLOGD_MEM_STR(_mem_data, _mem_len, _msg) \
747 BT_CPPLOGD_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
748 #define BT_CPPLOGI_MEM_STR(_mem_data, _mem_len, _msg) \
749 BT_CPPLOGI_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
750 #define BT_CPPLOGW_MEM_STR(_mem_data, _mem_len, _msg) \
751 BT_CPPLOGW_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
752 #define BT_CPPLOGE_MEM_STR(_mem_data, _mem_len, _msg) \
753 BT_CPPLOGE_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
754 #define BT_CPPLOGF_MEM_STR(_mem_data, _mem_len, _msg) \
755 BT_CPPLOGF_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
758 * Calls logErrno() on `_logger` to log using the level `_lvl` and
759 * initial message `_init_msg`.
761 #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _init_msg, _fmt, ...) \
763 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
764 (_logger).logErrno<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
770 * BT_CPPLOG_ERRNO_EX() with specific logging levels.
772 #define BT_CPPLOGT_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
773 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
774 #define BT_CPPLOGD_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
775 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
776 #define BT_CPPLOGI_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
777 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
778 #define BT_CPPLOGW_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
779 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
780 #define BT_CPPLOGE_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
781 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
782 #define BT_CPPLOGF_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
783 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
786 * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the
789 #define BT_CPPLOGT_ERRNO(_init_msg, _fmt, ...) \
790 BT_CPPLOGT_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
791 #define BT_CPPLOGD_ERRNO(_init_msg, _fmt, ...) \
792 BT_CPPLOGD_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
793 #define BT_CPPLOGI_ERRNO(_init_msg, _fmt, ...) \
794 BT_CPPLOGI_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
795 #define BT_CPPLOGW_ERRNO(_init_msg, _fmt, ...) \
796 BT_CPPLOGW_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
797 #define BT_CPPLOGE_ERRNO(_init_msg, _fmt, ...) \
798 BT_CPPLOGE_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
799 #define BT_CPPLOGF_ERRNO(_init_msg, _fmt, ...) \
800 BT_CPPLOGF_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
803 * Calls logErrnoStr() on `_logger` to log using the level `_lvl` and
804 * initial message `_init_msg`.
806 #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _init_msg, _msg) \
807 (_logger).logErrnoStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
810 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels.
812 #define BT_CPPLOGT_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
813 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_msg))
814 #define BT_CPPLOGD_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
815 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_msg))
816 #define BT_CPPLOGI_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
817 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_msg))
818 #define BT_CPPLOGW_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
819 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_msg))
820 #define BT_CPPLOGE_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
821 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_msg))
822 #define BT_CPPLOGF_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
823 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_msg))
826 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels and using the
829 #define BT_CPPLOGT_ERRNO_STR(_init_msg, _msg) \
830 BT_CPPLOGT_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
831 #define BT_CPPLOGD_ERRNO_STR(_init_msg, _msg) \
832 BT_CPPLOGD_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
833 #define BT_CPPLOGI_ERRNO_STR(_init_msg, _msg) \
834 BT_CPPLOGI_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
835 #define BT_CPPLOGW_ERRNO_STR(_init_msg, _msg) \
836 BT_CPPLOGW_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
837 #define BT_CPPLOGE_ERRNO_STR(_init_msg, _msg) \
838 BT_CPPLOGE_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
839 #define BT_CPPLOGF_ERRNO_STR(_init_msg, _msg) \
840 BT_CPPLOGF_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
843 * Calls log() on `_logger` with the `Error` level to log an error and
844 * append a cause to the error of the current thread.
846 #define BT_CPPLOGE_APPEND_CAUSE_SPEC(_logger, _fmt, ...) \
847 (_logger).log<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_fmt), \
851 * BT_CPPLOGE_APPEND_CAUSE_SPEC() using the default logger.
853 #define BT_CPPLOGE_APPEND_CAUSE(_fmt, ...) \
854 BT_CPPLOGE_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
857 * Calls logStr() on `_logger` with the `Error` level to log an error and
858 * append a cause to the error of the current thread.
860 #define BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_logger, _msg) \
861 (_logger).logStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_msg))
864 * BT_CPPLOGE_STR_APPEND_CAUSE_SPEC() using the default logger.
866 #define BT_CPPLOGE_STR_APPEND_CAUSE(_msg) \
867 BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
870 * Calls logErrorAndThrow() on `_logger` to log an error, append a cause
871 * to the error of the current thread, and throw an instance of
874 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \
875 (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
878 * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger.
880 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \
881 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_fmt), ##__VA_ARGS__)
884 * Calls logErrorStrAndThrow() on `_logger` to log an error, append a
885 * cause to the error of the current thread, and throw an instance of
888 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _msg) \
889 (_logger).logErrorStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_msg))
892 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
895 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _msg) \
896 BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_msg))
899 * Calls logErrorAndRethrow() on `_logger` to log an error, append a
900 * cause to the error of the current thread, and throw an instance of
903 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \
904 (_logger).logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
907 * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger.
909 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \
910 BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
913 * Calls logErrorStrAndRethrow() on `_logger` to log an error, append a
914 * cause to the error of the current thread, and throw an instance of
917 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \
918 (_logger).logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg))
921 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
924 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \
925 BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
928 * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a
929 * cause to the error of the current thread, and throw an instance of
932 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _fmt, ...) \
933 (_logger).logErrorErrnoAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
934 (_fmt), ##__VA_ARGS__)
937 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default
940 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \
941 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
942 (_fmt), ##__VA_ARGS__)
945 * Calls logErrorErrnoStrAndThrow() on `_logger` to log an error, append
946 * a cause to the error of the current thread, and throw an instance of
949 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _msg) \
950 (_logger).logErrorErrnoStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
954 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
957 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _msg) \
958 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
962 * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append
963 * a cause to the error of the current thread, and throw an instance of
966 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _fmt, ...) \
967 (_logger).logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
971 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
974 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_init_msg, _fmt, ...) \
975 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), \
979 * Calls logErrorErrnoStrAndRethrow() on `_logger` to log an error,
980 * append a cause to the error of the current thread, and throw an
981 * instance of `_exc_cls`.
983 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _msg) \
984 (_logger).logErrorErrnoStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
987 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the
990 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW(_init_msg, _msg) \
991 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
993 #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */