Commit | Line | Data |
---|---|---|
7a72f18a PP |
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 | ||
d88930bc PP |
15 | #include <glib.h> |
16 | ||
7a72f18a PP |
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" | |
53e1ecce | 25 | #include "cpp-common/bt2s/span.hpp" |
7a72f18a | 26 | #include "cpp-common/vendor/fmt/core.h" |
644c6ca7 | 27 | #include "cpp-common/vendor/wise-enum/wise_enum.h" |
7a72f18a PP |
28 | #include "logging/log-api.h" |
29 | ||
30 | namespace bt2c { | |
31 | ||
32 | /* | |
33 | * A logger contains an actor (self component class, self component, | |
34 | * self message iterator, or simple module name), a current logging | |
35 | * level, and a logging tag. | |
36 | * | |
9d15c577 SM |
37 | * It offers the log(), logMem(), logErrno(), logErrorAndThrow(), |
38 | * logErrorAndRethrow(), logErrorErrnoAndThrow(), and | |
39 | * logErrorErrnoAndRethrow() method templates to log using a given | |
7a72f18a PP |
40 | * level, optionally append a cause to the error of the current thread |
41 | * using the correct actor, and optionally throw or rethrow. | |
42 | * | |
43 | * The methods above expect a format string and zero or more arguments | |
44 | * to be formatted with fmt::format(). | |
45 | */ | |
46 | class Logger final | |
47 | { | |
48 | public: | |
53e1ecce PP |
49 | using MemData = bt2s::span<const std::uint8_t>; |
50 | ||
644c6ca7 SM |
51 | /* clang-format off */ |
52 | ||
7a72f18a | 53 | /* Available log levels */ |
644c6ca7 | 54 | WISE_ENUM_CLASS_MEMBER(Level, |
24ce7ecb PP |
55 | (Trace, BT_LOG_TRACE), |
56 | (Debug, BT_LOG_DEBUG), | |
57 | (Info, BT_LOG_INFO), | |
58 | (Warning, BT_LOG_WARNING), | |
59 | (Error, BT_LOG_ERROR), | |
60 | (Fatal, BT_LOG_FATAL), | |
61 | (None, BT_LOG_NONE) | |
62 | ) | |
644c6ca7 SM |
63 | |
64 | /* clang-format on */ | |
7a72f18a PP |
65 | |
66 | /* | |
67 | * Builds a logger from the self component class `selfCompCls` using | |
68 | * the tag `tag` and the logging level of `privQueryExec`. | |
69 | */ | |
70 | explicit Logger(const bt2::SelfComponentClass selfCompCls, | |
71 | const bt2::PrivateQueryExecutor privQueryExec, std::string tag) noexcept : | |
72 | _mSelfCompCls {selfCompCls}, | |
73 | _mLevel {static_cast<Level>(privQueryExec.loggingLevel())}, _mTag {std::move(tag)} | |
74 | { | |
75 | } | |
76 | ||
77 | /* | |
78 | * Builds a logger from the self component `selfComp` using the tag | |
79 | * `tag`. | |
80 | */ | |
81 | explicit Logger(const bt2::SelfComponent selfComp, std::string tag) noexcept : | |
82 | _mSelfComp {selfComp}, _mLevel {static_cast<Level>(selfComp.loggingLevel())}, _mTag { | |
83 | std::move( | |
84 | tag)} | |
85 | { | |
86 | } | |
87 | ||
88 | /* | |
89 | * Builds a logger from the self source component `selfComp` using | |
90 | * the tag `tag`. | |
91 | */ | |
92 | explicit Logger(const bt2::SelfSourceComponent selfComp, std::string tag) noexcept : | |
93 | Logger { | |
94 | bt2::SelfComponent {bt_self_component_source_as_self_component(selfComp.libObjPtr())}, | |
95 | std::move(tag)} | |
96 | { | |
97 | } | |
98 | ||
99 | /* | |
100 | * Builds a logger from the self filter component `selfComp` using | |
101 | * the tag `tag`. | |
102 | */ | |
103 | explicit Logger(const bt2::SelfFilterComponent selfComp, std::string tag) noexcept : | |
104 | Logger { | |
105 | bt2::SelfComponent {bt_self_component_filter_as_self_component(selfComp.libObjPtr())}, | |
106 | std::move(tag)} | |
107 | { | |
108 | } | |
109 | ||
110 | /* | |
111 | * Builds a logger from the self sink component `selfComp` using the | |
112 | * tag `tag`. | |
113 | */ | |
114 | explicit Logger(const bt2::SelfSinkComponent selfComp, std::string tag) noexcept : | |
115 | Logger {bt2::SelfComponent {bt_self_component_sink_as_self_component(selfComp.libObjPtr())}, | |
116 | std::move(tag)} | |
117 | { | |
118 | } | |
119 | ||
120 | /* | |
121 | * Builds a logger from the self message iterator `selfMsgIter` | |
122 | * using the tag `tag`. | |
123 | */ | |
124 | explicit Logger(const bt2::SelfMessageIterator selfMsgIter, std::string tag) noexcept : | |
4f235e19 SM |
125 | _mSelfMsgIter {selfMsgIter}, |
126 | _mLevel {static_cast<Level>(selfMsgIter.component().loggingLevel())}, _mTag {std::move(tag)} | |
7a72f18a | 127 | { |
7a72f18a PP |
128 | } |
129 | ||
130 | /* | |
131 | * Builds a logger from the module named `moduleName` using the tag | |
132 | * `tag` and logging level `logLevel`. | |
133 | */ | |
134 | explicit Logger(std::string moduleName, std::string tag, const Level logLevel) noexcept : | |
135 | _mModuleName {std::move(moduleName)}, _mLevel {logLevel}, _mTag {std::move(tag)} | |
136 | { | |
137 | } | |
138 | ||
139 | /* | |
140 | * Builds a logger from another logger `other` using the new tag | |
141 | * `newTag`. | |
142 | */ | |
143 | explicit Logger(const Logger& other, std::string newTag) : | |
eee15e59 SM |
144 | _mSelfCompCls {other._mSelfCompCls}, _mSelfComp {other._mSelfComp}, |
145 | _mSelfMsgIter {other._mSelfMsgIter}, | |
7a72f18a PP |
146 | _mModuleName {other._mModuleName}, _mLevel {other._mLevel}, _mTag {std::move(newTag)} |
147 | { | |
148 | } | |
149 | ||
150 | /* | |
151 | * Current logging level. | |
152 | */ | |
153 | Level level() const noexcept | |
154 | { | |
155 | return _mLevel; | |
156 | } | |
157 | ||
7a72f18a PP |
158 | /* |
159 | * Whether or not this logger would log at the level `level`. | |
160 | */ | |
161 | bool wouldLog(const Level level) const noexcept | |
162 | { | |
163 | return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel)); | |
164 | } | |
165 | ||
166 | /* | |
167 | * Whether or not this logger would log at the trace level. | |
168 | */ | |
169 | bool wouldLogT() const noexcept | |
170 | { | |
1c5ea5eb | 171 | return this->wouldLog(Level::Trace); |
7a72f18a PP |
172 | } |
173 | ||
174 | /* | |
175 | * Whether or not this logger would log at the debug level. | |
176 | */ | |
177 | bool wouldLogD() const noexcept | |
178 | { | |
1c5ea5eb | 179 | return this->wouldLog(Level::Debug); |
7a72f18a PP |
180 | } |
181 | ||
182 | /* | |
183 | * Whether or not this logger would log at the info level. | |
184 | */ | |
185 | bool wouldLogI() const noexcept | |
186 | { | |
1c5ea5eb | 187 | return this->wouldLog(Level::Info); |
7a72f18a PP |
188 | } |
189 | ||
190 | /* | |
191 | * Whether or not this logger would log at the warning level. | |
192 | */ | |
193 | bool wouldLogW() const noexcept | |
194 | { | |
1c5ea5eb | 195 | return this->wouldLog(Level::Warning); |
7a72f18a PP |
196 | } |
197 | ||
198 | /* | |
199 | * Whether or not this logger would log at the error level. | |
200 | */ | |
201 | bool wouldLogE() const noexcept | |
202 | { | |
1c5ea5eb | 203 | return this->wouldLog(Level::Error); |
7a72f18a PP |
204 | } |
205 | ||
206 | /* | |
207 | * Whether or not this logger would log at the fatal level. | |
208 | */ | |
209 | bool wouldLogF() const noexcept | |
210 | { | |
1c5ea5eb | 211 | return this->wouldLog(Level::Fatal); |
7a72f18a PP |
212 | } |
213 | ||
214 | /* | |
215 | * Logging tag. | |
216 | */ | |
217 | const std::string& tag() const noexcept | |
218 | { | |
219 | return _mTag; | |
220 | } | |
221 | ||
222 | /* | |
223 | * Self component class actor, or `bt2s::nullopt` if none. | |
224 | */ | |
225 | const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept | |
226 | { | |
227 | return _mSelfCompCls; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Self component actor, or `bt2s::nullopt` if none. | |
232 | */ | |
233 | const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept | |
234 | { | |
235 | return _mSelfComp; | |
236 | } | |
237 | ||
238 | /* | |
239 | * Self message iterator actor, or `bt2s::nullopt` if none. | |
240 | */ | |
241 | const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept | |
242 | { | |
243 | return _mSelfMsgIter; | |
244 | } | |
245 | ||
246 | /* | |
247 | * Name of module actor, or `bt2s::nullopt` if none. | |
248 | */ | |
249 | const bt2s::optional<std::string>& moduleName() const noexcept | |
250 | { | |
251 | return _mModuleName; | |
252 | } | |
253 | ||
254 | private: | |
255 | struct _StdLogWriter final | |
256 | { | |
257 | static void write(const char * const fileName, const char * const funcName, | |
53e1ecce PP |
258 | const unsigned lineNo, const Level level, const char * const tag, MemData, |
259 | const char * const initMsg, const char * const msg) noexcept | |
7a72f18a | 260 | { |
d88930bc | 261 | BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0); |
7a72f18a PP |
262 | bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg); |
263 | } | |
264 | }; | |
265 | ||
266 | public: | |
267 | /* | |
268 | * Logs using the level `LevelV`. | |
269 | * | |
270 | * This method forwards `fmt` and `args` to fmt::format() to create | |
271 | * the log message. | |
272 | * | |
273 | * If `AppendCauseV` is true, this method also appends a cause to | |
274 | * the error of the current thread using the same message. | |
275 | */ | |
276 | template <Level LevelV, bool AppendCauseV, typename... ArgTs> | |
9d15c577 SM |
277 | void log(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
278 | const char * const fmt, ArgTs&&...args) const | |
7a72f18a | 279 | { |
53e1ecce PP |
280 | this->_log<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, {}, "", fmt, |
281 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
282 | } |
283 | ||
7a72f18a | 284 | /* |
9d15c577 SM |
285 | * Like log() with the `Level::Error` level, but also throws a |
286 | * default-constructed instance of `ExcT`. | |
7a72f18a PP |
287 | */ |
288 | template <bool AppendCauseV, typename ExcT, typename... ArgTs> | |
289 | [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName, | |
290 | const unsigned int lineNo, const char * const fmt, | |
291 | ArgTs&&...args) const | |
292 | { | |
9d15c577 SM |
293 | this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt, |
294 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
295 | throw ExcT {}; |
296 | } | |
297 | ||
7a72f18a | 298 | /* |
9d15c577 | 299 | * Like log() with the `Level::Error` level, but also rethrows. |
7a72f18a PP |
300 | */ |
301 | template <bool AppendCauseV, typename... ArgTs> | |
302 | [[noreturn]] void logErrorAndRethrow(const char * const fileName, const char * const funcName, | |
303 | const unsigned int lineNo, const char * const fmt, | |
304 | ArgTs&&...args) const | |
305 | { | |
9d15c577 SM |
306 | this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt, |
307 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
308 | throw; |
309 | } | |
310 | ||
7a72f18a | 311 | private: |
d88930bc | 312 | struct _InitMsgLogWriter final |
7a72f18a PP |
313 | { |
314 | static void write(const char * const fileName, const char * const funcName, | |
53e1ecce PP |
315 | const unsigned lineNo, const Level level, const char * const tag, MemData, |
316 | const char * const initMsg, const char * const msg) noexcept | |
7a72f18a | 317 | { |
d88930bc PP |
318 | bt_log_write_printf(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag, |
319 | "%s%s", initMsg, msg); | |
7a72f18a PP |
320 | } |
321 | }; | |
322 | ||
323 | public: | |
324 | /* | |
325 | * Logs the message of `errno` using the level `LevelV`. | |
326 | * | |
327 | * The log message starts with `initMsg`, is followed with the | |
328 | * message for `errno`, and then with what fmt::format() creates | |
329 | * given `fmt` and `args`. | |
330 | * | |
331 | * If `AppendCauseV` is true, this method also appends a cause to | |
332 | * the error of the current thread using the same message. | |
333 | */ | |
334 | template <Level LevelV, bool AppendCauseV, typename... ArgTs> | |
9d15c577 SM |
335 | void logErrno(const char * const fileName, const char * const funcName, |
336 | const unsigned int lineNo, const char * const initMsg, const char * const fmt, | |
337 | ArgTs&&...args) const | |
7a72f18a | 338 | { |
53e1ecce | 339 | this->_log<_InitMsgLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, {}, |
9d15c577 SM |
340 | this->_errnoIntroStr(initMsg).c_str(), |
341 | fmt, std::forward<ArgTs>(args)...); | |
7a72f18a PP |
342 | } |
343 | ||
7a72f18a | 344 | /* |
9d15c577 SM |
345 | * Like logErrno() with the `Level::Error` level, but also throws a |
346 | * default-constructed instance of `ExcT`. | |
7a72f18a PP |
347 | */ |
348 | template <bool AppendCauseV, typename ExcT, typename... ArgTs> | |
349 | [[noreturn]] void logErrorErrnoAndThrow(const char * const fileName, | |
350 | const char * const funcName, const unsigned int lineNo, | |
351 | const char * const initMsg, const char * const fmt, | |
352 | ArgTs&&...args) const | |
353 | { | |
9d15c577 SM |
354 | this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt, |
355 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
356 | throw ExcT {}; |
357 | } | |
358 | ||
7a72f18a | 359 | /* |
9d15c577 | 360 | * Like logErrno() with the `Level::Error` level, but also rethrows. |
7a72f18a PP |
361 | */ |
362 | template <bool AppendCauseV, typename... ArgTs> | |
363 | [[noreturn]] void logErrorErrnoAndRethrow(const char * const fileName, | |
364 | const char * const funcName, | |
365 | const unsigned int lineNo, const char * const initMsg, | |
366 | const char * const fmt, ArgTs&&...args) const | |
367 | { | |
9d15c577 SM |
368 | this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt, |
369 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
370 | throw; |
371 | } | |
372 | ||
7a72f18a PP |
373 | private: |
374 | struct _MemLogWriter final | |
375 | { | |
376 | static void write(const char * const fileName, const char * const funcName, | |
377 | const unsigned lineNo, const Level level, const char * const tag, | |
53e1ecce | 378 | const MemData memData, const char *, const char * const msg) noexcept |
7a72f18a PP |
379 | { |
380 | bt_log_write_mem(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag, | |
53e1ecce | 381 | memData.data(), memData.size(), msg); |
7a72f18a PP |
382 | } |
383 | }; | |
384 | ||
385 | public: | |
386 | /* | |
387 | * Logs memory data using the level `LevelV`. | |
388 | * | |
389 | * This method forwards `fmt` and `args` to fmt::format() to create | |
390 | * the log message. | |
391 | */ | |
392 | template <Level LevelV, typename... ArgTs> | |
9d15c577 | 393 | void logMem(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
53e1ecce | 394 | const MemData memData, const char * const fmt, ArgTs&&...args) const |
7a72f18a | 395 | { |
53e1ecce PP |
396 | this->_log<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, "", fmt, |
397 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
398 | } |
399 | ||
7a72f18a PP |
400 | private: |
401 | /* | |
402 | * Formats a log message with fmt::format() given `fmt` and `args`, | |
e27adb90 PP |
403 | * and then: |
404 | * | |
405 | * 1. Calls LogWriterT::write() with its arguments to log using the | |
406 | * level `LevelV`. | |
407 | * | |
408 | * 2. If `AppendCauseV` is true, this method also appends a cause to | |
409 | * the error of the current thread using the concatenation of | |
410 | * `initMsg` and `msg` as the message. | |
7a72f18a PP |
411 | */ |
412 | template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs> | |
9d15c577 | 413 | void _log(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
53e1ecce PP |
414 | const MemData memData, const char * const initMsg, const char * const fmt, |
415 | ArgTs&&...args) const | |
7a72f18a | 416 | { |
e27adb90 PP |
417 | const auto wouldLog = this->wouldLog(LevelV); |
418 | ||
7a72f18a | 419 | /* Only format arguments if logging or appending an error cause */ |
e27adb90 | 420 | if (G_UNLIKELY(wouldLog || AppendCauseV)) { |
7a72f18a PP |
421 | /* |
422 | * Format arguments to our buffer (fmt::format_to() doesn't | |
423 | * append a null character). | |
424 | */ | |
425 | _mBuf.clear(); | |
e27adb90 | 426 | BT_ASSERT(fmt); |
7a72f18a PP |
427 | fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...); |
428 | _mBuf.push_back('\0'); | |
429 | } | |
430 | ||
e27adb90 | 431 | /* Initial message is required */ |
d88930bc | 432 | BT_ASSERT(initMsg); |
d88930bc | 433 | |
7a72f18a | 434 | /* Log if needed */ |
e27adb90 | 435 | if (wouldLog) { |
53e1ecce | 436 | LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, initMsg, |
e27adb90 | 437 | _mBuf.data()); |
7a72f18a PP |
438 | } |
439 | ||
440 | /* Append an error cause if needed */ | |
441 | if (AppendCauseV) { | |
442 | if (_mSelfMsgIter) { | |
443 | bt_current_thread_error_append_cause_from_message_iterator( | |
e27adb90 | 444 | _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); |
7a72f18a PP |
445 | } else if (_mSelfComp) { |
446 | bt_current_thread_error_append_cause_from_component( | |
e27adb90 | 447 | _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); |
7a72f18a PP |
448 | } else if (_mSelfCompCls) { |
449 | bt_current_thread_error_append_cause_from_component_class( | |
e27adb90 | 450 | _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); |
7a72f18a | 451 | } else { |
d88930bc | 452 | BT_ASSERT(_mModuleName); |
e27adb90 PP |
453 | bt_current_thread_error_append_cause_from_unknown( |
454 | _mModuleName->data(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); | |
7a72f18a PP |
455 | } |
456 | } | |
457 | } | |
458 | ||
d88930bc PP |
459 | static std::string _errnoIntroStr(const char * const initMsg) |
460 | { | |
461 | BT_ASSERT(errno != 0); | |
462 | return fmt::format("{}: {}", initMsg, g_strerror(errno)); | |
463 | } | |
464 | ||
4f235e19 | 465 | /* Exactly one of the following four members has a value */ |
7a72f18a PP |
466 | bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls; |
467 | bt2s::optional<bt2::SelfComponent> _mSelfComp; | |
468 | bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter; | |
469 | bt2s::optional<std::string> _mModuleName; | |
470 | ||
471 | /* Current logging level */ | |
472 | Level _mLevel; | |
473 | ||
474 | /* Logging tag */ | |
475 | std::string _mTag; | |
476 | ||
477 | /* Formatting buffer */ | |
478 | mutable std::vector<char> _mBuf; | |
479 | }; | |
480 | ||
3c20ac12 SM |
481 | /* |
482 | * Returns `s` if it's not `nullptr`, or the `(null)` string otherwise. | |
483 | */ | |
484 | inline const char *maybeNull(const char * const s) noexcept | |
485 | { | |
486 | return s ? s : "(null)"; | |
487 | } | |
488 | ||
7a72f18a PP |
489 | } /* namespace bt2c */ |
490 | ||
491 | /* Internal: default logger name */ | |
492 | #define _BT_CPPLOG_DEF_LOGGER _mLogger | |
493 | ||
494 | /* | |
9d15c577 | 495 | * Calls log() on `_logger` to log using the level `_lvl`. |
7a72f18a PP |
496 | */ |
497 | #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \ | |
498 | do { \ | |
499 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
e40ac8f6 PP |
500 | (_logger).template log<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), \ |
501 | ##__VA_ARGS__); \ | |
7a72f18a PP |
502 | } \ |
503 | } while (0) | |
504 | ||
505 | /* | |
506 | * BT_CPPLOG_EX() with specific logging levels. | |
507 | */ | |
508 | #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \ | |
1c5ea5eb | 509 | BT_CPPLOG_EX(bt2c::Logger::Level::Trace, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 510 | #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 511 | BT_CPPLOG_EX(bt2c::Logger::Level::Debug, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 512 | #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 513 | BT_CPPLOG_EX(bt2c::Logger::Level::Info, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 514 | #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 515 | BT_CPPLOG_EX(bt2c::Logger::Level::Warning, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 516 | #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 517 | BT_CPPLOG_EX(bt2c::Logger::Level::Error, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 518 | #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 519 | BT_CPPLOG_EX(bt2c::Logger::Level::Fatal, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a PP |
520 | |
521 | /* | |
522 | * BT_CPPLOG_EX() with specific logging levels and using the default | |
523 | * logger. | |
524 | */ | |
525 | #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
526 | #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
527 | #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
528 | #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
529 | #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
530 | #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
531 | ||
7a72f18a | 532 | /* |
9d15c577 | 533 | * Calls logMem() on `_logger` to log using the level `_lvl`. |
7a72f18a | 534 | */ |
53e1ecce | 535 | #define BT_CPPLOG_MEM_EX(_lvl, _logger, _memData, _fmt, ...) \ |
7a72f18a PP |
536 | do { \ |
537 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
e40ac8f6 PP |
538 | (_logger).template logMem<(_lvl)>(__FILE__, __func__, __LINE__, (_memData), (_fmt), \ |
539 | ##__VA_ARGS__); \ | |
7a72f18a PP |
540 | } \ |
541 | } while (0) | |
542 | ||
543 | /* | |
544 | * BT_CPPLOG_MEM_EX() with specific logging levels. | |
545 | */ | |
53e1ecce PP |
546 | #define BT_CPPLOGT_MEM_SPEC(_logger, _memData, _fmt, ...) \ |
547 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Trace, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
548 | #define BT_CPPLOGD_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
549 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Debug, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
550 | #define BT_CPPLOGI_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
551 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Info, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
552 | #define BT_CPPLOGW_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
553 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Warning, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
554 | #define BT_CPPLOGE_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
555 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Error, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
556 | #define BT_CPPLOGF_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
557 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Fatal, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
558 | |
559 | /* | |
560 | * BT_CPPLOG_MEM_EX() with specific logging levels and using the default | |
561 | * logger. | |
562 | */ | |
53e1ecce PP |
563 | #define BT_CPPLOGT_MEM(_memData, _fmt, ...) \ |
564 | BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
565 | #define BT_CPPLOGD_MEM(_memData, _fmt, ...) \ | |
566 | BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
567 | #define BT_CPPLOGI_MEM(_memData, _fmt, ...) \ | |
568 | BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
569 | #define BT_CPPLOGW_MEM(_memData, _fmt, ...) \ | |
570 | BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
571 | #define BT_CPPLOGE_MEM(_memData, _fmt, ...) \ | |
572 | BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
573 | #define BT_CPPLOGF_MEM(_memData, _fmt, ...) \ | |
574 | BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
7a72f18a | 575 | |
7a72f18a | 576 | /* |
9d15c577 | 577 | * Calls logErrno() on `_logger` to log using the level `_lvl` and |
b5acd2ad | 578 | * initial message `_initMsg`. |
7a72f18a | 579 | */ |
b5acd2ad | 580 | #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _initMsg, _fmt, ...) \ |
7a72f18a PP |
581 | do { \ |
582 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
e40ac8f6 PP |
583 | (_logger).template logErrno<(_lvl), false>(__FILE__, __func__, __LINE__, (_initMsg), \ |
584 | (_fmt), ##__VA_ARGS__); \ | |
7a72f18a PP |
585 | } \ |
586 | } while (0) | |
587 | ||
588 | /* | |
589 | * BT_CPPLOG_ERRNO_EX() with specific logging levels. | |
590 | */ | |
b5acd2ad PP |
591 | #define BT_CPPLOGT_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ |
592 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Trace, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
593 | #define BT_CPPLOGD_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
594 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Debug, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
595 | #define BT_CPPLOGI_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
596 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Info, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
597 | #define BT_CPPLOGW_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
598 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Warning, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
599 | #define BT_CPPLOGE_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
600 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Error, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
601 | #define BT_CPPLOGF_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
602 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Fatal, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
603 | |
604 | /* | |
605 | * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the | |
606 | * default logger. | |
607 | */ | |
b5acd2ad PP |
608 | #define BT_CPPLOGT_ERRNO(_initMsg, _fmt, ...) \ |
609 | BT_CPPLOGT_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
610 | #define BT_CPPLOGD_ERRNO(_initMsg, _fmt, ...) \ | |
611 | BT_CPPLOGD_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
612 | #define BT_CPPLOGI_ERRNO(_initMsg, _fmt, ...) \ | |
613 | BT_CPPLOGI_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
614 | #define BT_CPPLOGW_ERRNO(_initMsg, _fmt, ...) \ | |
615 | BT_CPPLOGW_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
616 | #define BT_CPPLOGE_ERRNO(_initMsg, _fmt, ...) \ | |
617 | BT_CPPLOGE_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
618 | #define BT_CPPLOGF_ERRNO(_initMsg, _fmt, ...) \ | |
619 | BT_CPPLOGF_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
7a72f18a | 620 | |
aa37512c SM |
621 | /* |
622 | * Calls log() on `_logger` with the `Error` level to log an error and | |
623 | * append a cause to the error of the current thread. | |
624 | */ | |
625 | #define BT_CPPLOGE_APPEND_CAUSE_SPEC(_logger, _fmt, ...) \ | |
e40ac8f6 PP |
626 | (_logger).template log<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_fmt), \ |
627 | ##__VA_ARGS__) | |
aa37512c SM |
628 | |
629 | /* | |
630 | * BT_CPPLOGE_APPEND_CAUSE_SPEC() using the default logger. | |
631 | */ | |
632 | #define BT_CPPLOGE_APPEND_CAUSE(_fmt, ...) \ | |
633 | BT_CPPLOGE_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
634 | ||
7a72f18a PP |
635 | /* |
636 | * Calls logErrorAndThrow() on `_logger` to log an error, append a cause | |
637 | * to the error of the current thread, and throw an instance of | |
b5acd2ad | 638 | * `_excCls`. |
7a72f18a | 639 | */ |
b5acd2ad | 640 | #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _excCls, _fmt, ...) \ |
e40ac8f6 PP |
641 | (_logger).template logErrorAndThrow<true, _excCls>(__FILE__, __func__, __LINE__, (_fmt), \ |
642 | ##__VA_ARGS__) | |
7a72f18a | 643 | |
7a72f18a PP |
644 | /* |
645 | * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger. | |
646 | */ | |
b5acd2ad PP |
647 | #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_excCls, _fmt, ...) \ |
648 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _excCls, (_fmt), ##__VA_ARGS__) | |
7a72f18a | 649 | |
7a72f18a PP |
650 | /* |
651 | * Calls logErrorAndRethrow() on `_logger` to log an error, append a | |
652 | * cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 653 | * `_excCls`. |
7a72f18a PP |
654 | */ |
655 | #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \ | |
e40ac8f6 | 656 | (_logger).template logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__) |
7a72f18a PP |
657 | |
658 | /* | |
659 | * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger. | |
660 | */ | |
661 | #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \ | |
662 | BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
663 | ||
2f505dde SM |
664 | /* |
665 | * Calls logErrno() on `_logger` with the `Level::Error` level to log an | |
666 | * error and append a cause to the error of the current thread. | |
667 | */ | |
b5acd2ad | 668 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_logger, _initMsg, _fmt, ...) \ |
e40ac8f6 PP |
669 | (_logger).template logErrno<bt2c::Logger::Level::Error, true>( \ |
670 | __FILE__, __func__, __LINE__, (_initMsg), (_fmt), ##__VA_ARGS__) | |
2f505dde SM |
671 | |
672 | /* | |
673 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC() using the default logger. | |
674 | */ | |
b5acd2ad PP |
675 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE(_initMsg, _fmt, ...) \ |
676 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
2f505dde | 677 | |
7a72f18a PP |
678 | /* |
679 | * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a | |
680 | * cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 681 | * `_excCls`. |
7a72f18a | 682 | */ |
b5acd2ad | 683 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _excCls, _initMsg, _fmt, ...) \ |
e40ac8f6 PP |
684 | (_logger).template logErrorErrnoAndThrow<true, _excCls>(__FILE__, __func__, __LINE__, \ |
685 | (_initMsg), (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
686 | |
687 | /* | |
688 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default | |
689 | * logger. | |
690 | */ | |
b5acd2ad PP |
691 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_excCls, _initMsg, _fmt, ...) \ |
692 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _excCls, (_initMsg), \ | |
7a72f18a PP |
693 | (_fmt), ##__VA_ARGS__) |
694 | ||
7a72f18a PP |
695 | /* |
696 | * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append | |
697 | * a cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 698 | * `_excCls`. |
7a72f18a | 699 | */ |
b5acd2ad | 700 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _initMsg, _fmt, ...) \ |
e40ac8f6 PP |
701 | (_logger).template logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_initMsg), \ |
702 | (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
703 | |
704 | /* | |
705 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default | |
706 | * logger. | |
707 | */ | |
b5acd2ad PP |
708 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_initMsg, _fmt, ...) \ |
709 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), \ | |
7a72f18a PP |
710 | ##__VA_ARGS__) |
711 | ||
7a72f18a | 712 | #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */ |