| 1 | /* |
| 2 | * SPDX-FileCopyrightText: 2023 Philippe Proulx <pproulx@efficios.com> |
| 3 | * SPDX-License-Identifier: MIT |
| 4 | */ |
| 5 | |
| 6 | #ifndef BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP |
| 7 | #define BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP |
| 8 | |
| 9 | #include <cstring> |
| 10 | #include <iterator> |
| 11 | #include <string> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <glib.h> |
| 16 | |
| 17 | #include <babeltrace2/babeltrace.h> |
| 18 | |
| 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" |
| 27 | |
| 28 | namespace bt2c { |
| 29 | |
| 30 | /* |
| 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. |
| 34 | * |
| 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. |
| 40 | * |
| 41 | * The methods above expect a format string and zero or more arguments |
| 42 | * to be formatted with fmt::format(). |
| 43 | */ |
| 44 | class Logger final |
| 45 | { |
| 46 | public: |
| 47 | /* Available log levels */ |
| 48 | enum class Level |
| 49 | { |
| 50 | TRACE = BT_LOG_TRACE, |
| 51 | DEBUG = BT_LOG_DEBUG, |
| 52 | INFO = BT_LOG_INFO, |
| 53 | WARNING = BT_LOG_WARNING, |
| 54 | ERROR = BT_LOG_ERROR, |
| 55 | FATAL = BT_LOG_FATAL, |
| 56 | NONE = BT_LOG_NONE, |
| 57 | }; |
| 58 | |
| 59 | /* |
| 60 | * Builds a logger from the self component class `selfCompCls` using |
| 61 | * the tag `tag` and the logging level of `privQueryExec`. |
| 62 | */ |
| 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)} |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Builds a logger from the self component `selfComp` using the tag |
| 72 | * `tag`. |
| 73 | */ |
| 74 | explicit Logger(const bt2::SelfComponent selfComp, std::string tag) noexcept : |
| 75 | _mSelfComp {selfComp}, _mLevel {static_cast<Level>(selfComp.loggingLevel())}, _mTag { |
| 76 | std::move( |
| 77 | tag)} |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Builds a logger from the self source component `selfComp` using |
| 83 | * the tag `tag`. |
| 84 | */ |
| 85 | explicit Logger(const bt2::SelfSourceComponent selfComp, std::string tag) noexcept : |
| 86 | Logger { |
| 87 | bt2::SelfComponent {bt_self_component_source_as_self_component(selfComp.libObjPtr())}, |
| 88 | std::move(tag)} |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Builds a logger from the self filter component `selfComp` using |
| 94 | * the tag `tag`. |
| 95 | */ |
| 96 | explicit Logger(const bt2::SelfFilterComponent selfComp, std::string tag) noexcept : |
| 97 | Logger { |
| 98 | bt2::SelfComponent {bt_self_component_filter_as_self_component(selfComp.libObjPtr())}, |
| 99 | std::move(tag)} |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Builds a logger from the self sink component `selfComp` using the |
| 105 | * tag `tag`. |
| 106 | */ |
| 107 | explicit Logger(const bt2::SelfSinkComponent selfComp, std::string tag) noexcept : |
| 108 | Logger {bt2::SelfComponent {bt_self_component_sink_as_self_component(selfComp.libObjPtr())}, |
| 109 | std::move(tag)} |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Builds a logger from the self message iterator `selfMsgIter` |
| 115 | * using the tag `tag`. |
| 116 | */ |
| 117 | explicit Logger(const bt2::SelfMessageIterator selfMsgIter, std::string tag) noexcept : |
| 118 | Logger {selfMsgIter.component(), std::move(tag)} |
| 119 | { |
| 120 | _mSelfMsgIter = selfMsgIter; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Builds a logger from the module named `moduleName` using the tag |
| 125 | * `tag` and logging level `logLevel`. |
| 126 | */ |
| 127 | explicit Logger(std::string moduleName, std::string tag, const Level logLevel) noexcept : |
| 128 | _mModuleName {std::move(moduleName)}, _mLevel {logLevel}, _mTag {std::move(tag)} |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Builds a logger from another logger `other` using the new tag |
| 134 | * `newTag`. |
| 135 | */ |
| 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)} |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Current logging level. |
| 144 | */ |
| 145 | Level level() const noexcept |
| 146 | { |
| 147 | return _mLevel; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Current logging level converted to a `bt_log_level` value. |
| 152 | * |
| 153 | * For legacy code. |
| 154 | */ |
| 155 | bt_log_level cLevel() const noexcept |
| 156 | { |
| 157 | return static_cast<bt_log_level>(_mLevel); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Whether or not this logger would log at the level `level`. |
| 162 | */ |
| 163 | bool wouldLog(const Level level) const noexcept |
| 164 | { |
| 165 | return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel)); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Whether or not this logger would log at the trace level. |
| 170 | */ |
| 171 | bool wouldLogT() const noexcept |
| 172 | { |
| 173 | return this->wouldLog(Level::TRACE); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Whether or not this logger would log at the debug level. |
| 178 | */ |
| 179 | bool wouldLogD() const noexcept |
| 180 | { |
| 181 | return this->wouldLog(Level::DEBUG); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Whether or not this logger would log at the info level. |
| 186 | */ |
| 187 | bool wouldLogI() const noexcept |
| 188 | { |
| 189 | return this->wouldLog(Level::INFO); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Whether or not this logger would log at the warning level. |
| 194 | */ |
| 195 | bool wouldLogW() const noexcept |
| 196 | { |
| 197 | return this->wouldLog(Level::WARNING); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Whether or not this logger would log at the error level. |
| 202 | */ |
| 203 | bool wouldLogE() const noexcept |
| 204 | { |
| 205 | return this->wouldLog(Level::ERROR); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Whether or not this logger would log at the fatal level. |
| 210 | */ |
| 211 | bool wouldLogF() const noexcept |
| 212 | { |
| 213 | return this->wouldLog(Level::FATAL); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Logging tag. |
| 218 | */ |
| 219 | const std::string& tag() const noexcept |
| 220 | { |
| 221 | return _mTag; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Self component class actor, or `bt2s::nullopt` if none. |
| 226 | */ |
| 227 | const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept |
| 228 | { |
| 229 | return _mSelfCompCls; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Self component actor, or `bt2s::nullopt` if none. |
| 234 | */ |
| 235 | const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept |
| 236 | { |
| 237 | return _mSelfComp; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Self message iterator actor, or `bt2s::nullopt` if none. |
| 242 | */ |
| 243 | const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept |
| 244 | { |
| 245 | return _mSelfMsgIter; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Name of module actor, or `bt2s::nullopt` if none. |
| 250 | */ |
| 251 | const bt2s::optional<std::string>& moduleName() const noexcept |
| 252 | { |
| 253 | return _mModuleName; |
| 254 | } |
| 255 | |
| 256 | private: |
| 257 | struct _StdLogWriter final |
| 258 | { |
| 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 |
| 263 | { |
| 264 | BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0); |
| 265 | bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg); |
| 266 | } |
| 267 | }; |
| 268 | |
| 269 | public: |
| 270 | /* |
| 271 | * Logs using the level `LevelV`. |
| 272 | * |
| 273 | * This method forwards `fmt` and `args` to fmt::format() to create |
| 274 | * the log message. |
| 275 | * |
| 276 | * If `AppendCauseV` is true, this method also appends a cause to |
| 277 | * the error of the current thread using the same message. |
| 278 | */ |
| 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 |
| 282 | { |
| 283 | this->_logNoThrow<_StdLogWriter, LevelV, AppendCauseV>( |
| 284 | fileName, funcName, lineNo, nullptr, 0, "", fmt, std::forward<ArgTs>(args)...); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Logs `msg` using the level `LevelV`. |
| 289 | * |
| 290 | * If `AppendCauseV` is true, this method also appends a cause to |
| 291 | * the error of the current thread using the same message. |
| 292 | */ |
| 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 |
| 296 | { |
| 297 | this->_logStrNoThrow<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, |
| 298 | nullptr, 0, "", msg); |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Like logAndNoThrow() with the `Level::ERROR` level, but also |
| 303 | * throws a default-constructed instance of `ExcT`. |
| 304 | */ |
| 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 |
| 309 | { |
| 310 | this->logNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, fmt, |
| 311 | std::forward<ArgTs>(args)...); |
| 312 | throw ExcT {}; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Like logStrAndNoThrow() with the `Level::ERROR` level, but also |
| 317 | * throws a default-constructed instance of `ExcT`. |
| 318 | */ |
| 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 |
| 322 | { |
| 323 | this->logStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, msg); |
| 324 | throw ExcT {}; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Like logAndNoThrow() with the `Level::ERROR` level, but also |
| 329 | * rethrows. |
| 330 | */ |
| 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 |
| 335 | { |
| 336 | this->logNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, fmt, |
| 337 | std::forward<ArgTs>(args)...); |
| 338 | throw; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Like logStrAndNoThrow() with the `Level::ERROR` level, but also |
| 343 | * rethrows. |
| 344 | */ |
| 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 |
| 349 | { |
| 350 | this->logStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, msg); |
| 351 | throw; |
| 352 | } |
| 353 | |
| 354 | private: |
| 355 | struct _InitMsgLogWriter final |
| 356 | { |
| 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 |
| 361 | { |
| 362 | bt_log_write_printf(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag, |
| 363 | "%s%s", initMsg, msg); |
| 364 | } |
| 365 | }; |
| 366 | |
| 367 | public: |
| 368 | /* |
| 369 | * Logs the message of `errno` using the level `LevelV`. |
| 370 | * |
| 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`. |
| 374 | * |
| 375 | * If `AppendCauseV` is true, this method also appends a cause to |
| 376 | * the error of the current thread using the same message. |
| 377 | */ |
| 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 |
| 382 | { |
| 383 | this->_logNoThrow<_InitMsgLogWriter, LevelV, AppendCauseV>( |
| 384 | fileName, funcName, lineNo, nullptr, 0, this->_errnoIntroStr(initMsg).c_str(), fmt, |
| 385 | std::forward<ArgTs>(args)...); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Logs the message of `errno` using the level `LevelV`. |
| 390 | * |
| 391 | * The log message starts with `initMsg`, is followed with the |
| 392 | * message for `errno`, and then with `msg`. |
| 393 | * |
| 394 | * If `AppendCauseV` is true, this method also appends a cause to |
| 395 | * the error of the current thread using the same message. |
| 396 | */ |
| 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 |
| 401 | { |
| 402 | this->_logStrNoThrow<_InitMsgLogWriter, LevelV, AppendCauseV>( |
| 403 | fileName, funcName, lineNo, nullptr, 0, this->_errnoIntroStr(initMsg).c_str(), msg); |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Like logErrnoNoThrow() with the `Level::ERROR` level, but also |
| 408 | * throws a default-constructed instance of `ExcT`. |
| 409 | */ |
| 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 |
| 415 | { |
| 416 | this->logErrnoNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt, |
| 417 | std::forward<ArgTs>(args)...); |
| 418 | throw ExcT {}; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Like logErrnoStrNoThrow() with the `Level::ERROR` level, but also |
| 423 | * throws a default-constructed instance of `ExcT`. |
| 424 | */ |
| 425 | template <bool AppendCauseV, typename ExcT> |
| 426 | [[noreturn]] void |
| 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 |
| 430 | { |
| 431 | this->logErrnoStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg, |
| 432 | msg); |
| 433 | throw ExcT {}; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Like logErrnoNoThrow() with the `Level::ERROR` level, but also |
| 438 | * rethrows. |
| 439 | */ |
| 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 |
| 445 | { |
| 446 | this->logErrnoNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt, |
| 447 | std::forward<ArgTs>(args)...); |
| 448 | throw; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Like logErrnoStrNoThrow() with the `Level::ERROR` level, but also |
| 453 | * rethrows. |
| 454 | */ |
| 455 | template <bool AppendCauseV> |
| 456 | [[noreturn]] void |
| 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 |
| 460 | { |
| 461 | this->logErrnoStrNoThrow<Level::ERROR, AppendCauseV>(fileName, funcName, lineNo, initMsg, |
| 462 | msg); |
| 463 | throw; |
| 464 | } |
| 465 | |
| 466 | private: |
| 467 | struct _MemLogWriter final |
| 468 | { |
| 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 |
| 473 | { |
| 474 | bt_log_write_mem(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag, |
| 475 | memData, memLen, msg); |
| 476 | } |
| 477 | }; |
| 478 | |
| 479 | public: |
| 480 | /* |
| 481 | * Logs memory data using the level `LevelV`. |
| 482 | * |
| 483 | * This method forwards `fmt` and `args` to fmt::format() to create |
| 484 | * the log message. |
| 485 | */ |
| 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 |
| 490 | { |
| 491 | this->_logNoThrow<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, |
| 492 | "", fmt, std::forward<ArgTs>(args)...); |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Logs memory data using the level `LevelV`, starting with the |
| 497 | * message `msg`. |
| 498 | */ |
| 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 |
| 503 | { |
| 504 | this->_logStrNoThrow<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, |
| 505 | memLen, "", msg); |
| 506 | } |
| 507 | |
| 508 | private: |
| 509 | /* |
| 510 | * Formats a log message with fmt::format() given `fmt` and `args`, |
| 511 | * and then forwards everything to _logStrNoThrow(). |
| 512 | */ |
| 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 |
| 518 | { |
| 519 | /* Only format arguments if logging or appending an error cause */ |
| 520 | if (G_UNLIKELY(this->wouldLog(LevelV) || AppendCauseV)) { |
| 521 | /* |
| 522 | * Format arguments to our buffer (fmt::format_to() doesn't |
| 523 | * append a null character). |
| 524 | */ |
| 525 | _mBuf.clear(); |
| 526 | fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...); |
| 527 | _mBuf.push_back('\0'); |
| 528 | } |
| 529 | |
| 530 | this->_logStrNoThrow<LogWriterT, LevelV, AppendCauseV>(fileName, funcName, lineNo, memData, |
| 531 | memLen, initMsg, _mBuf.data()); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Calls LogWriterT::write() with its arguments to log using the |
| 536 | * level `LevelV`. |
| 537 | * |
| 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. |
| 541 | */ |
| 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 |
| 547 | { |
| 548 | /* Initial message and main message are required */ |
| 549 | BT_ASSERT(initMsg); |
| 550 | BT_ASSERT(msg); |
| 551 | |
| 552 | /* Log if needed */ |
| 553 | if (this->wouldLog(LevelV)) { |
| 554 | LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, memLen, |
| 555 | initMsg, msg); |
| 556 | } |
| 557 | |
| 558 | /* Append an error cause if needed */ |
| 559 | if (AppendCauseV) { |
| 560 | if (_mSelfMsgIter) { |
| 561 | bt_current_thread_error_append_cause_from_message_iterator( |
| 562 | _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); |
| 563 | } else if (_mSelfComp) { |
| 564 | bt_current_thread_error_append_cause_from_component( |
| 565 | _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); |
| 566 | } else if (_mSelfCompCls) { |
| 567 | bt_current_thread_error_append_cause_from_component_class( |
| 568 | _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); |
| 569 | } else { |
| 570 | BT_ASSERT(_mModuleName); |
| 571 | bt_current_thread_error_append_cause_from_unknown( |
| 572 | _mModuleName->data(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | static std::string _errnoIntroStr(const char * const initMsg) |
| 578 | { |
| 579 | BT_ASSERT(errno != 0); |
| 580 | return fmt::format("{}: {}", initMsg, g_strerror(errno)); |
| 581 | } |
| 582 | |
| 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; |
| 588 | |
| 589 | /* Current logging level */ |
| 590 | Level _mLevel; |
| 591 | |
| 592 | /* Logging tag */ |
| 593 | std::string _mTag; |
| 594 | |
| 595 | /* Formatting buffer */ |
| 596 | mutable std::vector<char> _mBuf; |
| 597 | }; |
| 598 | |
| 599 | } /* namespace bt2c */ |
| 600 | |
| 601 | /* Internal: default logger name */ |
| 602 | #define _BT_CPPLOG_DEF_LOGGER _mLogger |
| 603 | |
| 604 | /* |
| 605 | * Calls logNoThrow() on `_logger` to log using the level `_lvl` without |
| 606 | * appending nor throwing. |
| 607 | */ |
| 608 | #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \ |
| 609 | do { \ |
| 610 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ |
| 611 | (_logger).logNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), \ |
| 612 | ##__VA_ARGS__); \ |
| 613 | } \ |
| 614 | } while (0) |
| 615 | |
| 616 | /* |
| 617 | * BT_CPPLOG_EX() with specific logging levels. |
| 618 | */ |
| 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__) |
| 631 | |
| 632 | /* |
| 633 | * BT_CPPLOG_EX() with specific logging levels and using the default |
| 634 | * logger. |
| 635 | */ |
| 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__) |
| 642 | |
| 643 | /* |
| 644 | * Calls logStrNoThrow() on `_logger` to log using the level `_lvl` |
| 645 | * without appending nor throwing. |
| 646 | */ |
| 647 | #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \ |
| 648 | (_logger).logStrNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg)) |
| 649 | |
| 650 | /* |
| 651 | * BT_CPPLOG_STR_EX() with specific logging levels. |
| 652 | */ |
| 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)) |
| 665 | |
| 666 | /* |
| 667 | * BT_CPPLOG_STR_EX() with specific logging levels and using the default |
| 668 | * logger. |
| 669 | */ |
| 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)) |
| 676 | |
| 677 | /* |
| 678 | * Calls logMemNoThrow() on `_logger` to log using the level `_lvl` |
| 679 | * without appending nor throwing. |
| 680 | */ |
| 681 | #define BT_CPPLOG_MEM_EX(_lvl, _logger, _mem_data, _mem_len, _fmt, ...) \ |
| 682 | do { \ |
| 683 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ |
| 684 | (_logger).logMemNoThrow<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \ |
| 685 | (_fmt), ##__VA_ARGS__); \ |
| 686 | } \ |
| 687 | } while (0) |
| 688 | |
| 689 | /* |
| 690 | * BT_CPPLOG_MEM_EX() with specific logging levels. |
| 691 | */ |
| 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), \ |
| 694 | ##__VA_ARGS__) |
| 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), \ |
| 697 | ##__VA_ARGS__) |
| 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), \ |
| 700 | ##__VA_ARGS__) |
| 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), \ |
| 703 | ##__VA_ARGS__) |
| 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), \ |
| 706 | ##__VA_ARGS__) |
| 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), \ |
| 709 | ##__VA_ARGS__) |
| 710 | |
| 711 | /* |
| 712 | * BT_CPPLOG_MEM_EX() with specific logging levels and using the default |
| 713 | * logger. |
| 714 | */ |
| 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__) |
| 727 | |
| 728 | /* |
| 729 | * Calls logMemStrNoThrow() on `_logger` to log using the level `_lvl` |
| 730 | * without appending nor throwing. |
| 731 | */ |
| 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), \ |
| 734 | (_msg)) |
| 735 | |
| 736 | /* |
| 737 | * BT_CPPLOG_MEM_STR_EX() with specific logging levels. |
| 738 | */ |
| 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)) |
| 751 | |
| 752 | /* |
| 753 | * BT_CPPLOG_MEM_STR_EX() with specific logging levels and using the |
| 754 | * default logger. |
| 755 | */ |
| 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)) |
| 768 | |
| 769 | /* |
| 770 | * Calls logErrnoNoThrow() on `_logger` to log using the level `_lvl` |
| 771 | * and initial message `_init_msg` without appending nor throwing. |
| 772 | */ |
| 773 | #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _init_msg, _fmt, ...) \ |
| 774 | do { \ |
| 775 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ |
| 776 | (_logger).logErrnoNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), \ |
| 777 | (_fmt), ##__VA_ARGS__); \ |
| 778 | } \ |
| 779 | } while (0) |
| 780 | |
| 781 | /* |
| 782 | * BT_CPPLOG_ERRNO_EX() with specific logging levels. |
| 783 | */ |
| 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__) |
| 796 | |
| 797 | /* |
| 798 | * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the |
| 799 | * default logger. |
| 800 | */ |
| 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__) |
| 813 | |
| 814 | /* |
| 815 | * Calls logErrnoStrNoThrow() on `_logger` to log using the level `_lvl` |
| 816 | * and initial message `_init_msg` without appending nor throwing. |
| 817 | */ |
| 818 | #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _init_msg, _msg) \ |
| 819 | (_logger).logErrnoStrNoThrow<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_msg)) |
| 820 | |
| 821 | /* |
| 822 | * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels. |
| 823 | */ |
| 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)) |
| 836 | |
| 837 | /* |
| 838 | * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels and using the |
| 839 | * default logger. |
| 840 | */ |
| 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)) |
| 853 | |
| 854 | /* |
| 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 |
| 857 | * `_exc_cls`. |
| 858 | */ |
| 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__) |
| 861 | |
| 862 | /* |
| 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 |
| 865 | * `_exc_cls`. |
| 866 | */ |
| 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__) |
| 869 | |
| 870 | /* |
| 871 | * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger. |
| 872 | */ |
| 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__) |
| 875 | |
| 876 | /* |
| 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 |
| 879 | * `_exc_cls`. |
| 880 | */ |
| 881 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _msg) \ |
| 882 | (_logger).logErrorStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_msg)) |
| 883 | |
| 884 | /* |
| 885 | * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default |
| 886 | * logger. |
| 887 | */ |
| 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)) |
| 890 | |
| 891 | /* |
| 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 |
| 894 | * `_exc_cls`. |
| 895 | */ |
| 896 | #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \ |
| 897 | (_logger).logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__) |
| 898 | |
| 899 | /* |
| 900 | * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger. |
| 901 | */ |
| 902 | #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \ |
| 903 | BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) |
| 904 | |
| 905 | /* |
| 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 |
| 908 | * `_exc_cls`. |
| 909 | */ |
| 910 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \ |
| 911 | (_logger).logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg)) |
| 912 | |
| 913 | /* |
| 914 | * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default |
| 915 | * logger. |
| 916 | */ |
| 917 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \ |
| 918 | BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) |
| 919 | |
| 920 | /* |
| 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 |
| 923 | * `_exc_cls`. |
| 924 | */ |
| 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__) |
| 928 | |
| 929 | /* |
| 930 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default |
| 931 | * logger. |
| 932 | */ |
| 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__) |
| 936 | |
| 937 | /* |
| 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 |
| 940 | * `_exc_cls`. |
| 941 | */ |
| 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), \ |
| 944 | (_msg)) |
| 945 | |
| 946 | /* |
| 947 | * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default |
| 948 | * logger. |
| 949 | */ |
| 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), \ |
| 952 | (_msg)) |
| 953 | |
| 954 | /* |
| 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 |
| 957 | * `_exc_cls`. |
| 958 | */ |
| 959 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _fmt, ...) \ |
| 960 | (_logger).logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \ |
| 961 | ##__VA_ARGS__) |
| 962 | |
| 963 | /* |
| 964 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default |
| 965 | * logger. |
| 966 | */ |
| 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), \ |
| 969 | ##__VA_ARGS__) |
| 970 | |
| 971 | /* |
| 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`. |
| 975 | */ |
| 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)) |
| 978 | |
| 979 | /* |
| 980 | * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the |
| 981 | * default logger. |
| 982 | */ |
| 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)) |
| 985 | |
| 986 | #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */ |