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 logNoThrow(), logMemNoThrow(), logErrnoNoThrow(),
36 * logErrorAndThrow(), logErrorAndRethrow(), logErrorErrnoAndThrow(),
37 * and 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 Logger {selfMsgIter.component(), std::move(tag)}
120 _mSelfMsgIter = selfMsgIter;
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 _mSelfComp {other._mSelfComp}, _mSelfMsgIter {other._mSelfMsgIter},
138 _mModuleName {other._mModuleName}, _mLevel {other._mLevel}, _mTag {std::move(newTag)}
143 * Current logging level.
145 Level level() const noexcept
151 * Current logging level converted to a `bt_log_level` value.
155 bt_log_level cLevel() const noexcept
157 return static_cast<bt_log_level>(_mLevel);
161 * Whether or not this logger would log at the level `level`.
163 bool wouldLog(const Level level) const noexcept
165 return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel));
169 * Whether or not this logger would log at the trace level.
171 bool wouldLogT() const noexcept
173 return this->wouldLog(Level::TRACE);
177 * Whether or not this logger would log at the debug level.
179 bool wouldLogD() const noexcept
181 return this->wouldLog(Level::DEBUG);
185 * Whether or not this logger would log at the info level.
187 bool wouldLogI() const noexcept
189 return this->wouldLog(Level::INFO);
193 * Whether or not this logger would log at the warning level.
195 bool wouldLogW() const noexcept
197 return this->wouldLog(Level::WARNING);
201 * Whether or not this logger would log at the error level.
203 bool wouldLogE() const noexcept
205 return this->wouldLog(Level::ERROR);
209 * Whether or not this logger would log at the fatal level.
211 bool wouldLogF() const noexcept
213 return this->wouldLog(Level::FATAL);
219 const std::string& tag() const noexcept
225 * Self component class actor, or `bt2s::nullopt` if none.
227 const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept
229 return _mSelfCompCls;
233 * Self component actor, or `bt2s::nullopt` if none.
235 const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept
241 * Self message iterator actor, or `bt2s::nullopt` if none.
243 const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept
245 return _mSelfMsgIter;
249 * Name of module actor, or `bt2s::nullopt` if none.
251 const bt2s::optional<std::string>& moduleName() const noexcept
257 struct _StdLogWriter final
259 static void write(const char * const fileName, const char * const funcName,
260 const unsigned lineNo, const Level level, const char * const tag,
261 const void *, unsigned int, const char * const initMsg,
262 const char * const msg) noexcept
264 BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0);
265 bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg);
271 * Logs using the level `LevelV`.
273 * This method forwards `fmt` and `args` to fmt::format() to create
276 * If `AppendCauseV` is true, this method also appends a cause to
277 * the error of the current thread using the same message.
279 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
280 void logNoThrow(const char * const fileName, const char * const funcName,
281 const unsigned int lineNo, const char * const fmt, ArgTs&&...args) const
283 this->_logNoThrow<_StdLogWriter, LevelV, AppendCauseV>(
284 fileName, funcName, lineNo, nullptr, 0, "", fmt, std::forward<ArgTs>(args)...);
288 * Logs `msg` using the level `LevelV`.
290 * If `AppendCauseV` is true, this method also appends a cause to
291 * the error of the current thread using the same message.
293 template <Level LevelV, bool AppendCauseV>
294 void logStrNoThrow(const char * const fileName, const char * const funcName,
295 const unsigned int lineNo, const char * const msg) const
297 this->_logStrNoThrow<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo,
298 nullptr, 0, "", msg);
302 * Like logAndNoThrow() with the `Level::ERROR` level, but also
303 * throws a default-constructed instance of `ExcT`.
305 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
306 [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName,
307 const unsigned int lineNo, const char * const fmt,
308 ArgTs&&...args) const
310 this->logNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, fmt,
311 std::forward<ArgTs>(args)...);
316 * Like logStrAndNoThrow() with the `Level::ERROR` level, but also
317 * throws a default-constructed instance of `ExcT`.
319 template <bool AppendCauseV, typename ExcT>
320 [[noreturn]] void logErrorStrAndThrow(const char * const fileName, const char * const funcName,
321 const unsigned int lineNo, const char * const msg) const
323 this->logStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, msg);
328 * Like logAndNoThrow() with the `Level::ERROR` level, but also
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->logNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, fmt,
337 std::forward<ArgTs>(args)...);
342 * Like logStrAndNoThrow() with the `Level::ERROR` level, but also
345 template <bool AppendCauseV>
346 [[noreturn]] void logErrorStrAndRethrow(const char * const fileName,
347 const char * const funcName, const unsigned int lineNo,
348 const char * const msg) const
350 this->logStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, msg);
355 struct _InitMsgLogWriter final
357 static void write(const char * const fileName, const char * const funcName,
358 const unsigned lineNo, const Level level, const char * const tag,
359 const void *, unsigned int, const char * const initMsg,
360 const char * const msg) noexcept
362 bt_log_write_printf(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
363 "%s%s", initMsg, msg);
369 * Logs the message of `errno` using the level `LevelV`.
371 * The log message starts with `initMsg`, is followed with the
372 * message for `errno`, and then with what fmt::format() creates
373 * given `fmt` and `args`.
375 * If `AppendCauseV` is true, this method also appends a cause to
376 * the error of the current thread using the same message.
378 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
379 void logErrnoNoThrow(const char * const fileName, const char * const funcName,
380 const unsigned int lineNo, const char * const initMsg,
381 const char * const fmt, ArgTs&&...args) const
383 this->_logNoThrow<_InitMsgLogWriter, LevelV, AppendCauseV>(
384 fileName, funcName, lineNo, nullptr, 0, this->_errnoIntroStr(initMsg).c_str(), fmt,
385 std::forward<ArgTs>(args)...);
389 * Logs the message of `errno` using the level `LevelV`.
391 * The log message starts with `initMsg`, is followed with the
392 * message for `errno`, and then with `msg`.
394 * If `AppendCauseV` is true, this method also appends a cause to
395 * the error of the current thread using the same message.
397 template <Level LevelV, bool AppendCauseV>
398 void logErrnoStrNoThrow(const char * const fileName, const char * const funcName,
399 const unsigned int lineNo, const char * const initMsg,
400 const char * const msg) const
402 this->_logStrNoThrow<_InitMsgLogWriter, LevelV, AppendCauseV>(
403 fileName, funcName, lineNo, nullptr, 0, this->_errnoIntroStr(initMsg).c_str(), msg);
407 * Like logErrnoNoThrow() with the `Level::ERROR` level, but also
408 * throws a default-constructed instance of `ExcT`.
410 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
411 [[noreturn]] void logErrorErrnoAndThrow(const char * const fileName,
412 const char * const funcName, const unsigned int lineNo,
413 const char * const initMsg, const char * const fmt,
414 ArgTs&&...args) const
416 this->logErrnoNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
417 std::forward<ArgTs>(args)...);
422 * Like logErrnoStrNoThrow() with the `Level::ERROR` level, but also
423 * throws a default-constructed instance of `ExcT`.
425 template <bool AppendCauseV, typename ExcT>
427 logErrorErrnoStrAndThrow(const char * const fileName, const char * const funcName,
428 const unsigned int lineNo, const char * const initMsg,
429 const char * const msg) const
431 this->logErrnoStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg,
437 * Like logErrnoNoThrow() with the `Level::ERROR` level, but also
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->logErrnoNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
447 std::forward<ArgTs>(args)...);
452 * Like logErrnoStrNoThrow() 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->logErrnoStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg,
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 logMemNoThrow(const char * const fileName, const char * const funcName,
488 const unsigned int lineNo, const void * const memData,
489 const unsigned int memLen, const char * const fmt, ArgTs&&...args) const
491 this->_logNoThrow<_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 logMemStrNoThrow(const char * const fileName, const char * const funcName,
501 const unsigned int lineNo, const void * const memData,
502 const unsigned int memLen, const char * const msg) const
504 this->_logStrNoThrow<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData,
510 * Formats a log message with fmt::format() given `fmt` and `args`,
511 * and then forwards everything to _logStrNoThrow().
513 template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs>
514 void _logNoThrow(const char * const fileName, const char * const funcName,
515 const unsigned int lineNo, const void * const memData,
516 const std::size_t memLen, const char * const initMsg, const char * const fmt,
517 ArgTs&&...args) const
519 /* Only format arguments if logging or appending an error cause */
520 if (G_UNLIKELY(this->wouldLog(LevelV) || AppendCauseV)) {
522 * Format arguments to our buffer (fmt::format_to() doesn't
523 * append a null character).
526 fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...);
527 _mBuf.push_back('\0');
530 this->_logStrNoThrow<LogWriterT, LevelV, AppendCauseV>(fileName, funcName, lineNo, memData,
531 memLen, initMsg, _mBuf.data());
535 * Calls LogWriterT::write() with its arguments to log using the
538 * If `AppendCauseV` is true, this method also appends a cause to
539 * the error of the current thread using the concatenation of
540 * `initMsg` and `msg` as the message.
542 template <typename LogWriterT, Level LevelV, bool AppendCauseV>
543 void _logStrNoThrow(const char * const fileName, const char * const funcName,
544 const unsigned int lineNo, const void * const memData,
545 const std::size_t memLen, const char * const initMsg,
546 const char * const msg) const
548 /* Initial message and main message are required */
553 if (this->wouldLog(LevelV)) {
554 LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, memLen,
558 /* Append an error cause if needed */
561 bt_current_thread_error_append_cause_from_message_iterator(
562 _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
563 } else if (_mSelfComp) {
564 bt_current_thread_error_append_cause_from_component(
565 _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
566 } else if (_mSelfCompCls) {
567 bt_current_thread_error_append_cause_from_component_class(
568 _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg);
570 BT_ASSERT(_mModuleName);
571 bt_current_thread_error_append_cause_from_unknown(_mModuleName->data(), fileName,
572 lineNo, "%s%s", initMsg, msg);
577 static std::string _errnoIntroStr(const char * const initMsg)
579 BT_ASSERT(errno != 0);
580 return fmt::format("{}: {}", initMsg, g_strerror(errno));
583 /* At least one of the following four members has a value */
584 bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls;
585 bt2s::optional<bt2::SelfComponent> _mSelfComp;
586 bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter;
587 bt2s::optional<std::string> _mModuleName;
589 /* Current logging level */
595 /* Formatting buffer */
596 mutable std::vector<char> _mBuf;
599 } /* namespace bt2c */
601 /* Internal: default logger name */
602 #define _BT_CPPLOG_DEF_LOGGER _mLogger
605 * Calls logNoThrow() on `_logger` to log using the level `_lvl` without
606 * appending nor throwing.
608 #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \
610 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
611 (_logger).logNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), \
617 * BT_CPPLOG_EX() with specific logging levels.
619 #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \
620 BT_CPPLOG_EX(bt2c::Logger::Level::TRACE, (_logger), (_fmt), ##__VA_ARGS__)
621 #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \
622 BT_CPPLOG_EX(bt2c::Logger::Level::DEBUG, (_logger), (_fmt), ##__VA_ARGS__)
623 #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \
624 BT_CPPLOG_EX(bt2c::Logger::Level::INFO, (_logger), (_fmt), ##__VA_ARGS__)
625 #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \
626 BT_CPPLOG_EX(bt2c::Logger::Level::WARNING, (_logger), (_fmt), ##__VA_ARGS__)
627 #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \
628 BT_CPPLOG_EX(bt2c::Logger::Level::ERROR, (_logger), (_fmt), ##__VA_ARGS__)
629 #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \
630 BT_CPPLOG_EX(bt2c::Logger::Level::FATAL, (_logger), (_fmt), ##__VA_ARGS__)
633 * BT_CPPLOG_EX() with specific logging levels and using the default
636 #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
637 #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
638 #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
639 #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
640 #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
641 #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
644 * Calls logStrNoThrow() on `_logger` to log using the level `_lvl`
645 * without appending nor throwing.
647 #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \
648 (_logger).logStrNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg))
651 * BT_CPPLOG_STR_EX() with specific logging levels.
653 #define BT_CPPLOGT_STR_SPEC(_logger, _msg) \
654 BT_CPPLOG_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_msg))
655 #define BT_CPPLOGD_STR_SPEC(_logger, _msg) \
656 BT_CPPLOG_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_msg))
657 #define BT_CPPLOGI_STR_SPEC(_logger, _msg) \
658 BT_CPPLOG_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_msg))
659 #define BT_CPPLOGW_STR_SPEC(_logger, _msg) \
660 BT_CPPLOG_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_msg))
661 #define BT_CPPLOGE_STR_SPEC(_logger, _msg) \
662 BT_CPPLOG_STR_EX(bt2c::Logger::Level::ERROR, (_logger), (_msg))
663 #define BT_CPPLOGF_STR_SPEC(_logger, _msg) \
664 BT_CPPLOG_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_msg))
667 * BT_CPPLOG_STR_EX() with specific logging levels and using the default
670 #define BT_CPPLOGT_STR(_msg) BT_CPPLOGT_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
671 #define BT_CPPLOGD_STR(_msg) BT_CPPLOGD_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
672 #define BT_CPPLOGI_STR(_msg) BT_CPPLOGI_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
673 #define BT_CPPLOGW_STR(_msg) BT_CPPLOGW_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
674 #define BT_CPPLOGE_STR(_msg) BT_CPPLOGE_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
675 #define BT_CPPLOGF_STR(_msg) BT_CPPLOGF_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
678 * Calls logMemNoThrow() on `_logger` to log using the level `_lvl`
679 * without appending nor throwing.
681 #define BT_CPPLOG_MEM_EX(_lvl, _logger, _mem_data, _mem_len, _fmt, ...) \
683 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
684 (_logger).logMemNoThrow<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \
685 (_fmt), ##__VA_ARGS__); \
690 * BT_CPPLOG_MEM_EX() with specific logging levels.
692 #define BT_CPPLOGT_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
693 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::TRACE, (_logger), (_mem_data), (_mem_len), (_fmt), \
695 #define BT_CPPLOGD_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
696 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::DEBUG, (_logger), (_mem_data), (_mem_len), (_fmt), \
698 #define BT_CPPLOGI_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
699 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::INFO, (_logger), (_mem_data), (_mem_len), (_fmt), \
701 #define BT_CPPLOGW_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
702 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::WARNING, (_logger), (_mem_data), (_mem_len), (_fmt), \
704 #define BT_CPPLOGE_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
705 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::ERROR, (_logger), (_mem_data), (_mem_len), (_fmt), \
707 #define BT_CPPLOGF_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
708 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::FATAL, (_logger), (_mem_data), (_mem_len), (_fmt), \
712 * BT_CPPLOG_MEM_EX() with specific logging levels and using the default
715 #define BT_CPPLOGT_MEM(_mem_data, _mem_len, _fmt, ...) \
716 BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
717 #define BT_CPPLOGD_MEM(_mem_data, _mem_len, _fmt, ...) \
718 BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
719 #define BT_CPPLOGI_MEM(_mem_data, _mem_len, _fmt, ...) \
720 BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
721 #define BT_CPPLOGW_MEM(_mem_data, _mem_len, _fmt, ...) \
722 BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
723 #define BT_CPPLOGE_MEM(_mem_data, _mem_len, _fmt, ...) \
724 BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
725 #define BT_CPPLOGF_MEM(_mem_data, _mem_len, _fmt, ...) \
726 BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
729 * Calls logMemStrNoThrow() on `_logger` to log using the level `_lvl`
730 * without appending nor throwing.
732 #define BT_CPPLOG_MEM_STR_EX(_lvl, _logger, _mem_data, _mem_len, _msg) \
733 (_logger).logMemStrNoThrow<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \
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 logErrnoNoThrow() on `_logger` to log using the level `_lvl`
771 * and initial message `_init_msg` without appending nor throwing.
773 #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _init_msg, _fmt, ...) \
775 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
776 (_logger).logErrnoNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), \
777 (_fmt), ##__VA_ARGS__); \
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 logErrnoStrNoThrow() on `_logger` to log using the level `_lvl`
816 * and initial message `_init_msg` without appending nor throwing.
818 #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _init_msg, _msg) \
819 (_logger).logErrnoStrNoThrow<(_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 logErrorAndThrow() on `_logger` to log an error, append a cause
856 * to the error of the current thread, and throw an instance of
859 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \
860 (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
863 * Calls logErrorAndThrow() on `_logger` to log an error, append a cause
864 * to the error of the current thread, and throw an instance of
867 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \
868 (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
871 * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger.
873 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \
874 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_fmt), ##__VA_ARGS__)
877 * Calls logErrorStrAndThrow() on `_logger` to log an error, append a
878 * cause to the error of the current thread, and throw an instance of
881 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _msg) \
882 (_logger).logErrorStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_msg))
885 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
888 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _msg) \
889 BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_msg))
892 * Calls logErrorAndRethrow() on `_logger` to log an error, append a
893 * cause to the error of the current thread, and throw an instance of
896 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \
897 (_logger).logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
900 * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger.
902 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \
903 BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
906 * Calls logErrorStrAndRethrow() on `_logger` to log an error, append a
907 * cause to the error of the current thread, and throw an instance of
910 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \
911 (_logger).logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg))
914 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
917 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \
918 BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
921 * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a
922 * cause to the error of the current thread, and throw an instance of
925 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _fmt, ...) \
926 (_logger).logErrorErrnoAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
927 (_fmt), ##__VA_ARGS__)
930 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default
933 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \
934 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
935 (_fmt), ##__VA_ARGS__)
938 * Calls logErrorErrnoStrAndThrow() on `_logger` to log an error, append
939 * a cause to the error of the current thread, and throw an instance of
942 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _msg) \
943 (_logger).logErrorErrnoStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
947 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
950 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _msg) \
951 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
955 * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append
956 * a cause to the error of the current thread, and throw an instance of
959 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _fmt, ...) \
960 (_logger).logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
964 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
967 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_init_msg, _fmt, ...) \
968 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), \
972 * Calls logErrorErrnoStrAndRethrow() on `_logger` to log an error,
973 * append a cause to the error of the current thread, and throw an
974 * instance of `_exc_cls`.
976 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _msg) \
977 (_logger).logErrorErrnoStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
980 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the
983 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW(_init_msg, _msg) \
984 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
986 #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */