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