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),
63 * Builds a logger from the self component class `selfCompCls` using
64 * the tag `tag` and the logging level of `privQueryExec`.
66 explicit Logger(const bt2::SelfComponentClass selfCompCls,
67 const bt2::PrivateQueryExecutor privQueryExec, std::string tag) noexcept :
68 _mSelfCompCls {selfCompCls},
69 _mLevel {static_cast<Level>(privQueryExec.loggingLevel())}, _mTag {std::move(tag)}
74 * Builds a logger from the self component `selfComp` using the tag
77 explicit Logger(const bt2::SelfComponent selfComp, std::string tag) noexcept :
78 _mSelfComp {selfComp}, _mLevel {static_cast<Level>(selfComp.loggingLevel())}, _mTag {
85 * Builds a logger from the self source component `selfComp` using
88 explicit Logger(const bt2::SelfSourceComponent selfComp, std::string tag) noexcept :
90 bt2::SelfComponent {bt_self_component_source_as_self_component(selfComp.libObjPtr())},
96 * Builds a logger from the self filter component `selfComp` using
99 explicit Logger(const bt2::SelfFilterComponent selfComp, std::string tag) noexcept :
101 bt2::SelfComponent {bt_self_component_filter_as_self_component(selfComp.libObjPtr())},
107 * Builds a logger from the self sink component `selfComp` using the
110 explicit Logger(const bt2::SelfSinkComponent selfComp, std::string tag) noexcept :
111 Logger {bt2::SelfComponent {bt_self_component_sink_as_self_component(selfComp.libObjPtr())},
117 * Builds a logger from the self message iterator `selfMsgIter`
118 * using the tag `tag`.
120 explicit Logger(const bt2::SelfMessageIterator selfMsgIter, std::string tag) noexcept :
121 _mSelfMsgIter {selfMsgIter},
122 _mLevel {static_cast<Level>(selfMsgIter.component().loggingLevel())}, _mTag {std::move(tag)}
127 * Builds a logger from the module named `moduleName` using the tag
128 * `tag` and logging level `logLevel`.
130 explicit Logger(std::string moduleName, std::string tag, const Level logLevel) noexcept :
131 _mModuleName {std::move(moduleName)}, _mLevel {logLevel}, _mTag {std::move(tag)}
136 * Builds a logger from another logger `other` using the new tag
139 explicit Logger(const Logger& other, std::string newTag) :
140 _mSelfCompCls {other._mSelfCompCls}, _mSelfComp {other._mSelfComp},
141 _mSelfMsgIter {other._mSelfMsgIter},
142 _mModuleName {other._mModuleName}, _mLevel {other._mLevel}, _mTag {std::move(newTag)}
147 * Current logging level.
149 Level level() const noexcept
155 * Current logging level converted to a `bt_log_level` value.
159 bt_log_level cLevel() const noexcept
161 return static_cast<bt_log_level>(_mLevel);
165 * Whether or not this logger would log at the level `level`.
167 bool wouldLog(const Level level) const noexcept
169 return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel));
173 * Whether or not this logger would log at the trace level.
175 bool wouldLogT() const noexcept
177 return this->wouldLog(Level::Trace);
181 * Whether or not this logger would log at the debug level.
183 bool wouldLogD() const noexcept
185 return this->wouldLog(Level::Debug);
189 * Whether or not this logger would log at the info level.
191 bool wouldLogI() const noexcept
193 return this->wouldLog(Level::Info);
197 * Whether or not this logger would log at the warning level.
199 bool wouldLogW() const noexcept
201 return this->wouldLog(Level::Warning);
205 * Whether or not this logger would log at the error level.
207 bool wouldLogE() const noexcept
209 return this->wouldLog(Level::Error);
213 * Whether or not this logger would log at the fatal level.
215 bool wouldLogF() const noexcept
217 return this->wouldLog(Level::Fatal);
223 const std::string& tag() const noexcept
229 * Self component class actor, or `bt2s::nullopt` if none.
231 const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept
233 return _mSelfCompCls;
237 * Self component actor, or `bt2s::nullopt` if none.
239 const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept
245 * Self message iterator actor, or `bt2s::nullopt` if none.
247 const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept
249 return _mSelfMsgIter;
253 * Name of module actor, or `bt2s::nullopt` if none.
255 const bt2s::optional<std::string>& moduleName() const noexcept
261 struct _StdLogWriter final
263 static void write(const char * const fileName, const char * const funcName,
264 const unsigned lineNo, const Level level, const char * const tag,
265 const void *, unsigned int, const char * const initMsg,
266 const char * const msg) noexcept
268 BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0);
269 bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg);
275 * Logs using the level `LevelV`.
277 * This method forwards `fmt` and `args` to fmt::format() to create
280 * If `AppendCauseV` is true, this method also appends a cause to
281 * the error of the current thread using the same message.
283 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
284 void log(const char * const fileName, const char * const funcName, const unsigned int lineNo,
285 const char * const fmt, ArgTs&&...args) const
287 this->_log<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0, "",
288 fmt, std::forward<ArgTs>(args)...);
292 * Logs `msg` using the level `LevelV`.
294 * If `AppendCauseV` is true, this method also appends a cause to
295 * the error of the current thread using the same message.
297 template <Level LevelV, bool AppendCauseV>
298 void logStr(const char * const fileName, const char * const funcName, const unsigned int lineNo,
299 const char * const msg) const
301 this->_logStr<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0,
306 * Like log() with the `Level::Error` level, but also throws a
307 * default-constructed instance of `ExcT`.
309 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
310 [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName,
311 const unsigned int lineNo, const char * const fmt,
312 ArgTs&&...args) const
314 this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt,
315 std::forward<ArgTs>(args)...);
320 * Like logStr() with the `Level::Error` level, but also throws a
321 * default-constructed instance of `ExcT`.
323 template <bool AppendCauseV, typename ExcT>
324 [[noreturn]] void logErrorStrAndThrow(const char * const fileName, const char * const funcName,
325 const unsigned int lineNo, const char * const msg) const
327 this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg);
332 * Like log() with the `Level::Error` level, but also rethrows.
334 template <bool AppendCauseV, typename... ArgTs>
335 [[noreturn]] void logErrorAndRethrow(const char * const fileName, const char * const funcName,
336 const unsigned int lineNo, const char * const fmt,
337 ArgTs&&...args) const
339 this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt,
340 std::forward<ArgTs>(args)...);
345 * Like logStr() with the `Level::Error` level, but also rethrows.
347 template <bool AppendCauseV>
348 [[noreturn]] void logErrorStrAndRethrow(const char * const fileName,
349 const char * const funcName, const unsigned int lineNo,
350 const char * const msg) const
352 this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg);
357 struct _InitMsgLogWriter final
359 static void write(const char * const fileName, const char * const funcName,
360 const unsigned lineNo, const Level level, const char * const tag,
361 const void *, unsigned int, const char * const initMsg,
362 const char * const msg) noexcept
364 bt_log_write_printf(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
365 "%s%s", initMsg, msg);
371 * Logs the message of `errno` using the level `LevelV`.
373 * The log message starts with `initMsg`, is followed with the
374 * message for `errno`, and then with what fmt::format() creates
375 * given `fmt` and `args`.
377 * If `AppendCauseV` is true, this method also appends a cause to
378 * the error of the current thread using the same message.
380 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
381 void logErrno(const char * const fileName, const char * const funcName,
382 const unsigned int lineNo, const char * const initMsg, const char * const fmt,
383 ArgTs&&...args) const
385 this->_log<_InitMsgLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0,
386 this->_errnoIntroStr(initMsg).c_str(),
387 fmt, std::forward<ArgTs>(args)...);
391 * Logs the message of `errno` using the level `LevelV`.
393 * The log message starts with `initMsg`, is followed with the
394 * message for `errno`, and then with `msg`.
396 * If `AppendCauseV` is true, this method also appends a cause to
397 * the error of the current thread using the same message.
399 template <Level LevelV, bool AppendCauseV>
400 void logErrnoStr(const char * const fileName, const char * const funcName,
401 const unsigned int lineNo, const char * const initMsg,
402 const char * const msg) const
404 this->_logStr<_InitMsgLogWriter, LevelV, AppendCauseV>(
405 fileName, funcName, lineNo, nullptr, 0, this->_errnoIntroStr(initMsg).c_str(), msg);
409 * Like logErrno() with the `Level::Error` level, but also throws a
410 * default-constructed instance of `ExcT`.
412 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
413 [[noreturn]] void logErrorErrnoAndThrow(const char * const fileName,
414 const char * const funcName, const unsigned int lineNo,
415 const char * const initMsg, const char * const fmt,
416 ArgTs&&...args) const
418 this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
419 std::forward<ArgTs>(args)...);
424 * Like logErrnoStr() with the `Level::Error` level, but also throws
425 * a default-constructed instance of `ExcT`.
427 template <bool AppendCauseV, typename ExcT>
429 logErrorErrnoStrAndThrow(const char * const fileName, const char * const funcName,
430 const unsigned int lineNo, const char * const initMsg,
431 const char * const msg) const
433 this->logErrnoStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, msg);
438 * Like logErrno() with the `Level::Error` level, but also rethrows.
440 template <bool AppendCauseV, typename... ArgTs>
441 [[noreturn]] void logErrorErrnoAndRethrow(const char * const fileName,
442 const char * const funcName,
443 const unsigned int lineNo, const char * const initMsg,
444 const char * const fmt, ArgTs&&...args) const
446 this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
447 std::forward<ArgTs>(args)...);
452 * Like logErrnoStr() with the `Level::Error` level, but also
455 template <bool AppendCauseV>
457 logErrorErrnoStrAndRethrow(const char * const fileName, const char * const funcName,
458 const unsigned int lineNo, const char * const initMsg,
459 const char * const msg) const
461 this->logErrnoStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, msg);
466 struct _MemLogWriter final
468 static void write(const char * const fileName, const char * const funcName,
469 const unsigned lineNo, const Level level, const char * const tag,
470 const void * const memData, const unsigned int memLen, const char *,
471 const char * const msg) noexcept
473 bt_log_write_mem(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
474 memData, memLen, msg);
480 * Logs memory data using the level `LevelV`.
482 * This method forwards `fmt` and `args` to fmt::format() to create
485 template <Level LevelV, typename... ArgTs>
486 void logMem(const char * const fileName, const char * const funcName, const unsigned int lineNo,
487 const void * const memData, const unsigned int memLen, const char * const fmt,
488 ArgTs&&...args) const
490 this->_log<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, "",
491 fmt, std::forward<ArgTs>(args)...);
495 * Logs memory data using the level `LevelV`, starting with the
498 template <Level LevelV>
499 void logMemStr(const char * const fileName, const char * const funcName,
500 const unsigned int lineNo, const void * const memData, const unsigned int memLen,
501 const char * const msg) const
503 this->_logStr<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, "",
509 * Formats a log message with fmt::format() given `fmt` and `args`,
510 * and then forwards everything to _logStr().
512 template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs>
513 void _log(const char * const fileName, const char * const funcName, const unsigned int lineNo,
514 const void * const memData, const std::size_t memLen, const char * const initMsg,
515 const char * const fmt, ArgTs&&...args) const
517 /* Only format arguments if logging or appending an error cause */
518 if (G_UNLIKELY(this->wouldLog(LevelV) || AppendCauseV)) {
520 * Format arguments to our buffer (fmt::format_to() doesn't
521 * append a null character).
524 fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...);
525 _mBuf.push_back('\0');
528 this->_logStr<LogWriterT, LevelV, AppendCauseV>(fileName, funcName, lineNo, memData, memLen,
529 initMsg, _mBuf.data());
533 * Calls LogWriterT::write() with its arguments to log using the
536 * If `AppendCauseV` is true, this method also appends a cause to
537 * the error of the current thread using the concatenation of
538 * `initMsg` and `msg` as the message.
540 template <typename LogWriterT, Level LevelV, bool AppendCauseV>
541 void _logStr(const char * const fileName, const char * const funcName,
542 const unsigned int lineNo, const void * const memData, const std::size_t memLen,
543 const char * const initMsg, const char * const msg) const
545 /* Initial message and main message are required */
550 if (this->wouldLog(LevelV)) {
551 LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, memLen,
555 /* Append an error cause if needed */
558 bt_current_thread_error_append_cause_from_message_iterator(
559 _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
560 } else if (_mSelfComp) {
561 bt_current_thread_error_append_cause_from_component(
562 _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
563 } else if (_mSelfCompCls) {
564 bt_current_thread_error_append_cause_from_component_class(
565 _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
567 BT_ASSERT(_mModuleName);
568 bt_current_thread_error_append_cause_from_unknown(_mModuleName->data(), fileName,
569 lineNo, "%s%s", initMsg, msg);
574 static std::string _errnoIntroStr(const char * const initMsg)
576 BT_ASSERT(errno != 0);
577 return fmt::format("{}: {}", initMsg, g_strerror(errno));
580 /* Exactly one of the following four members has a value */
581 bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls;
582 bt2s::optional<bt2::SelfComponent> _mSelfComp;
583 bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter;
584 bt2s::optional<std::string> _mModuleName;
586 /* Current logging level */
592 /* Formatting buffer */
593 mutable std::vector<char> _mBuf;
597 * Returns `s` if it's not `nullptr`, or the `(null)` string otherwise.
599 inline const char *maybeNull(const char * const s) noexcept
601 return s ? s : "(null)";
604 } /* namespace bt2c */
606 /* Internal: default logger name */
607 #define _BT_CPPLOG_DEF_LOGGER _mLogger
610 * Calls log() on `_logger` to log using the level `_lvl`.
612 #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \
614 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
615 (_logger).log<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__); \
620 * BT_CPPLOG_EX() with specific logging levels.
622 #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \
623 BT_CPPLOG_EX(bt2c::Logger::Level::Trace, (_logger), (_fmt), ##__VA_ARGS__)
624 #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \
625 BT_CPPLOG_EX(bt2c::Logger::Level::Debug, (_logger), (_fmt), ##__VA_ARGS__)
626 #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \
627 BT_CPPLOG_EX(bt2c::Logger::Level::Info, (_logger), (_fmt), ##__VA_ARGS__)
628 #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \
629 BT_CPPLOG_EX(bt2c::Logger::Level::Warning, (_logger), (_fmt), ##__VA_ARGS__)
630 #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \
631 BT_CPPLOG_EX(bt2c::Logger::Level::Error, (_logger), (_fmt), ##__VA_ARGS__)
632 #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \
633 BT_CPPLOG_EX(bt2c::Logger::Level::Fatal, (_logger), (_fmt), ##__VA_ARGS__)
636 * BT_CPPLOG_EX() with specific logging levels and using the default
639 #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
640 #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
641 #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
642 #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
643 #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
644 #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
647 * Calls logStr() on `_logger` to log using the level `_lvl`.
649 #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \
650 (_logger).logStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg))
653 * BT_CPPLOG_STR_EX() with specific logging levels.
655 #define BT_CPPLOGT_STR_SPEC(_logger, _msg) \
656 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_msg))
657 #define BT_CPPLOGD_STR_SPEC(_logger, _msg) \
658 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_msg))
659 #define BT_CPPLOGI_STR_SPEC(_logger, _msg) \
660 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Info, (_logger), (_msg))
661 #define BT_CPPLOGW_STR_SPEC(_logger, _msg) \
662 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_msg))
663 #define BT_CPPLOGE_STR_SPEC(_logger, _msg) \
664 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Error, (_logger), (_msg))
665 #define BT_CPPLOGF_STR_SPEC(_logger, _msg) \
666 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_msg))
669 * BT_CPPLOG_STR_EX() with specific logging levels and using the default
672 #define BT_CPPLOGT_STR(_msg) BT_CPPLOGT_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
673 #define BT_CPPLOGD_STR(_msg) BT_CPPLOGD_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
674 #define BT_CPPLOGI_STR(_msg) BT_CPPLOGI_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
675 #define BT_CPPLOGW_STR(_msg) BT_CPPLOGW_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
676 #define BT_CPPLOGE_STR(_msg) BT_CPPLOGE_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
677 #define BT_CPPLOGF_STR(_msg) BT_CPPLOGF_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
680 * Calls logMem() on `_logger` to log using the level `_lvl`.
682 #define BT_CPPLOG_MEM_EX(_lvl, _logger, _mem_data, _mem_len, _fmt, ...) \
684 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
685 (_logger).logMem<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \
686 (_fmt), ##__VA_ARGS__); \
691 * BT_CPPLOG_MEM_EX() with specific logging levels.
693 #define BT_CPPLOGT_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
694 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Trace, (_logger), (_mem_data), (_mem_len), (_fmt), \
696 #define BT_CPPLOGD_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
697 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Debug, (_logger), (_mem_data), (_mem_len), (_fmt), \
699 #define BT_CPPLOGI_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
700 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Info, (_logger), (_mem_data), (_mem_len), (_fmt), \
702 #define BT_CPPLOGW_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
703 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Warning, (_logger), (_mem_data), (_mem_len), (_fmt), \
705 #define BT_CPPLOGE_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
706 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_fmt), \
708 #define BT_CPPLOGF_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
709 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Fatal, (_logger), (_mem_data), (_mem_len), (_fmt), \
713 * BT_CPPLOG_MEM_EX() with specific logging levels and using the default
716 #define BT_CPPLOGT_MEM(_mem_data, _mem_len, _fmt, ...) \
717 BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
718 #define BT_CPPLOGD_MEM(_mem_data, _mem_len, _fmt, ...) \
719 BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
720 #define BT_CPPLOGI_MEM(_mem_data, _mem_len, _fmt, ...) \
721 BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
722 #define BT_CPPLOGW_MEM(_mem_data, _mem_len, _fmt, ...) \
723 BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
724 #define BT_CPPLOGE_MEM(_mem_data, _mem_len, _fmt, ...) \
725 BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
726 #define BT_CPPLOGF_MEM(_mem_data, _mem_len, _fmt, ...) \
727 BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
730 * Calls logMemStr() on `_logger` to log using the level `_lvl`.
732 #define BT_CPPLOG_MEM_STR_EX(_lvl, _logger, _mem_data, _mem_len, _msg) \
733 (_logger).logMemStr<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), (_msg))
736 * BT_CPPLOG_MEM_STR_EX() with specific logging levels.
738 #define BT_CPPLOGT_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
739 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_mem_data), (_mem_len), (_msg))
740 #define BT_CPPLOGD_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
741 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_mem_data), (_mem_len), (_msg))
742 #define BT_CPPLOGI_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
743 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_mem_data), (_mem_len), (_msg))
744 #define BT_CPPLOGW_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
745 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_mem_data), (_mem_len), (_msg))
746 #define BT_CPPLOGE_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
747 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_msg))
748 #define BT_CPPLOGF_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
749 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_mem_data), (_mem_len), (_msg))
752 * BT_CPPLOG_MEM_STR_EX() with specific logging levels and using the
755 #define BT_CPPLOGT_MEM_STR(_mem_data, _mem_len, _msg) \
756 BT_CPPLOGT_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
757 #define BT_CPPLOGD_MEM_STR(_mem_data, _mem_len, _msg) \
758 BT_CPPLOGD_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
759 #define BT_CPPLOGI_MEM_STR(_mem_data, _mem_len, _msg) \
760 BT_CPPLOGI_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
761 #define BT_CPPLOGW_MEM_STR(_mem_data, _mem_len, _msg) \
762 BT_CPPLOGW_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
763 #define BT_CPPLOGE_MEM_STR(_mem_data, _mem_len, _msg) \
764 BT_CPPLOGE_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
765 #define BT_CPPLOGF_MEM_STR(_mem_data, _mem_len, _msg) \
766 BT_CPPLOGF_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
769 * Calls logErrno() on `_logger` to log using the level `_lvl` and
770 * initial message `_init_msg`.
772 #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _init_msg, _fmt, ...) \
774 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
775 (_logger).logErrno<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
781 * BT_CPPLOG_ERRNO_EX() with specific logging levels.
783 #define BT_CPPLOGT_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
784 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
785 #define BT_CPPLOGD_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
786 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
787 #define BT_CPPLOGI_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
788 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
789 #define BT_CPPLOGW_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
790 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
791 #define BT_CPPLOGE_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
792 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
793 #define BT_CPPLOGF_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
794 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
797 * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the
800 #define BT_CPPLOGT_ERRNO(_init_msg, _fmt, ...) \
801 BT_CPPLOGT_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
802 #define BT_CPPLOGD_ERRNO(_init_msg, _fmt, ...) \
803 BT_CPPLOGD_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
804 #define BT_CPPLOGI_ERRNO(_init_msg, _fmt, ...) \
805 BT_CPPLOGI_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
806 #define BT_CPPLOGW_ERRNO(_init_msg, _fmt, ...) \
807 BT_CPPLOGW_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
808 #define BT_CPPLOGE_ERRNO(_init_msg, _fmt, ...) \
809 BT_CPPLOGE_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
810 #define BT_CPPLOGF_ERRNO(_init_msg, _fmt, ...) \
811 BT_CPPLOGF_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
814 * Calls logErrnoStr() on `_logger` to log using the level `_lvl` and
815 * initial message `_init_msg`.
817 #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _init_msg, _msg) \
818 (_logger).logErrnoStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
821 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels.
823 #define BT_CPPLOGT_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
824 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_msg))
825 #define BT_CPPLOGD_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
826 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_msg))
827 #define BT_CPPLOGI_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
828 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_msg))
829 #define BT_CPPLOGW_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
830 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_msg))
831 #define BT_CPPLOGE_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
832 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_msg))
833 #define BT_CPPLOGF_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
834 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_msg))
837 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels and using the
840 #define BT_CPPLOGT_ERRNO_STR(_init_msg, _msg) \
841 BT_CPPLOGT_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
842 #define BT_CPPLOGD_ERRNO_STR(_init_msg, _msg) \
843 BT_CPPLOGD_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
844 #define BT_CPPLOGI_ERRNO_STR(_init_msg, _msg) \
845 BT_CPPLOGI_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
846 #define BT_CPPLOGW_ERRNO_STR(_init_msg, _msg) \
847 BT_CPPLOGW_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
848 #define BT_CPPLOGE_ERRNO_STR(_init_msg, _msg) \
849 BT_CPPLOGE_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
850 #define BT_CPPLOGF_ERRNO_STR(_init_msg, _msg) \
851 BT_CPPLOGF_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
854 * Calls log() on `_logger` with the `Error` level to log an error and
855 * append a cause to the error of the current thread.
857 #define BT_CPPLOGE_APPEND_CAUSE_SPEC(_logger, _fmt, ...) \
858 (_logger).log<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_fmt), \
862 * BT_CPPLOGE_APPEND_CAUSE_SPEC() using the default logger.
864 #define BT_CPPLOGE_APPEND_CAUSE(_fmt, ...) \
865 BT_CPPLOGE_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
868 * Calls logStr() on `_logger` with the `Error` level to log an error and
869 * append a cause to the error of the current thread.
871 #define BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_logger, _msg) \
872 (_logger).logStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_msg))
875 * BT_CPPLOGE_STR_APPEND_CAUSE_SPEC() using the default logger.
877 #define BT_CPPLOGE_STR_APPEND_CAUSE(_msg) \
878 BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
881 * Calls logErrorAndThrow() on `_logger` to log an error, append a cause
882 * to the error of the current thread, and throw an instance of
885 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \
886 (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
889 * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger.
891 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \
892 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_fmt), ##__VA_ARGS__)
895 * Calls logErrorStrAndThrow() on `_logger` to log an error, append a
896 * cause to the error of the current thread, and throw an instance of
899 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _msg) \
900 (_logger).logErrorStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_msg))
903 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
906 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _msg) \
907 BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_msg))
910 * Calls logErrorAndRethrow() on `_logger` to log an error, append a
911 * cause to the error of the current thread, and throw an instance of
914 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \
915 (_logger).logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
918 * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger.
920 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \
921 BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
924 * Calls logErrorStrAndRethrow() on `_logger` to log an error, append a
925 * cause to the error of the current thread, and throw an instance of
928 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \
929 (_logger).logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg))
932 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
935 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \
936 BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
939 * Calls logErrno() on `_logger` with the `Level::Error` level to log an
940 * error and append a cause to the error of the current thread.
942 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_logger, _init_msg, _fmt, ...) \
943 (_logger).logErrno<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \
944 (_init_msg), (_fmt), ##__VA_ARGS__)
947 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC() using the default logger.
949 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE(_init_msg, _fmt, ...) \
950 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
953 * Calls logErrnoStr() on `_logger` with the `Level::Error` level to log
954 * an error and append a cause to the error of the current thread.
956 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_logger, _init_msg, _msg) \
957 (_logger).logErrnoStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \
961 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC() using the default logger.
963 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE(_init_msg, _msg) \
964 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
967 * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a
968 * cause to the error of the current thread, and throw an instance of
971 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _fmt, ...) \
972 (_logger).logErrorErrnoAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
973 (_fmt), ##__VA_ARGS__)
976 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default
979 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \
980 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
981 (_fmt), ##__VA_ARGS__)
984 * Calls logErrorErrnoStrAndThrow() on `_logger` to log an error, append
985 * a cause to the error of the current thread, and throw an instance of
988 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _msg) \
989 (_logger).logErrorErrnoStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
993 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
996 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _msg) \
997 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
1001 * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append
1002 * a cause to the error of the current thread, and throw an instance of
1005 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _fmt, ...) \
1006 (_logger).logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
1010 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
1013 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_init_msg, _fmt, ...) \
1014 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), \
1018 * Calls logErrorErrnoStrAndRethrow() on `_logger` to log an error,
1019 * append a cause to the error of the current thread, and throw an
1020 * instance of `_exc_cls`.
1022 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _msg) \
1023 (_logger).logErrorErrnoStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
1026 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the
1029 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW(_init_msg, _msg) \
1030 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
1032 #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */