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
15 #include <babeltrace2/babeltrace.h>
17 #include "common/assert.h"
18 #include "cpp-common/bt2/private-query-executor.hpp"
19 #include "cpp-common/bt2/self-component-class.hpp"
20 #include "cpp-common/bt2/self-component-port.hpp"
21 #include "cpp-common/bt2/self-message-iterator.hpp"
22 #include "cpp-common/bt2s/optional.hpp"
23 #include "cpp-common/vendor/fmt/core.h"
24 #include "logging/log-api.h"
29 * A logger contains an actor (self component class, self component,
30 * self message iterator, or simple module name), a current logging
31 * level, and a logging tag.
33 * It offers the logNoThrow(), logMemNoThrow(), logErrnoNoThrow(),
34 * logErrorAndThrow(), logErrorAndRethrow(), logErrorErrnoAndThrow(),
35 * and logErrorErrnoAndRethrow() method templates to log using a given
36 * level, optionally append a cause to the error of the current thread
37 * using the correct actor, and optionally throw or rethrow.
39 * The methods above expect a format string and zero or more arguments
40 * to be formatted with fmt::format().
45 /* Available log levels */
51 WARNING = BT_LOG_WARNING,
58 * Builds a logger from the self component class `selfCompCls` using
59 * the tag `tag` and the logging level of `privQueryExec`.
61 explicit Logger(const bt2::SelfComponentClass selfCompCls,
62 const bt2::PrivateQueryExecutor privQueryExec, std::string tag) noexcept :
63 _mSelfCompCls {selfCompCls},
64 _mLevel {static_cast<Level>(privQueryExec.loggingLevel())}, _mTag {std::move(tag)}
69 * Builds a logger from the self component `selfComp` using the tag
72 explicit Logger(const bt2::SelfComponent selfComp, std::string tag) noexcept :
73 _mSelfComp {selfComp}, _mLevel {static_cast<Level>(selfComp.loggingLevel())}, _mTag {
80 * Builds a logger from the self source component `selfComp` using
83 explicit Logger(const bt2::SelfSourceComponent selfComp, std::string tag) noexcept :
85 bt2::SelfComponent {bt_self_component_source_as_self_component(selfComp.libObjPtr())},
91 * Builds a logger from the self filter component `selfComp` using
94 explicit Logger(const bt2::SelfFilterComponent selfComp, std::string tag) noexcept :
96 bt2::SelfComponent {bt_self_component_filter_as_self_component(selfComp.libObjPtr())},
102 * Builds a logger from the self sink component `selfComp` using the
105 explicit Logger(const bt2::SelfSinkComponent selfComp, std::string tag) noexcept :
106 Logger {bt2::SelfComponent {bt_self_component_sink_as_self_component(selfComp.libObjPtr())},
112 * Builds a logger from the self message iterator `selfMsgIter`
113 * using the tag `tag`.
115 explicit Logger(const bt2::SelfMessageIterator selfMsgIter, std::string tag) noexcept :
116 Logger {selfMsgIter.component(), std::move(tag)}
118 _mSelfMsgIter = selfMsgIter;
122 * Builds a logger from the module named `moduleName` using the tag
123 * `tag` and logging level `logLevel`.
125 explicit Logger(std::string moduleName, std::string tag, const Level logLevel) noexcept :
126 _mModuleName {std::move(moduleName)}, _mLevel {logLevel}, _mTag {std::move(tag)}
131 * Builds a logger from another logger `other` using the new tag
134 explicit Logger(const Logger& other, std::string newTag) :
135 _mSelfComp {other._mSelfComp}, _mSelfMsgIter {other._mSelfMsgIter},
136 _mModuleName {other._mModuleName}, _mLevel {other._mLevel}, _mTag {std::move(newTag)}
141 * Current logging level.
143 Level level() const noexcept
149 * Current logging level converted to a `bt_log_level` value.
153 bt_log_level cLevel() const noexcept
155 return static_cast<bt_log_level>(_mLevel);
159 * Whether or not this logger would log at the level `level`.
161 bool wouldLog(const Level level) const noexcept
163 return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel));
167 * Whether or not this logger would log at the trace level.
169 bool wouldLogT() const noexcept
171 return this->wouldLog(Level::TRACE);
175 * Whether or not this logger would log at the debug level.
177 bool wouldLogD() const noexcept
179 return this->wouldLog(Level::DEBUG);
183 * Whether or not this logger would log at the info level.
185 bool wouldLogI() const noexcept
187 return this->wouldLog(Level::INFO);
191 * Whether or not this logger would log at the warning level.
193 bool wouldLogW() const noexcept
195 return this->wouldLog(Level::WARNING);
199 * Whether or not this logger would log at the error level.
201 bool wouldLogE() const noexcept
203 return this->wouldLog(Level::ERROR);
207 * Whether or not this logger would log at the fatal level.
209 bool wouldLogF() const noexcept
211 return this->wouldLog(Level::FATAL);
217 const std::string& tag() const noexcept
223 * Self component class actor, or `bt2s::nullopt` if none.
225 const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept
227 return _mSelfCompCls;
231 * Self component actor, or `bt2s::nullopt` if none.
233 const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept
239 * Self message iterator actor, or `bt2s::nullopt` if none.
241 const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept
243 return _mSelfMsgIter;
247 * Name of module actor, or `bt2s::nullopt` if none.
249 const bt2s::optional<std::string>& moduleName() const noexcept
255 struct _StdLogWriter final
257 static void write(const char * const fileName, const char * const funcName,
258 const unsigned lineNo, const Level level, const char * const tag,
259 const void *, unsigned int, const char *, const char * const msg) noexcept
261 bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg);
267 * Logs using the level `LevelV`.
269 * This method forwards `fmt` and `args` to fmt::format() to create
272 * If `AppendCauseV` is true, this method also appends a cause to
273 * the error of the current thread using the same message.
275 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
276 void logNoThrow(const char * const fileName, const char * const funcName,
277 const unsigned int lineNo, const char * const fmt, ArgTs&&...args) const
279 this->_logNoThrow<_StdLogWriter, LevelV, AppendCauseV>(
280 fileName, funcName, lineNo, nullptr, 0, nullptr, fmt, std::forward<ArgTs>(args)...);
284 * Logs `msg` using the level `LevelV`.
286 * If `AppendCauseV` is true, this method also appends a cause to
287 * the error of the current thread using the same message.
289 template <Level LevelV, bool AppendCauseV>
290 void logStrNoThrow(const char * const fileName, const char * const funcName,
291 const unsigned int lineNo, const char * const msg) const
293 this->_logStrNoThrow<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo,
294 nullptr, 0, nullptr, msg);
298 * Like logAndNoThrow() with the `Level::ERROR` level, but also
299 * throws a default-constructed instance of `ExcT`.
301 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
302 [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName,
303 const unsigned int lineNo, const char * const fmt,
304 ArgTs&&...args) const
306 this->logNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, fmt,
307 std::forward<ArgTs>(args)...);
312 * Like logStrAndNoThrow() with the `Level::ERROR` level, but also
313 * throws a default-constructed instance of `ExcT`.
315 template <bool AppendCauseV, typename ExcT>
316 [[noreturn]] void logErrorStrAndThrow(const char * const fileName, const char * const funcName,
317 const unsigned int lineNo, const char * const msg) const
319 this->logStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, msg);
324 * Like logAndNoThrow() with the `Level::ERROR` level, but also
327 template <bool AppendCauseV, typename... ArgTs>
328 [[noreturn]] void logErrorAndRethrow(const char * const fileName, const char * const funcName,
329 const unsigned int lineNo, const char * const fmt,
330 ArgTs&&...args) const
332 this->logNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, fmt,
333 std::forward<ArgTs>(args)...);
338 * Like logStrAndNoThrow() with the `Level::ERROR` level, but also
341 template <bool AppendCauseV>
342 [[noreturn]] void logErrorStrAndRethrow(const char * const fileName,
343 const char * const funcName, const unsigned int lineNo,
344 const char * const msg) const
346 this->logStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, msg);
351 struct _ErrnoLogWriter final
353 static void write(const char * const fileName, const char * const funcName,
354 const unsigned lineNo, const Level level, const char * const tag,
355 const void *, unsigned int, const char * const initMsg,
356 const char * const msg) noexcept
358 bt_log_write_errno(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
365 * Logs the message of `errno` using the level `LevelV`.
367 * The log message starts with `initMsg`, is followed with the
368 * message for `errno`, and then with what fmt::format() creates
369 * given `fmt` and `args`.
371 * If `AppendCauseV` is true, this method also appends a cause to
372 * the error of the current thread using the same message.
374 template <Level LevelV, bool AppendCauseV, typename... ArgTs>
375 void logErrnoNoThrow(const char * const fileName, const char * const funcName,
376 const unsigned int lineNo, const char * const initMsg,
377 const char * const fmt, ArgTs&&...args) const
379 this->_logNoThrow<_ErrnoLogWriter, LevelV, AppendCauseV>(
380 fileName, funcName, lineNo, nullptr, 0, initMsg, fmt, std::forward<ArgTs>(args)...);
384 * Logs the message of `errno` using the level `LevelV`.
386 * The log message starts with `initMsg`, is followed with the
387 * message for `errno`, and then with `msg`.
389 * If `AppendCauseV` is true, this method also appends a cause to
390 * the error of the current thread using the same message.
392 template <Level LevelV, bool AppendCauseV>
393 void logErrnoStrNoThrow(const char * const fileName, const char * const funcName,
394 const unsigned int lineNo, const char * const initMsg,
395 const char * const msg) const
397 this->_logStrNoThrow<_ErrnoLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo,
398 nullptr, 0, initMsg, msg);
402 * Like logErrnoNoThrow() with the `Level::ERROR` level, but also
403 * throws a default-constructed instance of `ExcT`.
405 template <bool AppendCauseV, typename ExcT, typename... ArgTs>
406 [[noreturn]] void logErrorErrnoAndThrow(const char * const fileName,
407 const char * const funcName, const unsigned int lineNo,
408 const char * const initMsg, const char * const fmt,
409 ArgTs&&...args) const
411 this->logErrnoNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
412 std::forward<ArgTs>(args)...);
417 * Like logErrnoStrNoThrow() with the `Level::ERROR` level, but also
418 * throws a default-constructed instance of `ExcT`.
420 template <bool AppendCauseV, typename ExcT>
422 logErrorErrnoStrAndThrow(const char * const fileName, const char * const funcName,
423 const unsigned int lineNo, const char * const initMsg,
424 const char * const msg) const
426 this->logErrnoStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg,
432 * Like logErrnoNoThrow() with the `Level::ERROR` level, but also
435 template <bool AppendCauseV, typename... ArgTs>
436 [[noreturn]] void logErrorErrnoAndRethrow(const char * const fileName,
437 const char * const funcName,
438 const unsigned int lineNo, const char * const initMsg,
439 const char * const fmt, ArgTs&&...args) const
441 this->logErrnoNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt,
442 std::forward<ArgTs>(args)...);
447 * Like logErrnoStrNoThrow() with the `Level::ERROR` level, but also
450 template <bool AppendCauseV>
452 logErrorErrnoStrAndRethrow(const char * const fileName, const char * const funcName,
453 const unsigned int lineNo, const char * const initMsg,
454 const char * const msg) const
456 this->logErrnoStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg,
462 struct _MemLogWriter final
464 static void write(const char * const fileName, const char * const funcName,
465 const unsigned lineNo, const Level level, const char * const tag,
466 const void * const memData, const unsigned int memLen, const char *,
467 const char * const msg) noexcept
469 bt_log_write_mem(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag,
470 memData, memLen, msg);
476 * Logs memory data using the level `LevelV`.
478 * This method forwards `fmt` and `args` to fmt::format() to create
481 template <Level LevelV, typename... ArgTs>
482 void logMemNoThrow(const char * const fileName, const char * const funcName,
483 const unsigned int lineNo, const void * const memData,
484 const unsigned int memLen, const char * const fmt, ArgTs&&...args) const
486 this->_logNoThrow<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen,
487 nullptr, fmt, std::forward<ArgTs>(args)...);
491 * Logs memory data using the level `LevelV`, starting with the
494 template <Level LevelV>
495 void logMemStrNoThrow(const char * const fileName, const char * const funcName,
496 const unsigned int lineNo, const void * const memData,
497 const unsigned int memLen, const char * const msg) const
499 this->_logStrNoThrow<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData,
500 memLen, nullptr, msg);
505 * Formats a log message with fmt::format() given `fmt` and `args`,
506 * and the forwards everything to _logStrNoThrow().
508 template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs>
509 void _logNoThrow(const char * const fileName, const char * const funcName,
510 const unsigned int lineNo, const void * const memData,
511 const std::size_t memLen, const char * const initMsg, const char * const fmt,
512 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->_logStrNoThrow<LogWriterT, LevelV, AppendCauseV>(fileName, funcName, lineNo, memData,
526 memLen, initMsg, _mBuf.data());
530 * Logs `msg` using the level `LevelV`.
532 * Calls LogWriterT::write() to write the actual log.
534 * If `AppendCauseV` is true, this method also appends a cause to
535 * the error of the current thread using the same message.
537 template <typename LogWriterT, Level LevelV, bool AppendCauseV>
538 void _logStrNoThrow(const char * const fileName, const char * const funcName,
539 const unsigned int lineNo, const void * const memData,
540 const std::size_t memLen, const char * const initMsg,
541 const char * const msg) const
544 if (this->wouldLog(LevelV)) {
545 LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, memLen,
549 /* Append an error cause if needed */
552 bt_current_thread_error_append_cause_from_message_iterator(
553 _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s", _mBuf.data());
554 } else if (_mSelfComp) {
555 bt_current_thread_error_append_cause_from_component(
556 _mSelfComp->libObjPtr(), fileName, lineNo, "%s", _mBuf.data());
557 } else if (_mSelfCompCls) {
558 bt_current_thread_error_append_cause_from_component_class(
559 _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s", _mBuf.data());
561 BT_ASSERT_DBG(_mModuleName);
562 bt_current_thread_error_append_cause_from_unknown(_mModuleName->data(), fileName,
563 lineNo, "%s", _mBuf.data());
568 /* At least one of the following four members has a value */
569 bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls;
570 bt2s::optional<bt2::SelfComponent> _mSelfComp;
571 bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter;
572 bt2s::optional<std::string> _mModuleName;
574 /* Current logging level */
580 /* Formatting buffer */
581 mutable std::vector<char> _mBuf;
584 } /* namespace bt2c */
586 /* Internal: default logger name */
587 #define _BT_CPPLOG_DEF_LOGGER _mLogger
590 * Calls logNoThrow() on `_logger` to log using the level `_lvl` without
591 * appending nor throwing.
593 #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \
595 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
596 (_logger).logNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), \
602 * BT_CPPLOG_EX() with specific logging levels.
604 #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \
605 BT_CPPLOG_EX(bt2c::Logger::Level::TRACE, (_logger), (_fmt), ##__VA_ARGS__)
606 #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \
607 BT_CPPLOG_EX(bt2c::Logger::Level::DEBUG, (_logger), (_fmt), ##__VA_ARGS__)
608 #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \
609 BT_CPPLOG_EX(bt2c::Logger::Level::INFO, (_logger), (_fmt), ##__VA_ARGS__)
610 #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \
611 BT_CPPLOG_EX(bt2c::Logger::Level::WARNING, (_logger), (_fmt), ##__VA_ARGS__)
612 #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \
613 BT_CPPLOG_EX(bt2c::Logger::Level::ERROR, (_logger), (_fmt), ##__VA_ARGS__)
614 #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \
615 BT_CPPLOG_EX(bt2c::Logger::Level::FATAL, (_logger), (_fmt), ##__VA_ARGS__)
618 * BT_CPPLOG_EX() with specific logging levels and using the default
621 #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
622 #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
623 #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
624 #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
625 #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
626 #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
629 * Calls logStrNoThrow() on `_logger` to log using the level `_lvl`
630 * without appending nor throwing.
632 #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \
633 (_logger).logStrNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg))
636 * BT_CPPLOG_STR_EX() with specific logging levels.
638 #define BT_CPPLOGT_STR_SPEC(_logger, _msg) \
639 BT_CPPLOG_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_msg))
640 #define BT_CPPLOGD_STR_SPEC(_logger, _msg) \
641 BT_CPPLOG_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_msg))
642 #define BT_CPPLOGI_STR_SPEC(_logger, _msg) \
643 BT_CPPLOG_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_msg))
644 #define BT_CPPLOGW_STR_SPEC(_logger, _msg) \
645 BT_CPPLOG_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_msg))
646 #define BT_CPPLOGE_STR_SPEC(_logger, _msg) \
647 BT_CPPLOG_STR_EX(bt2c::Logger::Level::ERROR, (_logger), (_msg))
648 #define BT_CPPLOGF_STR_SPEC(_logger, _msg) \
649 BT_CPPLOG_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_msg))
652 * BT_CPPLOG_STR_EX() with specific logging levels and using the default
655 #define BT_CPPLOGT_STR(_msg) BT_CPPLOGT_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
656 #define BT_CPPLOGD_STR(_msg) BT_CPPLOGD_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
657 #define BT_CPPLOGI_STR(_msg) BT_CPPLOGI_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
658 #define BT_CPPLOGW_STR(_msg) BT_CPPLOGW_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
659 #define BT_CPPLOGE_STR(_msg) BT_CPPLOGE_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
660 #define BT_CPPLOGF_STR(_msg) BT_CPPLOGF_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
663 * Calls logMemNoThrow() on `_logger` to log using the level `_lvl`
664 * without appending nor throwing.
666 #define BT_CPPLOG_MEM_EX(_lvl, _logger, _mem_data, _mem_len, _fmt, ...) \
668 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
669 (_logger).logMemNoThrow<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \
670 (_fmt), ##__VA_ARGS__); \
675 * BT_CPPLOG_MEM_EX() with specific logging levels.
677 #define BT_CPPLOGT_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
678 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::TRACE, (_logger), (_mem_data), (_mem_len), (_fmt), \
680 #define BT_CPPLOGD_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
681 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::DEBUG, (_logger), (_mem_data), (_mem_len), (_fmt), \
683 #define BT_CPPLOGI_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
684 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::INFO, (_logger), (_mem_data), (_mem_len), (_fmt), \
686 #define BT_CPPLOGW_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
687 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::WARNING, (_logger), (_mem_data), (_mem_len), (_fmt), \
689 #define BT_CPPLOGE_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
690 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::ERROR, (_logger), (_mem_data), (_mem_len), (_fmt), \
692 #define BT_CPPLOGF_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
693 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::FATAL, (_logger), (_mem_data), (_mem_len), (_fmt), \
697 * BT_CPPLOG_MEM_EX() with specific logging levels and using the default
700 #define BT_CPPLOGT_MEM(_mem_data, _mem_len, _fmt, ...) \
701 BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
702 #define BT_CPPLOGD_MEM(_mem_data, _mem_len, _fmt, ...) \
703 BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
704 #define BT_CPPLOGI_MEM(_mem_data, _mem_len, _fmt, ...) \
705 BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
706 #define BT_CPPLOGW_MEM(_mem_data, _mem_len, _fmt, ...) \
707 BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
708 #define BT_CPPLOGE_MEM(_mem_data, _mem_len, _fmt, ...) \
709 BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
710 #define BT_CPPLOGF_MEM(_mem_data, _mem_len, _fmt, ...) \
711 BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
714 * Calls logMemStrNoThrow() on `_logger` to log using the level `_lvl`
715 * without appending nor throwing.
717 #define BT_CPPLOG_MEM_STR_EX(_lvl, _logger, _mem_data, _mem_len, _msg) \
718 (_logger).logMemStrNoThrow<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \
722 * BT_CPPLOG_MEM_STR_EX() with specific logging levels.
724 #define BT_CPPLOGT_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
725 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_mem_data), (_mem_len), (_msg))
726 #define BT_CPPLOGD_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
727 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_mem_data), (_mem_len), (_msg))
728 #define BT_CPPLOGI_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
729 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_mem_data), (_mem_len), (_msg))
730 #define BT_CPPLOGW_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
731 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_mem_data), (_mem_len), (_msg))
732 #define BT_CPPLOGE_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
733 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::ERROR, (_logger), (_mem_data), (_mem_len), (_msg))
734 #define BT_CPPLOGF_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
735 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_mem_data), (_mem_len), (_msg))
738 * BT_CPPLOG_MEM_STR_EX() with specific logging levels and using the
741 #define BT_CPPLOGT_MEM_STR(_mem_data, _mem_len, _msg) \
742 BT_CPPLOGT_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
743 #define BT_CPPLOGD_MEM_STR(_mem_data, _mem_len, _msg) \
744 BT_CPPLOGD_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
745 #define BT_CPPLOGI_MEM_STR(_mem_data, _mem_len, _msg) \
746 BT_CPPLOGI_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
747 #define BT_CPPLOGW_MEM_STR(_mem_data, _mem_len, _msg) \
748 BT_CPPLOGW_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
749 #define BT_CPPLOGE_MEM_STR(_mem_data, _mem_len, _msg) \
750 BT_CPPLOGE_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
751 #define BT_CPPLOGF_MEM_STR(_mem_data, _mem_len, _msg) \
752 BT_CPPLOGF_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
755 * Calls logErrnoNoThrow() on `_logger` to log using the level `_lvl`
756 * and initial message `_init_msg` without appending nor throwing.
758 #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _init_msg, _fmt, ...) \
760 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
761 (_logger).logErrnoNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), \
762 (_fmt), ##__VA_ARGS__); \
767 * BT_CPPLOG_ERRNO_EX() with specific logging levels.
769 #define BT_CPPLOGT_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
770 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::TRACE, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
771 #define BT_CPPLOGD_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
772 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::DEBUG, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
773 #define BT_CPPLOGI_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
774 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::INFO, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
775 #define BT_CPPLOGW_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
776 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::WARNING, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
777 #define BT_CPPLOGE_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
778 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::ERROR, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
779 #define BT_CPPLOGF_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
780 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::FATAL, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
783 * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the
786 #define BT_CPPLOGT_ERRNO(_init_msg, _fmt, ...) \
787 BT_CPPLOGT_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
788 #define BT_CPPLOGD_ERRNO(_init_msg, _fmt, ...) \
789 BT_CPPLOGD_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
790 #define BT_CPPLOGI_ERRNO(_init_msg, _fmt, ...) \
791 BT_CPPLOGI_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
792 #define BT_CPPLOGW_ERRNO(_init_msg, _fmt, ...) \
793 BT_CPPLOGW_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
794 #define BT_CPPLOGE_ERRNO(_init_msg, _fmt, ...) \
795 BT_CPPLOGE_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
796 #define BT_CPPLOGF_ERRNO(_init_msg, _fmt, ...) \
797 BT_CPPLOGF_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
800 * Calls logErrnoStrNoThrow() on `_logger` to log using the level `_lvl`
801 * and initial message `_init_msg` without appending nor throwing.
803 #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _init_msg, _msg) \
804 (_logger).logErrnoStrNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
807 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels.
809 #define BT_CPPLOGT_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
810 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_init_msg), (_msg))
811 #define BT_CPPLOGD_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
812 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_init_msg), (_msg))
813 #define BT_CPPLOGI_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
814 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_init_msg), (_msg))
815 #define BT_CPPLOGW_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
816 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_init_msg), (_msg))
817 #define BT_CPPLOGE_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
818 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::ERROR, (_logger), (_init_msg), (_msg))
819 #define BT_CPPLOGF_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
820 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_init_msg), (_msg))
823 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels and using the
826 #define BT_CPPLOGT_ERRNO_STR(_init_msg, _msg) \
827 BT_CPPLOGT_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
828 #define BT_CPPLOGD_ERRNO_STR(_init_msg, _msg) \
829 BT_CPPLOGD_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
830 #define BT_CPPLOGI_ERRNO_STR(_init_msg, _msg) \
831 BT_CPPLOGI_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
832 #define BT_CPPLOGW_ERRNO_STR(_init_msg, _msg) \
833 BT_CPPLOGW_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
834 #define BT_CPPLOGE_ERRNO_STR(_init_msg, _msg) \
835 BT_CPPLOGE_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
836 #define BT_CPPLOGF_ERRNO_STR(_init_msg, _msg) \
837 BT_CPPLOGF_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
840 * Calls logErrorAndThrow() on `_logger` to log an error, append a cause
841 * to the error of the current thread, and throw an instance of
844 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \
845 (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
848 * Calls logErrorAndThrow() on `_logger` to log an error, append a cause
849 * to the error of the current thread, and throw an instance of
852 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \
853 (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
856 * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger.
858 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \
859 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_fmt), ##__VA_ARGS__)
862 * Calls logErrorStrAndThrow() on `_logger` to log an error, append a
863 * cause to the error of the current thread, and throw an instance of
866 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _msg) \
867 (_logger).logErrorStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_msg))
870 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
873 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _msg) \
874 BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_msg))
877 * Calls logErrorAndRethrow() 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_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \
882 (_logger).logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
885 * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger.
887 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \
888 BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
891 * Calls logErrorStrAndRethrow() on `_logger` to log an error, append a
892 * cause to the error of the current thread, and throw an instance of
895 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \
896 (_logger).logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg))
899 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
902 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \
903 BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
906 * Calls logErrorErrnoAndThrow() 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_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _fmt, ...) \
911 (_logger).logErrorErrnoAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
912 (_fmt), ##__VA_ARGS__)
915 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default
918 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \
919 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
920 (_fmt), ##__VA_ARGS__)
923 * Calls logErrorErrnoStrAndThrow() on `_logger` to log an error, append
924 * a cause to the error of the current thread, and throw an instance of
927 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _msg) \
928 (_logger).logErrorErrnoStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
932 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
935 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _msg) \
936 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
940 * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append
941 * a cause to the error of the current thread, and throw an instance of
944 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _fmt, ...) \
945 (_logger).logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
949 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
952 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_init_msg, _fmt, ...) \
953 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), \
957 * Calls logErrorErrnoStrAndRethrow() on `_logger` to log an error,
958 * append a cause to the error of the current thread, and throw an
959 * instance of `_exc_cls`.
961 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _msg) \
962 (_logger).logErrorErrnoStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
965 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the
968 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW(_init_msg, _msg) \
969 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
971 #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */