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;
596 } /* namespace bt2c */
598 /* Internal: default logger name */
599 #define _BT_CPPLOG_DEF_LOGGER _mLogger
602 * Calls log() on `_logger` to log using the level `_lvl`.
604 #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \
606 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
607 (_logger).log<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__); \
612 * BT_CPPLOG_EX() with specific logging levels.
614 #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \
615 BT_CPPLOG_EX(bt2c::Logger::Level::Trace, (_logger), (_fmt), ##__VA_ARGS__)
616 #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \
617 BT_CPPLOG_EX(bt2c::Logger::Level::Debug, (_logger), (_fmt), ##__VA_ARGS__)
618 #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \
619 BT_CPPLOG_EX(bt2c::Logger::Level::Info, (_logger), (_fmt), ##__VA_ARGS__)
620 #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \
621 BT_CPPLOG_EX(bt2c::Logger::Level::Warning, (_logger), (_fmt), ##__VA_ARGS__)
622 #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \
623 BT_CPPLOG_EX(bt2c::Logger::Level::Error, (_logger), (_fmt), ##__VA_ARGS__)
624 #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \
625 BT_CPPLOG_EX(bt2c::Logger::Level::Fatal, (_logger), (_fmt), ##__VA_ARGS__)
628 * BT_CPPLOG_EX() with specific logging levels and using the default
631 #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
632 #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
633 #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
634 #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
635 #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
636 #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
639 * Calls logStr() on `_logger` to log using the level `_lvl`.
641 #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \
642 (_logger).logStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg))
645 * BT_CPPLOG_STR_EX() with specific logging levels.
647 #define BT_CPPLOGT_STR_SPEC(_logger, _msg) \
648 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_msg))
649 #define BT_CPPLOGD_STR_SPEC(_logger, _msg) \
650 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_msg))
651 #define BT_CPPLOGI_STR_SPEC(_logger, _msg) \
652 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Info, (_logger), (_msg))
653 #define BT_CPPLOGW_STR_SPEC(_logger, _msg) \
654 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_msg))
655 #define BT_CPPLOGE_STR_SPEC(_logger, _msg) \
656 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Error, (_logger), (_msg))
657 #define BT_CPPLOGF_STR_SPEC(_logger, _msg) \
658 BT_CPPLOG_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_msg))
661 * BT_CPPLOG_STR_EX() with specific logging levels and using the default
664 #define BT_CPPLOGT_STR(_msg) BT_CPPLOGT_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
665 #define BT_CPPLOGD_STR(_msg) BT_CPPLOGD_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
666 #define BT_CPPLOGI_STR(_msg) BT_CPPLOGI_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
667 #define BT_CPPLOGW_STR(_msg) BT_CPPLOGW_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
668 #define BT_CPPLOGE_STR(_msg) BT_CPPLOGE_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
669 #define BT_CPPLOGF_STR(_msg) BT_CPPLOGF_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
672 * Calls logMem() on `_logger` to log using the level `_lvl`.
674 #define BT_CPPLOG_MEM_EX(_lvl, _logger, _mem_data, _mem_len, _fmt, ...) \
676 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
677 (_logger).logMem<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \
678 (_fmt), ##__VA_ARGS__); \
683 * BT_CPPLOG_MEM_EX() with specific logging levels.
685 #define BT_CPPLOGT_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
686 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Trace, (_logger), (_mem_data), (_mem_len), (_fmt), \
688 #define BT_CPPLOGD_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
689 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Debug, (_logger), (_mem_data), (_mem_len), (_fmt), \
691 #define BT_CPPLOGI_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
692 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Info, (_logger), (_mem_data), (_mem_len), (_fmt), \
694 #define BT_CPPLOGW_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
695 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Warning, (_logger), (_mem_data), (_mem_len), (_fmt), \
697 #define BT_CPPLOGE_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
698 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_fmt), \
700 #define BT_CPPLOGF_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \
701 BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Fatal, (_logger), (_mem_data), (_mem_len), (_fmt), \
705 * BT_CPPLOG_MEM_EX() with specific logging levels and using the default
708 #define BT_CPPLOGT_MEM(_mem_data, _mem_len, _fmt, ...) \
709 BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
710 #define BT_CPPLOGD_MEM(_mem_data, _mem_len, _fmt, ...) \
711 BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
712 #define BT_CPPLOGI_MEM(_mem_data, _mem_len, _fmt, ...) \
713 BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
714 #define BT_CPPLOGW_MEM(_mem_data, _mem_len, _fmt, ...) \
715 BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
716 #define BT_CPPLOGE_MEM(_mem_data, _mem_len, _fmt, ...) \
717 BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
718 #define BT_CPPLOGF_MEM(_mem_data, _mem_len, _fmt, ...) \
719 BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__)
722 * Calls logMemStr() on `_logger` to log using the level `_lvl`.
724 #define BT_CPPLOG_MEM_STR_EX(_lvl, _logger, _mem_data, _mem_len, _msg) \
725 (_logger).logMemStr<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), (_msg))
728 * BT_CPPLOG_MEM_STR_EX() with specific logging levels.
730 #define BT_CPPLOGT_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
731 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_mem_data), (_mem_len), (_msg))
732 #define BT_CPPLOGD_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
733 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_mem_data), (_mem_len), (_msg))
734 #define BT_CPPLOGI_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
735 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_mem_data), (_mem_len), (_msg))
736 #define BT_CPPLOGW_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
737 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_mem_data), (_mem_len), (_msg))
738 #define BT_CPPLOGE_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
739 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_msg))
740 #define BT_CPPLOGF_MEM_STR_SPEC(_logger, _mem_data, _mem_len, _msg) \
741 BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_mem_data), (_mem_len), (_msg))
744 * BT_CPPLOG_MEM_STR_EX() with specific logging levels and using the
747 #define BT_CPPLOGT_MEM_STR(_mem_data, _mem_len, _msg) \
748 BT_CPPLOGT_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
749 #define BT_CPPLOGD_MEM_STR(_mem_data, _mem_len, _msg) \
750 BT_CPPLOGD_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
751 #define BT_CPPLOGI_MEM_STR(_mem_data, _mem_len, _msg) \
752 BT_CPPLOGI_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
753 #define BT_CPPLOGW_MEM_STR(_mem_data, _mem_len, _msg) \
754 BT_CPPLOGW_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
755 #define BT_CPPLOGE_MEM_STR(_mem_data, _mem_len, _msg) \
756 BT_CPPLOGE_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
757 #define BT_CPPLOGF_MEM_STR(_mem_data, _mem_len, _msg) \
758 BT_CPPLOGF_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_msg))
761 * Calls logErrno() on `_logger` to log using the level `_lvl` and
762 * initial message `_init_msg`.
764 #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _init_msg, _fmt, ...) \
766 if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \
767 (_logger).logErrno<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
773 * BT_CPPLOG_ERRNO_EX() with specific logging levels.
775 #define BT_CPPLOGT_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
776 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
777 #define BT_CPPLOGD_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
778 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
779 #define BT_CPPLOGI_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
780 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
781 #define BT_CPPLOGW_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
782 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
783 #define BT_CPPLOGE_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
784 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
785 #define BT_CPPLOGF_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \
786 BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__)
789 * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the
792 #define BT_CPPLOGT_ERRNO(_init_msg, _fmt, ...) \
793 BT_CPPLOGT_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
794 #define BT_CPPLOGD_ERRNO(_init_msg, _fmt, ...) \
795 BT_CPPLOGD_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
796 #define BT_CPPLOGI_ERRNO(_init_msg, _fmt, ...) \
797 BT_CPPLOGI_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
798 #define BT_CPPLOGW_ERRNO(_init_msg, _fmt, ...) \
799 BT_CPPLOGW_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
800 #define BT_CPPLOGE_ERRNO(_init_msg, _fmt, ...) \
801 BT_CPPLOGE_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
802 #define BT_CPPLOGF_ERRNO(_init_msg, _fmt, ...) \
803 BT_CPPLOGF_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
806 * Calls logErrnoStr() on `_logger` to log using the level `_lvl` and
807 * initial message `_init_msg`.
809 #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _init_msg, _msg) \
810 (_logger).logErrnoStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
813 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels.
815 #define BT_CPPLOGT_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
816 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_msg))
817 #define BT_CPPLOGD_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
818 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_msg))
819 #define BT_CPPLOGI_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
820 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_msg))
821 #define BT_CPPLOGW_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
822 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_msg))
823 #define BT_CPPLOGE_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
824 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_msg))
825 #define BT_CPPLOGF_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \
826 BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_msg))
829 * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels and using the
832 #define BT_CPPLOGT_ERRNO_STR(_init_msg, _msg) \
833 BT_CPPLOGT_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
834 #define BT_CPPLOGD_ERRNO_STR(_init_msg, _msg) \
835 BT_CPPLOGD_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
836 #define BT_CPPLOGI_ERRNO_STR(_init_msg, _msg) \
837 BT_CPPLOGI_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
838 #define BT_CPPLOGW_ERRNO_STR(_init_msg, _msg) \
839 BT_CPPLOGW_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
840 #define BT_CPPLOGE_ERRNO_STR(_init_msg, _msg) \
841 BT_CPPLOGE_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
842 #define BT_CPPLOGF_ERRNO_STR(_init_msg, _msg) \
843 BT_CPPLOGF_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
846 * Calls log() on `_logger` with the `Error` level to log an error and
847 * append a cause to the error of the current thread.
849 #define BT_CPPLOGE_APPEND_CAUSE_SPEC(_logger, _fmt, ...) \
850 (_logger).log<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_fmt), \
854 * BT_CPPLOGE_APPEND_CAUSE_SPEC() using the default logger.
856 #define BT_CPPLOGE_APPEND_CAUSE(_fmt, ...) \
857 BT_CPPLOGE_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
860 * Calls logStr() on `_logger` with the `Error` level to log an error and
861 * append a cause to the error of the current thread.
863 #define BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_logger, _msg) \
864 (_logger).logStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_msg))
867 * BT_CPPLOGE_STR_APPEND_CAUSE_SPEC() using the default logger.
869 #define BT_CPPLOGE_STR_APPEND_CAUSE(_msg) \
870 BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
873 * Calls logErrorAndThrow() on `_logger` to log an error, append a cause
874 * to the error of the current thread, and throw an instance of
877 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \
878 (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
881 * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger.
883 #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \
884 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_fmt), ##__VA_ARGS__)
887 * Calls logErrorStrAndThrow() on `_logger` to log an error, append a
888 * cause to the error of the current thread, and throw an instance of
891 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _msg) \
892 (_logger).logErrorStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_msg))
895 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
898 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _msg) \
899 BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_msg))
902 * Calls logErrorAndRethrow() on `_logger` to log an error, append a
903 * cause to the error of the current thread, and throw an instance of
906 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \
907 (_logger).logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__)
910 * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger.
912 #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \
913 BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__)
916 * Calls logErrorStrAndRethrow() on `_logger` to log an error, append a
917 * cause to the error of the current thread, and throw an instance of
920 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \
921 (_logger).logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg))
924 * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
927 #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \
928 BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg))
931 * Calls logErrno() on `_logger` with the `Level::Error` level to log an
932 * error and append a cause to the error of the current thread.
934 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_logger, _init_msg, _fmt, ...) \
935 (_logger).logErrno<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \
936 (_init_msg), (_fmt), ##__VA_ARGS__)
939 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC() using the default logger.
941 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE(_init_msg, _fmt, ...) \
942 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__)
945 * Calls logErrnoStr() on `_logger` with the `Level::Error` level to log
946 * an error and append a cause to the error of the current thread.
948 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_logger, _init_msg, _msg) \
949 (_logger).logErrnoStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \
953 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC() using the default logger.
955 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE(_init_msg, _msg) \
956 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
959 * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a
960 * cause to the error of the current thread, and throw an instance of
963 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _fmt, ...) \
964 (_logger).logErrorErrnoAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
965 (_fmt), ##__VA_ARGS__)
968 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default
971 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \
972 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
973 (_fmt), ##__VA_ARGS__)
976 * Calls logErrorErrnoStrAndThrow() on `_logger` to log an error, append
977 * a cause to the error of the current thread, and throw an instance of
980 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _msg) \
981 (_logger).logErrorErrnoStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \
985 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default
988 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _msg) \
989 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \
993 * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append
994 * a cause to the error of the current thread, and throw an instance of
997 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _fmt, ...) \
998 (_logger).logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \
1002 * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default
1005 #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_init_msg, _fmt, ...) \
1006 BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), \
1010 * Calls logErrorErrnoStrAndRethrow() on `_logger` to log an error,
1011 * append a cause to the error of the current thread, and throw an
1012 * instance of `_exc_cls`.
1014 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _msg) \
1015 (_logger).logErrorErrnoStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_msg))
1018 * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the
1021 #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW(_init_msg, _msg) \
1022 BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg))
1024 #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */