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" | |
25 | #include "cpp-common/vendor/fmt/core.h" | |
644c6ca7 | 26 | #include "cpp-common/vendor/wise-enum/wise_enum.h" |
7a72f18a PP |
27 | #include "logging/log-api.h" |
28 | ||
29 | namespace bt2c { | |
30 | ||
31 | /* | |
32 | * A logger contains an actor (self component class, self component, | |
33 | * self message iterator, or simple module name), a current logging | |
34 | * level, and a logging tag. | |
35 | * | |
9d15c577 SM |
36 | * It offers the log(), logMem(), logErrno(), logErrorAndThrow(), |
37 | * logErrorAndRethrow(), logErrorErrnoAndThrow(), and | |
38 | * logErrorErrnoAndRethrow() method templates to log using a given | |
7a72f18a PP |
39 | * level, optionally append a cause to the error of the current thread |
40 | * using the correct actor, and optionally throw or rethrow. | |
41 | * | |
42 | * The methods above expect a format string and zero or more arguments | |
43 | * to be formatted with fmt::format(). | |
44 | */ | |
45 | class Logger final | |
46 | { | |
47 | public: | |
644c6ca7 SM |
48 | /* clang-format off */ |
49 | ||
7a72f18a | 50 | /* Available log levels */ |
644c6ca7 | 51 | WISE_ENUM_CLASS_MEMBER(Level, |
24ce7ecb PP |
52 | (Trace, BT_LOG_TRACE), |
53 | (Debug, BT_LOG_DEBUG), | |
54 | (Info, BT_LOG_INFO), | |
55 | (Warning, BT_LOG_WARNING), | |
56 | (Error, BT_LOG_ERROR), | |
57 | (Fatal, BT_LOG_FATAL), | |
58 | (None, BT_LOG_NONE) | |
59 | ) | |
644c6ca7 SM |
60 | |
61 | /* clang-format on */ | |
7a72f18a PP |
62 | |
63 | /* | |
64 | * Builds a logger from the self component class `selfCompCls` using | |
65 | * the tag `tag` and the logging level of `privQueryExec`. | |
66 | */ | |
67 | explicit Logger(const bt2::SelfComponentClass selfCompCls, | |
68 | const bt2::PrivateQueryExecutor privQueryExec, std::string tag) noexcept : | |
69 | _mSelfCompCls {selfCompCls}, | |
70 | _mLevel {static_cast<Level>(privQueryExec.loggingLevel())}, _mTag {std::move(tag)} | |
71 | { | |
72 | } | |
73 | ||
74 | /* | |
75 | * Builds a logger from the self component `selfComp` using the tag | |
76 | * `tag`. | |
77 | */ | |
78 | explicit Logger(const bt2::SelfComponent selfComp, std::string tag) noexcept : | |
79 | _mSelfComp {selfComp}, _mLevel {static_cast<Level>(selfComp.loggingLevel())}, _mTag { | |
80 | std::move( | |
81 | tag)} | |
82 | { | |
83 | } | |
84 | ||
85 | /* | |
86 | * Builds a logger from the self source component `selfComp` using | |
87 | * the tag `tag`. | |
88 | */ | |
89 | explicit Logger(const bt2::SelfSourceComponent selfComp, std::string tag) noexcept : | |
90 | Logger { | |
91 | bt2::SelfComponent {bt_self_component_source_as_self_component(selfComp.libObjPtr())}, | |
92 | std::move(tag)} | |
93 | { | |
94 | } | |
95 | ||
96 | /* | |
97 | * Builds a logger from the self filter component `selfComp` using | |
98 | * the tag `tag`. | |
99 | */ | |
100 | explicit Logger(const bt2::SelfFilterComponent selfComp, std::string tag) noexcept : | |
101 | Logger { | |
102 | bt2::SelfComponent {bt_self_component_filter_as_self_component(selfComp.libObjPtr())}, | |
103 | std::move(tag)} | |
104 | { | |
105 | } | |
106 | ||
107 | /* | |
108 | * Builds a logger from the self sink component `selfComp` using the | |
109 | * tag `tag`. | |
110 | */ | |
111 | explicit Logger(const bt2::SelfSinkComponent selfComp, std::string tag) noexcept : | |
112 | Logger {bt2::SelfComponent {bt_self_component_sink_as_self_component(selfComp.libObjPtr())}, | |
113 | std::move(tag)} | |
114 | { | |
115 | } | |
116 | ||
117 | /* | |
118 | * Builds a logger from the self message iterator `selfMsgIter` | |
119 | * using the tag `tag`. | |
120 | */ | |
121 | explicit Logger(const bt2::SelfMessageIterator selfMsgIter, std::string tag) noexcept : | |
4f235e19 SM |
122 | _mSelfMsgIter {selfMsgIter}, |
123 | _mLevel {static_cast<Level>(selfMsgIter.component().loggingLevel())}, _mTag {std::move(tag)} | |
7a72f18a | 124 | { |
7a72f18a PP |
125 | } |
126 | ||
127 | /* | |
128 | * Builds a logger from the module named `moduleName` using the tag | |
129 | * `tag` and logging level `logLevel`. | |
130 | */ | |
131 | explicit Logger(std::string moduleName, std::string tag, const Level logLevel) noexcept : | |
132 | _mModuleName {std::move(moduleName)}, _mLevel {logLevel}, _mTag {std::move(tag)} | |
133 | { | |
134 | } | |
135 | ||
136 | /* | |
137 | * Builds a logger from another logger `other` using the new tag | |
138 | * `newTag`. | |
139 | */ | |
140 | explicit Logger(const Logger& other, std::string newTag) : | |
eee15e59 SM |
141 | _mSelfCompCls {other._mSelfCompCls}, _mSelfComp {other._mSelfComp}, |
142 | _mSelfMsgIter {other._mSelfMsgIter}, | |
7a72f18a PP |
143 | _mModuleName {other._mModuleName}, _mLevel {other._mLevel}, _mTag {std::move(newTag)} |
144 | { | |
145 | } | |
146 | ||
147 | /* | |
148 | * Current logging level. | |
149 | */ | |
150 | Level level() const noexcept | |
151 | { | |
152 | return _mLevel; | |
153 | } | |
154 | ||
155 | /* | |
156 | * Current logging level converted to a `bt_log_level` value. | |
157 | * | |
158 | * For legacy code. | |
159 | */ | |
160 | bt_log_level cLevel() const noexcept | |
161 | { | |
162 | return static_cast<bt_log_level>(_mLevel); | |
163 | } | |
164 | ||
165 | /* | |
166 | * Whether or not this logger would log at the level `level`. | |
167 | */ | |
168 | bool wouldLog(const Level level) const noexcept | |
169 | { | |
170 | return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel)); | |
171 | } | |
172 | ||
173 | /* | |
174 | * Whether or not this logger would log at the trace level. | |
175 | */ | |
176 | bool wouldLogT() const noexcept | |
177 | { | |
1c5ea5eb | 178 | return this->wouldLog(Level::Trace); |
7a72f18a PP |
179 | } |
180 | ||
181 | /* | |
182 | * Whether or not this logger would log at the debug level. | |
183 | */ | |
184 | bool wouldLogD() const noexcept | |
185 | { | |
1c5ea5eb | 186 | return this->wouldLog(Level::Debug); |
7a72f18a PP |
187 | } |
188 | ||
189 | /* | |
190 | * Whether or not this logger would log at the info level. | |
191 | */ | |
192 | bool wouldLogI() const noexcept | |
193 | { | |
1c5ea5eb | 194 | return this->wouldLog(Level::Info); |
7a72f18a PP |
195 | } |
196 | ||
197 | /* | |
198 | * Whether or not this logger would log at the warning level. | |
199 | */ | |
200 | bool wouldLogW() const noexcept | |
201 | { | |
1c5ea5eb | 202 | return this->wouldLog(Level::Warning); |
7a72f18a PP |
203 | } |
204 | ||
205 | /* | |
206 | * Whether or not this logger would log at the error level. | |
207 | */ | |
208 | bool wouldLogE() const noexcept | |
209 | { | |
1c5ea5eb | 210 | return this->wouldLog(Level::Error); |
7a72f18a PP |
211 | } |
212 | ||
213 | /* | |
214 | * Whether or not this logger would log at the fatal level. | |
215 | */ | |
216 | bool wouldLogF() const noexcept | |
217 | { | |
1c5ea5eb | 218 | return this->wouldLog(Level::Fatal); |
7a72f18a PP |
219 | } |
220 | ||
221 | /* | |
222 | * Logging tag. | |
223 | */ | |
224 | const std::string& tag() const noexcept | |
225 | { | |
226 | return _mTag; | |
227 | } | |
228 | ||
229 | /* | |
230 | * Self component class actor, or `bt2s::nullopt` if none. | |
231 | */ | |
232 | const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept | |
233 | { | |
234 | return _mSelfCompCls; | |
235 | } | |
236 | ||
237 | /* | |
238 | * Self component actor, or `bt2s::nullopt` if none. | |
239 | */ | |
240 | const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept | |
241 | { | |
242 | return _mSelfComp; | |
243 | } | |
244 | ||
245 | /* | |
246 | * Self message iterator actor, or `bt2s::nullopt` if none. | |
247 | */ | |
248 | const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept | |
249 | { | |
250 | return _mSelfMsgIter; | |
251 | } | |
252 | ||
253 | /* | |
254 | * Name of module actor, or `bt2s::nullopt` if none. | |
255 | */ | |
256 | const bt2s::optional<std::string>& moduleName() const noexcept | |
257 | { | |
258 | return _mModuleName; | |
259 | } | |
260 | ||
261 | private: | |
262 | struct _StdLogWriter final | |
263 | { | |
264 | static void write(const char * const fileName, const char * const funcName, | |
265 | const unsigned lineNo, const Level level, const char * const tag, | |
d88930bc PP |
266 | const void *, unsigned int, const char * const initMsg, |
267 | const char * const msg) noexcept | |
7a72f18a | 268 | { |
d88930bc | 269 | BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0); |
7a72f18a PP |
270 | bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg); |
271 | } | |
272 | }; | |
273 | ||
274 | public: | |
275 | /* | |
276 | * Logs using the level `LevelV`. | |
277 | * | |
278 | * This method forwards `fmt` and `args` to fmt::format() to create | |
279 | * the log message. | |
280 | * | |
281 | * If `AppendCauseV` is true, this method also appends a cause to | |
282 | * the error of the current thread using the same message. | |
283 | */ | |
284 | template <Level LevelV, bool AppendCauseV, typename... ArgTs> | |
9d15c577 SM |
285 | void log(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
286 | const char * const fmt, ArgTs&&...args) const | |
7a72f18a | 287 | { |
9d15c577 SM |
288 | this->_log<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0, "", |
289 | fmt, std::forward<ArgTs>(args)...); | |
7a72f18a PP |
290 | } |
291 | ||
292 | /* | |
293 | * Logs `msg` using the level `LevelV`. | |
294 | * | |
295 | * If `AppendCauseV` is true, this method also appends a cause to | |
296 | * the error of the current thread using the same message. | |
297 | */ | |
298 | template <Level LevelV, bool AppendCauseV> | |
9d15c577 SM |
299 | void logStr(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
300 | const char * const msg) const | |
7a72f18a | 301 | { |
9d15c577 SM |
302 | this->_logStr<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0, |
303 | "", msg); | |
7a72f18a PP |
304 | } |
305 | ||
306 | /* | |
9d15c577 SM |
307 | * Like log() with the `Level::Error` level, but also throws a |
308 | * default-constructed instance of `ExcT`. | |
7a72f18a PP |
309 | */ |
310 | template <bool AppendCauseV, typename ExcT, typename... ArgTs> | |
311 | [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName, | |
312 | const unsigned int lineNo, const char * const fmt, | |
313 | ArgTs&&...args) const | |
314 | { | |
9d15c577 SM |
315 | this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt, |
316 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
317 | throw ExcT {}; |
318 | } | |
319 | ||
320 | /* | |
9d15c577 SM |
321 | * Like logStr() with the `Level::Error` level, but also throws a |
322 | * default-constructed instance of `ExcT`. | |
7a72f18a PP |
323 | */ |
324 | template <bool AppendCauseV, typename ExcT> | |
325 | [[noreturn]] void logErrorStrAndThrow(const char * const fileName, const char * const funcName, | |
326 | const unsigned int lineNo, const char * const msg) const | |
327 | { | |
9d15c577 | 328 | this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg); |
7a72f18a PP |
329 | throw ExcT {}; |
330 | } | |
331 | ||
332 | /* | |
9d15c577 | 333 | * Like log() with the `Level::Error` level, but also rethrows. |
7a72f18a PP |
334 | */ |
335 | template <bool AppendCauseV, typename... ArgTs> | |
336 | [[noreturn]] void logErrorAndRethrow(const char * const fileName, const char * const funcName, | |
337 | const unsigned int lineNo, const char * const fmt, | |
338 | ArgTs&&...args) const | |
339 | { | |
9d15c577 SM |
340 | this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt, |
341 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
342 | throw; |
343 | } | |
344 | ||
345 | /* | |
9d15c577 | 346 | * Like logStr() with the `Level::Error` level, but also rethrows. |
7a72f18a PP |
347 | */ |
348 | template <bool AppendCauseV> | |
349 | [[noreturn]] void logErrorStrAndRethrow(const char * const fileName, | |
350 | const char * const funcName, const unsigned int lineNo, | |
351 | const char * const msg) const | |
352 | { | |
9d15c577 | 353 | this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg); |
7a72f18a PP |
354 | throw; |
355 | } | |
356 | ||
357 | private: | |
d88930bc | 358 | struct _InitMsgLogWriter final |
7a72f18a PP |
359 | { |
360 | static void write(const char * const fileName, const char * const funcName, | |
361 | const unsigned lineNo, const Level level, const char * const tag, | |
362 | const void *, unsigned int, const char * const initMsg, | |
363 | const char * const msg) noexcept | |
364 | { | |
d88930bc PP |
365 | bt_log_write_printf(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag, |
366 | "%s%s", initMsg, msg); | |
7a72f18a PP |
367 | } |
368 | }; | |
369 | ||
370 | public: | |
371 | /* | |
372 | * Logs the message of `errno` using the level `LevelV`. | |
373 | * | |
374 | * The log message starts with `initMsg`, is followed with the | |
375 | * message for `errno`, and then with what fmt::format() creates | |
376 | * given `fmt` and `args`. | |
377 | * | |
378 | * If `AppendCauseV` is true, this method also appends a cause to | |
379 | * the error of the current thread using the same message. | |
380 | */ | |
381 | template <Level LevelV, bool AppendCauseV, typename... ArgTs> | |
9d15c577 SM |
382 | void logErrno(const char * const fileName, const char * const funcName, |
383 | const unsigned int lineNo, const char * const initMsg, const char * const fmt, | |
384 | ArgTs&&...args) const | |
7a72f18a | 385 | { |
9d15c577 SM |
386 | this->_log<_InitMsgLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, nullptr, 0, |
387 | this->_errnoIntroStr(initMsg).c_str(), | |
388 | fmt, std::forward<ArgTs>(args)...); | |
7a72f18a PP |
389 | } |
390 | ||
391 | /* | |
392 | * Logs the message of `errno` using the level `LevelV`. | |
393 | * | |
394 | * The log message starts with `initMsg`, is followed with the | |
395 | * message for `errno`, and then with `msg`. | |
396 | * | |
397 | * If `AppendCauseV` is true, this method also appends a cause to | |
398 | * the error of the current thread using the same message. | |
399 | */ | |
400 | template <Level LevelV, bool AppendCauseV> | |
9d15c577 SM |
401 | void logErrnoStr(const char * const fileName, const char * const funcName, |
402 | const unsigned int lineNo, const char * const initMsg, | |
403 | const char * const msg) const | |
7a72f18a | 404 | { |
9d15c577 | 405 | this->_logStr<_InitMsgLogWriter, LevelV, AppendCauseV>( |
d88930bc | 406 | fileName, funcName, lineNo, nullptr, 0, this->_errnoIntroStr(initMsg).c_str(), msg); |
7a72f18a PP |
407 | } |
408 | ||
409 | /* | |
9d15c577 SM |
410 | * Like logErrno() with the `Level::Error` level, but also throws a |
411 | * default-constructed instance of `ExcT`. | |
7a72f18a PP |
412 | */ |
413 | template <bool AppendCauseV, typename ExcT, typename... ArgTs> | |
414 | [[noreturn]] void logErrorErrnoAndThrow(const char * const fileName, | |
415 | const char * const funcName, const unsigned int lineNo, | |
416 | const char * const initMsg, const char * const fmt, | |
417 | ArgTs&&...args) const | |
418 | { | |
9d15c577 SM |
419 | this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt, |
420 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
421 | throw ExcT {}; |
422 | } | |
423 | ||
424 | /* | |
9d15c577 SM |
425 | * Like logErrnoStr() with the `Level::Error` level, but also throws |
426 | * a default-constructed instance of `ExcT`. | |
7a72f18a PP |
427 | */ |
428 | template <bool AppendCauseV, typename ExcT> | |
429 | [[noreturn]] void | |
430 | logErrorErrnoStrAndThrow(const char * const fileName, const char * const funcName, | |
431 | const unsigned int lineNo, const char * const initMsg, | |
432 | const char * const msg) const | |
433 | { | |
9d15c577 | 434 | this->logErrnoStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, msg); |
7a72f18a PP |
435 | throw ExcT {}; |
436 | } | |
437 | ||
438 | /* | |
9d15c577 | 439 | * Like logErrno() with the `Level::Error` level, but also rethrows. |
7a72f18a PP |
440 | */ |
441 | template <bool AppendCauseV, typename... ArgTs> | |
442 | [[noreturn]] void logErrorErrnoAndRethrow(const char * const fileName, | |
443 | const char * const funcName, | |
444 | const unsigned int lineNo, const char * const initMsg, | |
445 | const char * const fmt, ArgTs&&...args) const | |
446 | { | |
9d15c577 SM |
447 | this->logErrno<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, fmt, |
448 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
449 | throw; |
450 | } | |
451 | ||
452 | /* | |
9d15c577 | 453 | * Like logErrnoStr() with the `Level::Error` level, but also |
7a72f18a PP |
454 | * rethrows. |
455 | */ | |
456 | template <bool AppendCauseV> | |
457 | [[noreturn]] void | |
458 | logErrorErrnoStrAndRethrow(const char * const fileName, const char * const funcName, | |
459 | const unsigned int lineNo, const char * const initMsg, | |
460 | const char * const msg) const | |
461 | { | |
9d15c577 | 462 | this->logErrnoStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, initMsg, msg); |
7a72f18a PP |
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> | |
9d15c577 SM |
487 | void logMem(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
488 | const void * const memData, const unsigned int memLen, const char * const fmt, | |
489 | ArgTs&&...args) const | |
7a72f18a | 490 | { |
9d15c577 SM |
491 | this->_log<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, "", |
492 | fmt, std::forward<ArgTs>(args)...); | |
7a72f18a PP |
493 | } |
494 | ||
495 | /* | |
496 | * Logs memory data using the level `LevelV`, starting with the | |
497 | * message `msg`. | |
498 | */ | |
499 | template <Level LevelV> | |
9d15c577 SM |
500 | void logMemStr(const char * const fileName, const char * const funcName, |
501 | const unsigned int lineNo, const void * const memData, const unsigned int memLen, | |
502 | const char * const msg) const | |
7a72f18a | 503 | { |
9d15c577 SM |
504 | this->_logStr<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, memLen, "", |
505 | msg); | |
7a72f18a PP |
506 | } |
507 | ||
508 | private: | |
509 | /* | |
510 | * Formats a log message with fmt::format() given `fmt` and `args`, | |
9d15c577 | 511 | * and then forwards everything to _logStr(). |
7a72f18a PP |
512 | */ |
513 | template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs> | |
9d15c577 SM |
514 | void _log(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
515 | const void * const memData, const std::size_t memLen, const char * const initMsg, | |
516 | const char * const fmt, ArgTs&&...args) const | |
7a72f18a PP |
517 | { |
518 | /* Only format arguments if logging or appending an error cause */ | |
519 | if (G_UNLIKELY(this->wouldLog(LevelV) || AppendCauseV)) { | |
520 | /* | |
521 | * Format arguments to our buffer (fmt::format_to() doesn't | |
522 | * append a null character). | |
523 | */ | |
524 | _mBuf.clear(); | |
525 | fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...); | |
526 | _mBuf.push_back('\0'); | |
527 | } | |
528 | ||
9d15c577 SM |
529 | this->_logStr<LogWriterT, LevelV, AppendCauseV>(fileName, funcName, lineNo, memData, memLen, |
530 | initMsg, _mBuf.data()); | |
7a72f18a PP |
531 | } |
532 | ||
533 | /* | |
d88930bc PP |
534 | * Calls LogWriterT::write() with its arguments to log using the |
535 | * level `LevelV`. | |
7a72f18a PP |
536 | * |
537 | * If `AppendCauseV` is true, this method also appends a cause to | |
d88930bc PP |
538 | * the error of the current thread using the concatenation of |
539 | * `initMsg` and `msg` as the message. | |
7a72f18a PP |
540 | */ |
541 | template <typename LogWriterT, Level LevelV, bool AppendCauseV> | |
9d15c577 SM |
542 | void _logStr(const char * const fileName, const char * const funcName, |
543 | const unsigned int lineNo, const void * const memData, const std::size_t memLen, | |
544 | const char * const initMsg, const char * const msg) const | |
7a72f18a | 545 | { |
d88930bc PP |
546 | /* Initial message and main message are required */ |
547 | BT_ASSERT(initMsg); | |
548 | BT_ASSERT(msg); | |
549 | ||
7a72f18a PP |
550 | /* Log if needed */ |
551 | if (this->wouldLog(LevelV)) { | |
552 | LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, memLen, | |
553 | initMsg, msg); | |
554 | } | |
555 | ||
556 | /* Append an error cause if needed */ | |
557 | if (AppendCauseV) { | |
558 | if (_mSelfMsgIter) { | |
559 | bt_current_thread_error_append_cause_from_message_iterator( | |
bdc900ed | 560 | _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg); |
7a72f18a PP |
561 | } else if (_mSelfComp) { |
562 | bt_current_thread_error_append_cause_from_component( | |
bdc900ed | 563 | _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg); |
7a72f18a PP |
564 | } else if (_mSelfCompCls) { |
565 | bt_current_thread_error_append_cause_from_component_class( | |
bdc900ed | 566 | _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg); |
7a72f18a | 567 | } else { |
d88930bc | 568 | BT_ASSERT(_mModuleName); |
bdc900ed PP |
569 | bt_current_thread_error_append_cause_from_unknown(_mModuleName->data(), fileName, |
570 | lineNo, "%s%s", initMsg, msg); | |
7a72f18a PP |
571 | } |
572 | } | |
573 | } | |
574 | ||
d88930bc PP |
575 | static std::string _errnoIntroStr(const char * const initMsg) |
576 | { | |
577 | BT_ASSERT(errno != 0); | |
578 | return fmt::format("{}: {}", initMsg, g_strerror(errno)); | |
579 | } | |
580 | ||
4f235e19 | 581 | /* Exactly one of the following four members has a value */ |
7a72f18a PP |
582 | bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls; |
583 | bt2s::optional<bt2::SelfComponent> _mSelfComp; | |
584 | bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter; | |
585 | bt2s::optional<std::string> _mModuleName; | |
586 | ||
587 | /* Current logging level */ | |
588 | Level _mLevel; | |
589 | ||
590 | /* Logging tag */ | |
591 | std::string _mTag; | |
592 | ||
593 | /* Formatting buffer */ | |
594 | mutable std::vector<char> _mBuf; | |
595 | }; | |
596 | ||
3c20ac12 SM |
597 | /* |
598 | * Returns `s` if it's not `nullptr`, or the `(null)` string otherwise. | |
599 | */ | |
600 | inline const char *maybeNull(const char * const s) noexcept | |
601 | { | |
602 | return s ? s : "(null)"; | |
603 | } | |
604 | ||
7a72f18a PP |
605 | } /* namespace bt2c */ |
606 | ||
607 | /* Internal: default logger name */ | |
608 | #define _BT_CPPLOG_DEF_LOGGER _mLogger | |
609 | ||
610 | /* | |
9d15c577 | 611 | * Calls log() on `_logger` to log using the level `_lvl`. |
7a72f18a PP |
612 | */ |
613 | #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \ | |
614 | do { \ | |
615 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
9d15c577 | 616 | (_logger).log<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__); \ |
7a72f18a PP |
617 | } \ |
618 | } while (0) | |
619 | ||
620 | /* | |
621 | * BT_CPPLOG_EX() with specific logging levels. | |
622 | */ | |
623 | #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \ | |
1c5ea5eb | 624 | BT_CPPLOG_EX(bt2c::Logger::Level::Trace, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 625 | #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 626 | BT_CPPLOG_EX(bt2c::Logger::Level::Debug, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 627 | #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 628 | BT_CPPLOG_EX(bt2c::Logger::Level::Info, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 629 | #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 630 | BT_CPPLOG_EX(bt2c::Logger::Level::Warning, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 631 | #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 632 | BT_CPPLOG_EX(bt2c::Logger::Level::Error, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 633 | #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 634 | BT_CPPLOG_EX(bt2c::Logger::Level::Fatal, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a PP |
635 | |
636 | /* | |
637 | * BT_CPPLOG_EX() with specific logging levels and using the default | |
638 | * logger. | |
639 | */ | |
640 | #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
641 | #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
642 | #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
643 | #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
644 | #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
645 | #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
646 | ||
647 | /* | |
9d15c577 | 648 | * Calls logStr() on `_logger` to log using the level `_lvl`. |
7a72f18a PP |
649 | */ |
650 | #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \ | |
9d15c577 | 651 | (_logger).logStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg)) |
7a72f18a PP |
652 | |
653 | /* | |
654 | * BT_CPPLOG_STR_EX() with specific logging levels. | |
655 | */ | |
656 | #define BT_CPPLOGT_STR_SPEC(_logger, _msg) \ | |
1c5ea5eb | 657 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_msg)) |
7a72f18a | 658 | #define BT_CPPLOGD_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 659 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_msg)) |
7a72f18a | 660 | #define BT_CPPLOGI_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 661 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Info, (_logger), (_msg)) |
7a72f18a | 662 | #define BT_CPPLOGW_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 663 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_msg)) |
7a72f18a | 664 | #define BT_CPPLOGE_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 665 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Error, (_logger), (_msg)) |
7a72f18a | 666 | #define BT_CPPLOGF_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 667 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_msg)) |
7a72f18a PP |
668 | |
669 | /* | |
670 | * BT_CPPLOG_STR_EX() with specific logging levels and using the default | |
671 | * logger. | |
672 | */ | |
673 | #define BT_CPPLOGT_STR(_msg) BT_CPPLOGT_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
674 | #define BT_CPPLOGD_STR(_msg) BT_CPPLOGD_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
675 | #define BT_CPPLOGI_STR(_msg) BT_CPPLOGI_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
676 | #define BT_CPPLOGW_STR(_msg) BT_CPPLOGW_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
677 | #define BT_CPPLOGE_STR(_msg) BT_CPPLOGE_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
678 | #define BT_CPPLOGF_STR(_msg) BT_CPPLOGF_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
679 | ||
680 | /* | |
9d15c577 | 681 | * Calls logMem() on `_logger` to log using the level `_lvl`. |
7a72f18a PP |
682 | */ |
683 | #define BT_CPPLOG_MEM_EX(_lvl, _logger, _mem_data, _mem_len, _fmt, ...) \ | |
684 | do { \ | |
685 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
9d15c577 SM |
686 | (_logger).logMem<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), \ |
687 | (_fmt), ##__VA_ARGS__); \ | |
7a72f18a PP |
688 | } \ |
689 | } while (0) | |
690 | ||
691 | /* | |
692 | * BT_CPPLOG_MEM_EX() with specific logging levels. | |
693 | */ | |
694 | #define BT_CPPLOGT_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \ | |
1c5ea5eb | 695 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Trace, (_logger), (_mem_data), (_mem_len), (_fmt), \ |
7a72f18a PP |
696 | ##__VA_ARGS__) |
697 | #define BT_CPPLOGD_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \ | |
1c5ea5eb | 698 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Debug, (_logger), (_mem_data), (_mem_len), (_fmt), \ |
7a72f18a PP |
699 | ##__VA_ARGS__) |
700 | #define BT_CPPLOGI_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \ | |
1c5ea5eb | 701 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Info, (_logger), (_mem_data), (_mem_len), (_fmt), \ |
7a72f18a PP |
702 | ##__VA_ARGS__) |
703 | #define BT_CPPLOGW_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \ | |
1c5ea5eb | 704 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Warning, (_logger), (_mem_data), (_mem_len), (_fmt), \ |
7a72f18a PP |
705 | ##__VA_ARGS__) |
706 | #define BT_CPPLOGE_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \ | |
1c5ea5eb | 707 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_fmt), \ |
7a72f18a PP |
708 | ##__VA_ARGS__) |
709 | #define BT_CPPLOGF_MEM_SPEC(_logger, _mem_data, _mem_len, _fmt, ...) \ | |
1c5ea5eb | 710 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Fatal, (_logger), (_mem_data), (_mem_len), (_fmt), \ |
7a72f18a PP |
711 | ##__VA_ARGS__) |
712 | ||
713 | /* | |
714 | * BT_CPPLOG_MEM_EX() with specific logging levels and using the default | |
715 | * logger. | |
716 | */ | |
717 | #define BT_CPPLOGT_MEM(_mem_data, _mem_len, _fmt, ...) \ | |
718 | BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__) | |
719 | #define BT_CPPLOGD_MEM(_mem_data, _mem_len, _fmt, ...) \ | |
720 | BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__) | |
721 | #define BT_CPPLOGI_MEM(_mem_data, _mem_len, _fmt, ...) \ | |
722 | BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__) | |
723 | #define BT_CPPLOGW_MEM(_mem_data, _mem_len, _fmt, ...) \ | |
724 | BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__) | |
725 | #define BT_CPPLOGE_MEM(_mem_data, _mem_len, _fmt, ...) \ | |
726 | BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__) | |
727 | #define BT_CPPLOGF_MEM(_mem_data, _mem_len, _fmt, ...) \ | |
728 | BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_mem_data), (_mem_len), (_fmt), ##__VA_ARGS__) | |
729 | ||
730 | /* | |
9d15c577 | 731 | * Calls logMemStr() on `_logger` to log using the level `_lvl`. |
7a72f18a PP |
732 | */ |
733 | #define BT_CPPLOG_MEM_STR_EX(_lvl, _logger, _mem_data, _mem_len, _msg) \ | |
9d15c577 | 734 | (_logger).logMemStr<(_lvl)>(__FILE__, __func__, __LINE__, (_mem_data), (_mem_len), (_msg)) |
7a72f18a PP |
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) \ | |
1c5ea5eb | 748 | BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::Error, (_logger), (_mem_data), (_mem_len), (_msg)) |
7a72f18a PP |
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 | /* | |
9d15c577 SM |
770 | * Calls logErrno() on `_logger` to log using the level `_lvl` and |
771 | * initial message `_init_msg`. | |
7a72f18a PP |
772 | */ |
773 | #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _init_msg, _fmt, ...) \ | |
774 | do { \ | |
775 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
9d15c577 SM |
776 | (_logger).logErrno<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \ |
777 | ##__VA_ARGS__); \ | |
7a72f18a PP |
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, ...) \ | |
1c5ea5eb | 785 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__) |
7a72f18a | 786 | #define BT_CPPLOGD_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \ |
1c5ea5eb | 787 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__) |
7a72f18a | 788 | #define BT_CPPLOGI_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \ |
1c5ea5eb | 789 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__) |
7a72f18a | 790 | #define BT_CPPLOGW_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \ |
1c5ea5eb | 791 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__) |
7a72f18a | 792 | #define BT_CPPLOGE_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \ |
1c5ea5eb | 793 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__) |
7a72f18a | 794 | #define BT_CPPLOGF_ERRNO_SPEC(_logger, _init_msg, _fmt, ...) \ |
1c5ea5eb | 795 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_fmt), ##__VA_ARGS__) |
7a72f18a PP |
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 | /* | |
9d15c577 SM |
815 | * Calls logErrnoStr() on `_logger` to log using the level `_lvl` and |
816 | * initial message `_init_msg`. | |
7a72f18a PP |
817 | */ |
818 | #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _init_msg, _msg) \ | |
9d15c577 | 819 | (_logger).logErrnoStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_init_msg), (_msg)) |
7a72f18a PP |
820 | |
821 | /* | |
822 | * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels. | |
823 | */ | |
824 | #define BT_CPPLOGT_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \ | |
1c5ea5eb | 825 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_init_msg), (_msg)) |
7a72f18a | 826 | #define BT_CPPLOGD_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \ |
1c5ea5eb | 827 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_init_msg), (_msg)) |
7a72f18a | 828 | #define BT_CPPLOGI_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \ |
1c5ea5eb | 829 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Info, (_logger), (_init_msg), (_msg)) |
7a72f18a | 830 | #define BT_CPPLOGW_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \ |
1c5ea5eb | 831 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_init_msg), (_msg)) |
7a72f18a | 832 | #define BT_CPPLOGE_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \ |
1c5ea5eb | 833 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Error, (_logger), (_init_msg), (_msg)) |
7a72f18a | 834 | #define BT_CPPLOGF_ERRNO_STR_SPEC(_logger, _init_msg, _msg) \ |
1c5ea5eb | 835 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_init_msg), (_msg)) |
7a72f18a PP |
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 | ||
aa37512c SM |
854 | /* |
855 | * Calls log() on `_logger` with the `Error` level to log an error and | |
856 | * append a cause to the error of the current thread. | |
857 | */ | |
858 | #define BT_CPPLOGE_APPEND_CAUSE_SPEC(_logger, _fmt, ...) \ | |
859 | (_logger).log<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_fmt), \ | |
860 | ##__VA_ARGS__) | |
861 | ||
862 | /* | |
863 | * BT_CPPLOGE_APPEND_CAUSE_SPEC() using the default logger. | |
864 | */ | |
865 | #define BT_CPPLOGE_APPEND_CAUSE(_fmt, ...) \ | |
866 | BT_CPPLOGE_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
867 | ||
868 | /* | |
869 | * Calls logStr() on `_logger` with the `Error` level to log an error and | |
870 | * append a cause to the error of the current thread. | |
871 | */ | |
872 | #define BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_logger, _msg) \ | |
873 | (_logger).logStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_msg)) | |
874 | ||
875 | /* | |
876 | * BT_CPPLOGE_STR_APPEND_CAUSE_SPEC() using the default logger. | |
877 | */ | |
878 | #define BT_CPPLOGE_STR_APPEND_CAUSE(_msg) \ | |
879 | BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
880 | ||
7a72f18a PP |
881 | /* |
882 | * Calls logErrorAndThrow() on `_logger` to log an error, append a cause | |
883 | * to the error of the current thread, and throw an instance of | |
884 | * `_exc_cls`. | |
885 | */ | |
886 | #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _fmt, ...) \ | |
887 | (_logger).logErrorAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__) | |
888 | ||
7a72f18a PP |
889 | /* |
890 | * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger. | |
891 | */ | |
892 | #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \ | |
893 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_fmt), ##__VA_ARGS__) | |
894 | ||
895 | /* | |
896 | * Calls logErrorStrAndThrow() on `_logger` to log an error, append a | |
897 | * cause to the error of the current thread, and throw an instance of | |
898 | * `_exc_cls`. | |
899 | */ | |
900 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _msg) \ | |
901 | (_logger).logErrorStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_msg)) | |
902 | ||
903 | /* | |
904 | * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default | |
905 | * logger. | |
906 | */ | |
907 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _msg) \ | |
908 | BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_msg)) | |
909 | ||
910 | /* | |
911 | * Calls logErrorAndRethrow() on `_logger` to log an error, append a | |
912 | * cause to the error of the current thread, and throw an instance of | |
913 | * `_exc_cls`. | |
914 | */ | |
915 | #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \ | |
916 | (_logger).logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__) | |
917 | ||
918 | /* | |
919 | * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger. | |
920 | */ | |
921 | #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \ | |
922 | BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
923 | ||
924 | /* | |
925 | * Calls logErrorStrAndRethrow() on `_logger` to log an error, append a | |
926 | * cause to the error of the current thread, and throw an instance of | |
927 | * `_exc_cls`. | |
928 | */ | |
929 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \ | |
930 | (_logger).logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg)) | |
931 | ||
932 | /* | |
933 | * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default | |
934 | * logger. | |
935 | */ | |
936 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \ | |
937 | BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
938 | ||
2f505dde SM |
939 | /* |
940 | * Calls logErrno() on `_logger` with the `Level::Error` level to log an | |
941 | * error and append a cause to the error of the current thread. | |
942 | */ | |
943 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_logger, _init_msg, _fmt, ...) \ | |
944 | (_logger).logErrno<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \ | |
945 | (_init_msg), (_fmt), ##__VA_ARGS__) | |
946 | ||
947 | /* | |
948 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC() using the default logger. | |
949 | */ | |
950 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE(_init_msg, _fmt, ...) \ | |
951 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), ##__VA_ARGS__) | |
952 | ||
953 | /* | |
954 | * Calls logErrnoStr() on `_logger` with the `Level::Error` level to log | |
955 | * an error and append a cause to the error of the current thread. | |
956 | */ | |
957 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_logger, _init_msg, _msg) \ | |
958 | (_logger).logErrnoStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \ | |
959 | (_init_msg), (_msg)) | |
960 | ||
961 | /* | |
962 | * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC() using the default logger. | |
963 | */ | |
964 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE(_init_msg, _msg) \ | |
965 | BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg)) | |
966 | ||
7a72f18a PP |
967 | /* |
968 | * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a | |
969 | * cause to the error of the current thread, and throw an instance of | |
970 | * `_exc_cls`. | |
971 | */ | |
972 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _fmt, ...) \ | |
973 | (_logger).logErrorErrnoAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \ | |
974 | (_fmt), ##__VA_ARGS__) | |
975 | ||
976 | /* | |
977 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default | |
978 | * logger. | |
979 | */ | |
980 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \ | |
981 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \ | |
982 | (_fmt), ##__VA_ARGS__) | |
983 | ||
984 | /* | |
985 | * Calls logErrorErrnoStrAndThrow() on `_logger` to log an error, append | |
986 | * a cause to the error of the current thread, and throw an instance of | |
987 | * `_exc_cls`. | |
988 | */ | |
989 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _exc_cls, _init_msg, _msg) \ | |
990 | (_logger).logErrorErrnoStrAndThrow<true, _exc_cls>(__FILE__, __func__, __LINE__, (_init_msg), \ | |
991 | (_msg)) | |
992 | ||
993 | /* | |
994 | * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default | |
995 | * logger. | |
996 | */ | |
997 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _msg) \ | |
998 | BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _exc_cls, (_init_msg), \ | |
999 | (_msg)) | |
1000 | ||
1001 | /* | |
1002 | * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append | |
1003 | * a cause to the error of the current thread, and throw an instance of | |
1004 | * `_exc_cls`. | |
1005 | */ | |
1006 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _fmt, ...) \ | |
1007 | (_logger).logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_fmt), \ | |
1008 | ##__VA_ARGS__) | |
1009 | ||
1010 | /* | |
1011 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default | |
1012 | * logger. | |
1013 | */ | |
1014 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_init_msg, _fmt, ...) \ | |
1015 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_fmt), \ | |
1016 | ##__VA_ARGS__) | |
1017 | ||
1018 | /* | |
1019 | * Calls logErrorErrnoStrAndRethrow() on `_logger` to log an error, | |
1020 | * append a cause to the error of the current thread, and throw an | |
1021 | * instance of `_exc_cls`. | |
1022 | */ | |
1023 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _init_msg, _msg) \ | |
1024 | (_logger).logErrorErrnoStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_init_msg), (_msg)) | |
1025 | ||
1026 | /* | |
1027 | * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the | |
1028 | * default logger. | |
1029 | */ | |
1030 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW(_init_msg, _msg) \ | |
1031 | BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_init_msg), (_msg)) | |
1032 | ||
1033 | #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */ |