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 | ||
158 | /* | |
159 | * Current logging level converted to a `bt_log_level` value. | |
160 | * | |
161 | * For legacy code. | |
162 | */ | |
163 | bt_log_level cLevel() const noexcept | |
164 | { | |
165 | return static_cast<bt_log_level>(_mLevel); | |
166 | } | |
167 | ||
168 | /* | |
169 | * Whether or not this logger would log at the level `level`. | |
170 | */ | |
171 | bool wouldLog(const Level level) const noexcept | |
172 | { | |
173 | return BT_LOG_ON_CUR_LVL(static_cast<int>(level), static_cast<int>(_mLevel)); | |
174 | } | |
175 | ||
176 | /* | |
177 | * Whether or not this logger would log at the trace level. | |
178 | */ | |
179 | bool wouldLogT() const noexcept | |
180 | { | |
1c5ea5eb | 181 | return this->wouldLog(Level::Trace); |
7a72f18a PP |
182 | } |
183 | ||
184 | /* | |
185 | * Whether or not this logger would log at the debug level. | |
186 | */ | |
187 | bool wouldLogD() const noexcept | |
188 | { | |
1c5ea5eb | 189 | return this->wouldLog(Level::Debug); |
7a72f18a PP |
190 | } |
191 | ||
192 | /* | |
193 | * Whether or not this logger would log at the info level. | |
194 | */ | |
195 | bool wouldLogI() const noexcept | |
196 | { | |
1c5ea5eb | 197 | return this->wouldLog(Level::Info); |
7a72f18a PP |
198 | } |
199 | ||
200 | /* | |
201 | * Whether or not this logger would log at the warning level. | |
202 | */ | |
203 | bool wouldLogW() const noexcept | |
204 | { | |
1c5ea5eb | 205 | return this->wouldLog(Level::Warning); |
7a72f18a PP |
206 | } |
207 | ||
208 | /* | |
209 | * Whether or not this logger would log at the error level. | |
210 | */ | |
211 | bool wouldLogE() const noexcept | |
212 | { | |
1c5ea5eb | 213 | return this->wouldLog(Level::Error); |
7a72f18a PP |
214 | } |
215 | ||
216 | /* | |
217 | * Whether or not this logger would log at the fatal level. | |
218 | */ | |
219 | bool wouldLogF() const noexcept | |
220 | { | |
1c5ea5eb | 221 | return this->wouldLog(Level::Fatal); |
7a72f18a PP |
222 | } |
223 | ||
224 | /* | |
225 | * Logging tag. | |
226 | */ | |
227 | const std::string& tag() const noexcept | |
228 | { | |
229 | return _mTag; | |
230 | } | |
231 | ||
232 | /* | |
233 | * Self component class actor, or `bt2s::nullopt` if none. | |
234 | */ | |
235 | const bt2s::optional<bt2::SelfComponentClass>& selfCompCls() const noexcept | |
236 | { | |
237 | return _mSelfCompCls; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Self component actor, or `bt2s::nullopt` if none. | |
242 | */ | |
243 | const bt2s::optional<bt2::SelfComponent>& selfComp() const noexcept | |
244 | { | |
245 | return _mSelfComp; | |
246 | } | |
247 | ||
248 | /* | |
249 | * Self message iterator actor, or `bt2s::nullopt` if none. | |
250 | */ | |
251 | const bt2s::optional<bt2::SelfMessageIterator>& selfMsgIter() const noexcept | |
252 | { | |
253 | return _mSelfMsgIter; | |
254 | } | |
255 | ||
256 | /* | |
257 | * Name of module actor, or `bt2s::nullopt` if none. | |
258 | */ | |
259 | const bt2s::optional<std::string>& moduleName() const noexcept | |
260 | { | |
261 | return _mModuleName; | |
262 | } | |
263 | ||
264 | private: | |
265 | struct _StdLogWriter final | |
266 | { | |
267 | static void write(const char * const fileName, const char * const funcName, | |
53e1ecce PP |
268 | const unsigned lineNo, const Level level, const char * const tag, MemData, |
269 | const char * const initMsg, const char * const msg) noexcept | |
7a72f18a | 270 | { |
d88930bc | 271 | BT_ASSERT_DBG(initMsg && std::strcmp(initMsg, "") == 0); |
7a72f18a PP |
272 | bt_log_write(fileName, funcName, lineNo, static_cast<bt_log_level>(level), tag, msg); |
273 | } | |
274 | }; | |
275 | ||
276 | public: | |
277 | /* | |
278 | * Logs using the level `LevelV`. | |
279 | * | |
280 | * This method forwards `fmt` and `args` to fmt::format() to create | |
281 | * the log message. | |
282 | * | |
283 | * If `AppendCauseV` is true, this method also appends a cause to | |
284 | * the error of the current thread using the same message. | |
285 | */ | |
286 | template <Level LevelV, bool AppendCauseV, typename... ArgTs> | |
9d15c577 SM |
287 | void log(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
288 | const char * const fmt, ArgTs&&...args) const | |
7a72f18a | 289 | { |
53e1ecce PP |
290 | this->_log<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, {}, "", fmt, |
291 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
292 | } |
293 | ||
294 | /* | |
295 | * Logs `msg` using the level `LevelV`. | |
296 | * | |
297 | * If `AppendCauseV` is true, this method also appends a cause to | |
298 | * the error of the current thread using the same message. | |
299 | */ | |
300 | template <Level LevelV, bool AppendCauseV> | |
9d15c577 SM |
301 | void logStr(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
302 | const char * const msg) const | |
7a72f18a | 303 | { |
53e1ecce | 304 | this->_logStr<_StdLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, {}, "", msg); |
7a72f18a PP |
305 | } |
306 | ||
307 | /* | |
9d15c577 SM |
308 | * Like log() with the `Level::Error` level, but also throws a |
309 | * default-constructed instance of `ExcT`. | |
7a72f18a PP |
310 | */ |
311 | template <bool AppendCauseV, typename ExcT, typename... ArgTs> | |
312 | [[noreturn]] void logErrorAndThrow(const char * const fileName, const char * const funcName, | |
313 | const unsigned int lineNo, const char * const fmt, | |
314 | ArgTs&&...args) const | |
315 | { | |
9d15c577 SM |
316 | this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt, |
317 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
318 | throw ExcT {}; |
319 | } | |
320 | ||
321 | /* | |
9d15c577 SM |
322 | * Like logStr() with the `Level::Error` level, but also throws a |
323 | * default-constructed instance of `ExcT`. | |
7a72f18a PP |
324 | */ |
325 | template <bool AppendCauseV, typename ExcT> | |
326 | [[noreturn]] void logErrorStrAndThrow(const char * const fileName, const char * const funcName, | |
327 | const unsigned int lineNo, const char * const msg) const | |
328 | { | |
9d15c577 | 329 | this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg); |
7a72f18a PP |
330 | throw ExcT {}; |
331 | } | |
332 | ||
333 | /* | |
9d15c577 | 334 | * Like log() with the `Level::Error` level, but also rethrows. |
7a72f18a PP |
335 | */ |
336 | template <bool AppendCauseV, typename... ArgTs> | |
337 | [[noreturn]] void logErrorAndRethrow(const char * const fileName, const char * const funcName, | |
338 | const unsigned int lineNo, const char * const fmt, | |
339 | ArgTs&&...args) const | |
340 | { | |
9d15c577 SM |
341 | this->log<Level::Error, AppendCauseV>(fileName, funcName, lineNo, fmt, |
342 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
343 | throw; |
344 | } | |
345 | ||
346 | /* | |
9d15c577 | 347 | * Like logStr() with the `Level::Error` level, but also rethrows. |
7a72f18a PP |
348 | */ |
349 | template <bool AppendCauseV> | |
350 | [[noreturn]] void logErrorStrAndRethrow(const char * const fileName, | |
351 | const char * const funcName, const unsigned int lineNo, | |
352 | const char * const msg) const | |
353 | { | |
9d15c577 | 354 | this->logStr<Level::Error, AppendCauseV>(fileName, funcName, lineNo, msg); |
7a72f18a PP |
355 | throw; |
356 | } | |
357 | ||
358 | private: | |
d88930bc | 359 | struct _InitMsgLogWriter final |
7a72f18a PP |
360 | { |
361 | static void write(const char * const fileName, const char * const funcName, | |
53e1ecce PP |
362 | const unsigned lineNo, const Level level, const char * const tag, MemData, |
363 | const char * const initMsg, const char * const msg) noexcept | |
7a72f18a | 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 | { |
53e1ecce | 386 | this->_log<_InitMsgLogWriter, LevelV, AppendCauseV>(fileName, funcName, lineNo, {}, |
9d15c577 SM |
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>( |
53e1ecce | 406 | fileName, funcName, lineNo, {}, 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, | |
53e1ecce | 471 | const MemData memData, const char *, const char * const msg) noexcept |
7a72f18a PP |
472 | { |
473 | bt_log_write_mem(funcName, fileName, lineNo, static_cast<bt_log_level>(level), tag, | |
53e1ecce | 474 | memData.data(), memData.size(), msg); |
7a72f18a PP |
475 | } |
476 | }; | |
477 | ||
478 | public: | |
479 | /* | |
480 | * Logs memory data using the level `LevelV`. | |
481 | * | |
482 | * This method forwards `fmt` and `args` to fmt::format() to create | |
483 | * the log message. | |
484 | */ | |
485 | template <Level LevelV, typename... ArgTs> | |
9d15c577 | 486 | void logMem(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
53e1ecce | 487 | const MemData memData, const char * const fmt, ArgTs&&...args) const |
7a72f18a | 488 | { |
53e1ecce PP |
489 | this->_log<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, "", fmt, |
490 | std::forward<ArgTs>(args)...); | |
7a72f18a PP |
491 | } |
492 | ||
493 | /* | |
494 | * Logs memory data using the level `LevelV`, starting with the | |
495 | * message `msg`. | |
496 | */ | |
497 | template <Level LevelV> | |
9d15c577 | 498 | void logMemStr(const char * const fileName, const char * const funcName, |
53e1ecce | 499 | const unsigned int lineNo, const MemData memData, const char * const msg) const |
7a72f18a | 500 | { |
53e1ecce | 501 | this->_logStr<_MemLogWriter, LevelV, false>(fileName, funcName, lineNo, memData, "", msg); |
7a72f18a PP |
502 | } |
503 | ||
504 | private: | |
505 | /* | |
506 | * Formats a log message with fmt::format() given `fmt` and `args`, | |
9d15c577 | 507 | * and then forwards everything to _logStr(). |
7a72f18a PP |
508 | */ |
509 | template <typename LogWriterT, Level LevelV, bool AppendCauseV, typename... ArgTs> | |
9d15c577 | 510 | void _log(const char * const fileName, const char * const funcName, const unsigned int lineNo, |
53e1ecce PP |
511 | const MemData memData, const char * const initMsg, const char * const fmt, |
512 | ArgTs&&...args) const | |
7a72f18a PP |
513 | { |
514 | /* Only format arguments if logging or appending an error cause */ | |
515 | if (G_UNLIKELY(this->wouldLog(LevelV) || AppendCauseV)) { | |
516 | /* | |
517 | * Format arguments to our buffer (fmt::format_to() doesn't | |
518 | * append a null character). | |
519 | */ | |
520 | _mBuf.clear(); | |
521 | fmt::format_to(std::back_inserter(_mBuf), fmt, std::forward<ArgTs>(args)...); | |
522 | _mBuf.push_back('\0'); | |
523 | } | |
524 | ||
53e1ecce | 525 | this->_logStr<LogWriterT, LevelV, AppendCauseV>(fileName, funcName, lineNo, memData, |
9d15c577 | 526 | initMsg, _mBuf.data()); |
7a72f18a PP |
527 | } |
528 | ||
529 | /* | |
d88930bc PP |
530 | * Calls LogWriterT::write() with its arguments to log using the |
531 | * level `LevelV`. | |
7a72f18a PP |
532 | * |
533 | * If `AppendCauseV` is true, this method also appends a cause to | |
d88930bc PP |
534 | * the error of the current thread using the concatenation of |
535 | * `initMsg` and `msg` as the message. | |
7a72f18a PP |
536 | */ |
537 | template <typename LogWriterT, Level LevelV, bool AppendCauseV> | |
9d15c577 | 538 | void _logStr(const char * const fileName, const char * const funcName, |
53e1ecce PP |
539 | const unsigned int lineNo, const MemData memData, const char * const initMsg, |
540 | const char * const msg) const | |
7a72f18a | 541 | { |
d88930bc PP |
542 | /* Initial message and main message are required */ |
543 | BT_ASSERT(initMsg); | |
544 | BT_ASSERT(msg); | |
545 | ||
7a72f18a PP |
546 | /* Log if needed */ |
547 | if (this->wouldLog(LevelV)) { | |
53e1ecce PP |
548 | LogWriterT::write(fileName, funcName, lineNo, LevelV, _mTag.data(), memData, initMsg, |
549 | msg); | |
7a72f18a PP |
550 | } |
551 | ||
552 | /* Append an error cause if needed */ | |
553 | if (AppendCauseV) { | |
554 | if (_mSelfMsgIter) { | |
555 | bt_current_thread_error_append_cause_from_message_iterator( | |
bdc900ed | 556 | _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg); |
7a72f18a PP |
557 | } else if (_mSelfComp) { |
558 | bt_current_thread_error_append_cause_from_component( | |
bdc900ed | 559 | _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg); |
7a72f18a PP |
560 | } else if (_mSelfCompCls) { |
561 | bt_current_thread_error_append_cause_from_component_class( | |
bdc900ed | 562 | _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg); |
7a72f18a | 563 | } else { |
d88930bc | 564 | BT_ASSERT(_mModuleName); |
bdc900ed PP |
565 | bt_current_thread_error_append_cause_from_unknown(_mModuleName->data(), fileName, |
566 | lineNo, "%s%s", initMsg, msg); | |
7a72f18a PP |
567 | } |
568 | } | |
569 | } | |
570 | ||
d88930bc PP |
571 | static std::string _errnoIntroStr(const char * const initMsg) |
572 | { | |
573 | BT_ASSERT(errno != 0); | |
574 | return fmt::format("{}: {}", initMsg, g_strerror(errno)); | |
575 | } | |
576 | ||
4f235e19 | 577 | /* Exactly one of the following four members has a value */ |
7a72f18a PP |
578 | bt2s::optional<bt2::SelfComponentClass> _mSelfCompCls; |
579 | bt2s::optional<bt2::SelfComponent> _mSelfComp; | |
580 | bt2s::optional<bt2::SelfMessageIterator> _mSelfMsgIter; | |
581 | bt2s::optional<std::string> _mModuleName; | |
582 | ||
583 | /* Current logging level */ | |
584 | Level _mLevel; | |
585 | ||
586 | /* Logging tag */ | |
587 | std::string _mTag; | |
588 | ||
589 | /* Formatting buffer */ | |
590 | mutable std::vector<char> _mBuf; | |
591 | }; | |
592 | ||
3c20ac12 SM |
593 | /* |
594 | * Returns `s` if it's not `nullptr`, or the `(null)` string otherwise. | |
595 | */ | |
596 | inline const char *maybeNull(const char * const s) noexcept | |
597 | { | |
598 | return s ? s : "(null)"; | |
599 | } | |
600 | ||
7a72f18a PP |
601 | } /* namespace bt2c */ |
602 | ||
603 | /* Internal: default logger name */ | |
604 | #define _BT_CPPLOG_DEF_LOGGER _mLogger | |
605 | ||
606 | /* | |
9d15c577 | 607 | * Calls log() on `_logger` to log using the level `_lvl`. |
7a72f18a PP |
608 | */ |
609 | #define BT_CPPLOG_EX(_lvl, _logger, _fmt, ...) \ | |
610 | do { \ | |
611 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
e40ac8f6 PP |
612 | (_logger).template log<(_lvl), false>(__FILE__, __func__, __LINE__, (_fmt), \ |
613 | ##__VA_ARGS__); \ | |
7a72f18a PP |
614 | } \ |
615 | } while (0) | |
616 | ||
617 | /* | |
618 | * BT_CPPLOG_EX() with specific logging levels. | |
619 | */ | |
620 | #define BT_CPPLOGT_SPEC(_logger, _fmt, ...) \ | |
1c5ea5eb | 621 | BT_CPPLOG_EX(bt2c::Logger::Level::Trace, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 622 | #define BT_CPPLOGD_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 623 | BT_CPPLOG_EX(bt2c::Logger::Level::Debug, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 624 | #define BT_CPPLOGI_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 625 | BT_CPPLOG_EX(bt2c::Logger::Level::Info, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 626 | #define BT_CPPLOGW_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 627 | BT_CPPLOG_EX(bt2c::Logger::Level::Warning, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 628 | #define BT_CPPLOGE_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 629 | BT_CPPLOG_EX(bt2c::Logger::Level::Error, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a | 630 | #define BT_CPPLOGF_SPEC(_logger, _fmt, ...) \ |
1c5ea5eb | 631 | BT_CPPLOG_EX(bt2c::Logger::Level::Fatal, (_logger), (_fmt), ##__VA_ARGS__) |
7a72f18a PP |
632 | |
633 | /* | |
634 | * BT_CPPLOG_EX() with specific logging levels and using the default | |
635 | * logger. | |
636 | */ | |
637 | #define BT_CPPLOGT(_fmt, ...) BT_CPPLOGT_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
638 | #define BT_CPPLOGD(_fmt, ...) BT_CPPLOGD_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
639 | #define BT_CPPLOGI(_fmt, ...) BT_CPPLOGI_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
640 | #define BT_CPPLOGW(_fmt, ...) BT_CPPLOGW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
641 | #define BT_CPPLOGE(_fmt, ...) BT_CPPLOGE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
642 | #define BT_CPPLOGF(_fmt, ...) BT_CPPLOGF_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
643 | ||
644 | /* | |
9d15c577 | 645 | * Calls logStr() on `_logger` to log using the level `_lvl`. |
7a72f18a PP |
646 | */ |
647 | #define BT_CPPLOG_STR_EX(_lvl, _logger, _msg) \ | |
e40ac8f6 | 648 | (_logger).template logStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_msg)) |
7a72f18a PP |
649 | |
650 | /* | |
651 | * BT_CPPLOG_STR_EX() with specific logging levels. | |
652 | */ | |
653 | #define BT_CPPLOGT_STR_SPEC(_logger, _msg) \ | |
1c5ea5eb | 654 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_msg)) |
7a72f18a | 655 | #define BT_CPPLOGD_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 656 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_msg)) |
7a72f18a | 657 | #define BT_CPPLOGI_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 658 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Info, (_logger), (_msg)) |
7a72f18a | 659 | #define BT_CPPLOGW_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 660 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_msg)) |
7a72f18a | 661 | #define BT_CPPLOGE_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 662 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Error, (_logger), (_msg)) |
7a72f18a | 663 | #define BT_CPPLOGF_STR_SPEC(_logger, _msg) \ |
1c5ea5eb | 664 | BT_CPPLOG_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_msg)) |
7a72f18a PP |
665 | |
666 | /* | |
667 | * BT_CPPLOG_STR_EX() with specific logging levels and using the default | |
668 | * logger. | |
669 | */ | |
670 | #define BT_CPPLOGT_STR(_msg) BT_CPPLOGT_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
671 | #define BT_CPPLOGD_STR(_msg) BT_CPPLOGD_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
672 | #define BT_CPPLOGI_STR(_msg) BT_CPPLOGI_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
673 | #define BT_CPPLOGW_STR(_msg) BT_CPPLOGW_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
674 | #define BT_CPPLOGE_STR(_msg) BT_CPPLOGE_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
675 | #define BT_CPPLOGF_STR(_msg) BT_CPPLOGF_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
676 | ||
677 | /* | |
9d15c577 | 678 | * Calls logMem() on `_logger` to log using the level `_lvl`. |
7a72f18a | 679 | */ |
53e1ecce | 680 | #define BT_CPPLOG_MEM_EX(_lvl, _logger, _memData, _fmt, ...) \ |
7a72f18a PP |
681 | do { \ |
682 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
e40ac8f6 PP |
683 | (_logger).template logMem<(_lvl)>(__FILE__, __func__, __LINE__, (_memData), (_fmt), \ |
684 | ##__VA_ARGS__); \ | |
7a72f18a PP |
685 | } \ |
686 | } while (0) | |
687 | ||
688 | /* | |
689 | * BT_CPPLOG_MEM_EX() with specific logging levels. | |
690 | */ | |
53e1ecce PP |
691 | #define BT_CPPLOGT_MEM_SPEC(_logger, _memData, _fmt, ...) \ |
692 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Trace, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
693 | #define BT_CPPLOGD_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
694 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Debug, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
695 | #define BT_CPPLOGI_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
696 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Info, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
697 | #define BT_CPPLOGW_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
698 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Warning, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
699 | #define BT_CPPLOGE_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
700 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Error, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
701 | #define BT_CPPLOGF_MEM_SPEC(_logger, _memData, _fmt, ...) \ | |
702 | BT_CPPLOG_MEM_EX(bt2c::Logger::Level::Fatal, (_logger), (_memData), (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
703 | |
704 | /* | |
705 | * BT_CPPLOG_MEM_EX() with specific logging levels and using the default | |
706 | * logger. | |
707 | */ | |
53e1ecce PP |
708 | #define BT_CPPLOGT_MEM(_memData, _fmt, ...) \ |
709 | BT_CPPLOGT_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
710 | #define BT_CPPLOGD_MEM(_memData, _fmt, ...) \ | |
711 | BT_CPPLOGD_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
712 | #define BT_CPPLOGI_MEM(_memData, _fmt, ...) \ | |
713 | BT_CPPLOGI_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
714 | #define BT_CPPLOGW_MEM(_memData, _fmt, ...) \ | |
715 | BT_CPPLOGW_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
716 | #define BT_CPPLOGE_MEM(_memData, _fmt, ...) \ | |
717 | BT_CPPLOGE_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
718 | #define BT_CPPLOGF_MEM(_memData, _fmt, ...) \ | |
719 | BT_CPPLOGF_MEM_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
720 | |
721 | /* | |
9d15c577 | 722 | * Calls logMemStr() on `_logger` to log using the level `_lvl`. |
7a72f18a | 723 | */ |
53e1ecce | 724 | #define BT_CPPLOG_MEM_STR_EX(_lvl, _logger, _memData, _msg) \ |
e40ac8f6 | 725 | (_logger).template logMemStr<(_lvl)>(__FILE__, __func__, __LINE__, (_memData), (_msg)) |
7a72f18a PP |
726 | |
727 | /* | |
728 | * BT_CPPLOG_MEM_STR_EX() with specific logging levels. | |
729 | */ | |
53e1ecce PP |
730 | #define BT_CPPLOGT_MEM_STR_SPEC(_logger, _memData, _msg) \ |
731 | BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::TRACE, (_logger), (_memData), (_msg)) | |
732 | #define BT_CPPLOGD_MEM_STR_SPEC(_logger, _memData, _msg) \ | |
733 | BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::DEBUG, (_logger), (_memData), (_msg)) | |
734 | #define BT_CPPLOGI_MEM_STR_SPEC(_logger, _memData, _msg) \ | |
735 | BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::INFO, (_logger), (_memData), (_msg)) | |
736 | #define BT_CPPLOGW_MEM_STR_SPEC(_logger, _memData, _msg) \ | |
737 | BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::WARNING, (_logger), (_memData), (_msg)) | |
738 | #define BT_CPPLOGE_MEM_STR_SPEC(_logger, _memData, _msg) \ | |
739 | BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::Error, (_logger), (_memData), (_msg)) | |
740 | #define BT_CPPLOGF_MEM_STR_SPEC(_logger, _memData, _msg) \ | |
741 | BT_CPPLOG_MEM_STR_EX(bt2c::Logger::Level::FATAL, (_logger), (_memData), (_msg)) | |
7a72f18a PP |
742 | |
743 | /* | |
744 | * BT_CPPLOG_MEM_STR_EX() with specific logging levels and using the | |
745 | * default logger. | |
746 | */ | |
53e1ecce PP |
747 | #define BT_CPPLOGT_MEM_STR(_memData, _msg) \ |
748 | BT_CPPLOGT_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_msg)) | |
749 | #define BT_CPPLOGD_MEM_STR(_memData, _msg) \ | |
750 | BT_CPPLOGD_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_msg)) | |
751 | #define BT_CPPLOGI_MEM_STR(_memData, _msg) \ | |
752 | BT_CPPLOGI_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_msg)) | |
753 | #define BT_CPPLOGW_MEM_STR(_memData, _msg) \ | |
754 | BT_CPPLOGW_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_msg)) | |
755 | #define BT_CPPLOGE_MEM_STR(_memData, _msg) \ | |
756 | BT_CPPLOGE_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_msg)) | |
757 | #define BT_CPPLOGF_MEM_STR(_memData, _msg) \ | |
758 | BT_CPPLOGF_MEM_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_memData), (_msg)) | |
7a72f18a PP |
759 | |
760 | /* | |
9d15c577 | 761 | * Calls logErrno() on `_logger` to log using the level `_lvl` and |
b5acd2ad | 762 | * initial message `_initMsg`. |
7a72f18a | 763 | */ |
b5acd2ad | 764 | #define BT_CPPLOG_ERRNO_EX(_lvl, _logger, _initMsg, _fmt, ...) \ |
7a72f18a PP |
765 | do { \ |
766 | if (G_UNLIKELY((_logger).wouldLog(_lvl))) { \ | |
e40ac8f6 PP |
767 | (_logger).template logErrno<(_lvl), false>(__FILE__, __func__, __LINE__, (_initMsg), \ |
768 | (_fmt), ##__VA_ARGS__); \ | |
7a72f18a PP |
769 | } \ |
770 | } while (0) | |
771 | ||
772 | /* | |
773 | * BT_CPPLOG_ERRNO_EX() with specific logging levels. | |
774 | */ | |
b5acd2ad PP |
775 | #define BT_CPPLOGT_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ |
776 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Trace, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
777 | #define BT_CPPLOGD_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
778 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Debug, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
779 | #define BT_CPPLOGI_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
780 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Info, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
781 | #define BT_CPPLOGW_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
782 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Warning, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
783 | #define BT_CPPLOGE_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
784 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Error, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
785 | #define BT_CPPLOGF_ERRNO_SPEC(_logger, _initMsg, _fmt, ...) \ | |
786 | BT_CPPLOG_ERRNO_EX(bt2c::Logger::Level::Fatal, (_logger), (_initMsg), (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
787 | |
788 | /* | |
789 | * BT_CPPLOG_ERRNO_EX() with specific logging levels and using the | |
790 | * default logger. | |
791 | */ | |
b5acd2ad PP |
792 | #define BT_CPPLOGT_ERRNO(_initMsg, _fmt, ...) \ |
793 | BT_CPPLOGT_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
794 | #define BT_CPPLOGD_ERRNO(_initMsg, _fmt, ...) \ | |
795 | BT_CPPLOGD_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
796 | #define BT_CPPLOGI_ERRNO(_initMsg, _fmt, ...) \ | |
797 | BT_CPPLOGI_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
798 | #define BT_CPPLOGW_ERRNO(_initMsg, _fmt, ...) \ | |
799 | BT_CPPLOGW_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
800 | #define BT_CPPLOGE_ERRNO(_initMsg, _fmt, ...) \ | |
801 | BT_CPPLOGE_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
802 | #define BT_CPPLOGF_ERRNO(_initMsg, _fmt, ...) \ | |
803 | BT_CPPLOGF_ERRNO_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
804 | |
805 | /* | |
9d15c577 | 806 | * Calls logErrnoStr() on `_logger` to log using the level `_lvl` and |
b5acd2ad | 807 | * initial message `_initMsg`. |
7a72f18a | 808 | */ |
b5acd2ad | 809 | #define BT_CPPLOG_ERRNO_STR_EX(_lvl, _logger, _initMsg, _msg) \ |
e40ac8f6 | 810 | (_logger).template logErrnoStr<(_lvl), false>(__FILE__, __func__, __LINE__, (_initMsg), (_msg)) |
7a72f18a PP |
811 | |
812 | /* | |
813 | * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels. | |
814 | */ | |
b5acd2ad PP |
815 | #define BT_CPPLOGT_ERRNO_STR_SPEC(_logger, _initMsg, _msg) \ |
816 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Trace, (_logger), (_initMsg), (_msg)) | |
817 | #define BT_CPPLOGD_ERRNO_STR_SPEC(_logger, _initMsg, _msg) \ | |
818 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Debug, (_logger), (_initMsg), (_msg)) | |
819 | #define BT_CPPLOGI_ERRNO_STR_SPEC(_logger, _initMsg, _msg) \ | |
820 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Info, (_logger), (_initMsg), (_msg)) | |
821 | #define BT_CPPLOGW_ERRNO_STR_SPEC(_logger, _initMsg, _msg) \ | |
822 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Warning, (_logger), (_initMsg), (_msg)) | |
823 | #define BT_CPPLOGE_ERRNO_STR_SPEC(_logger, _initMsg, _msg) \ | |
824 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Error, (_logger), (_initMsg), (_msg)) | |
825 | #define BT_CPPLOGF_ERRNO_STR_SPEC(_logger, _initMsg, _msg) \ | |
826 | BT_CPPLOG_ERRNO_STR_EX(bt2c::Logger::Level::Fatal, (_logger), (_initMsg), (_msg)) | |
7a72f18a PP |
827 | |
828 | /* | |
829 | * BT_CPPLOG_ERRNO_STR_EX() with specific logging levels and using the | |
830 | * default logger. | |
831 | */ | |
b5acd2ad PP |
832 | #define BT_CPPLOGT_ERRNO_STR(_initMsg, _msg) \ |
833 | BT_CPPLOGT_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_msg)) | |
834 | #define BT_CPPLOGD_ERRNO_STR(_initMsg, _msg) \ | |
835 | BT_CPPLOGD_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_msg)) | |
836 | #define BT_CPPLOGI_ERRNO_STR(_initMsg, _msg) \ | |
837 | BT_CPPLOGI_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_msg)) | |
838 | #define BT_CPPLOGW_ERRNO_STR(_initMsg, _msg) \ | |
839 | BT_CPPLOGW_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_msg)) | |
840 | #define BT_CPPLOGE_ERRNO_STR(_initMsg, _msg) \ | |
841 | BT_CPPLOGE_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_msg)) | |
842 | #define BT_CPPLOGF_ERRNO_STR(_initMsg, _msg) \ | |
843 | BT_CPPLOGF_ERRNO_STR_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_msg)) | |
7a72f18a | 844 | |
aa37512c SM |
845 | /* |
846 | * Calls log() on `_logger` with the `Error` level to log an error and | |
847 | * append a cause to the error of the current thread. | |
848 | */ | |
849 | #define BT_CPPLOGE_APPEND_CAUSE_SPEC(_logger, _fmt, ...) \ | |
e40ac8f6 PP |
850 | (_logger).template log<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, (_fmt), \ |
851 | ##__VA_ARGS__) | |
aa37512c SM |
852 | |
853 | /* | |
854 | * BT_CPPLOGE_APPEND_CAUSE_SPEC() using the default logger. | |
855 | */ | |
856 | #define BT_CPPLOGE_APPEND_CAUSE(_fmt, ...) \ | |
857 | BT_CPPLOGE_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
858 | ||
859 | /* | |
860 | * Calls logStr() on `_logger` with the `Error` level to log an error and | |
861 | * append a cause to the error of the current thread. | |
862 | */ | |
863 | #define BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_logger, _msg) \ | |
e40ac8f6 PP |
864 | (_logger).template logStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \ |
865 | (_msg)) | |
aa37512c SM |
866 | |
867 | /* | |
868 | * BT_CPPLOGE_STR_APPEND_CAUSE_SPEC() using the default logger. | |
869 | */ | |
870 | #define BT_CPPLOGE_STR_APPEND_CAUSE(_msg) \ | |
871 | BT_CPPLOGE_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
872 | ||
7a72f18a PP |
873 | /* |
874 | * Calls logErrorAndThrow() on `_logger` to log an error, append a cause | |
875 | * to the error of the current thread, and throw an instance of | |
b5acd2ad | 876 | * `_excCls`. |
7a72f18a | 877 | */ |
b5acd2ad | 878 | #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_logger, _excCls, _fmt, ...) \ |
e40ac8f6 PP |
879 | (_logger).template logErrorAndThrow<true, _excCls>(__FILE__, __func__, __LINE__, (_fmt), \ |
880 | ##__VA_ARGS__) | |
7a72f18a | 881 | |
7a72f18a PP |
882 | /* |
883 | * BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC() using the default logger. | |
884 | */ | |
b5acd2ad PP |
885 | #define BT_CPPLOGE_APPEND_CAUSE_AND_THROW(_excCls, _fmt, ...) \ |
886 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _excCls, (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
887 | |
888 | /* | |
889 | * Calls logErrorStrAndThrow() on `_logger` to log an error, append a | |
890 | * cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 891 | * `_excCls`. |
7a72f18a | 892 | */ |
b5acd2ad | 893 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _excCls, _msg) \ |
e40ac8f6 | 894 | (_logger).template logErrorStrAndThrow<true, _excCls>(__FILE__, __func__, __LINE__, (_msg)) |
7a72f18a PP |
895 | |
896 | /* | |
897 | * BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default | |
898 | * logger. | |
899 | */ | |
b5acd2ad PP |
900 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW(_excCls, _msg) \ |
901 | BT_CPPLOGE_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _excCls, (_msg)) | |
7a72f18a PP |
902 | |
903 | /* | |
904 | * Calls logErrorAndRethrow() on `_logger` to log an error, append a | |
905 | * cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 906 | * `_excCls`. |
7a72f18a PP |
907 | */ |
908 | #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _fmt, ...) \ | |
e40ac8f6 | 909 | (_logger).template logErrorAndRethrow<true>(__FILE__, __func__, __LINE__, (_fmt), ##__VA_ARGS__) |
7a72f18a PP |
910 | |
911 | /* | |
912 | * BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC() using the default logger. | |
913 | */ | |
914 | #define BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \ | |
915 | BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_fmt), ##__VA_ARGS__) | |
916 | ||
917 | /* | |
918 | * Calls logErrorStrAndRethrow() on `_logger` to log an error, append a | |
919 | * cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 920 | * `_excCls`. |
7a72f18a PP |
921 | */ |
922 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _msg) \ | |
e40ac8f6 | 923 | (_logger).template logErrorStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_msg)) |
7a72f18a PP |
924 | |
925 | /* | |
926 | * BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the default | |
927 | * logger. | |
928 | */ | |
929 | #define BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW(_msg) \ | |
930 | BT_CPPLOGE_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_msg)) | |
931 | ||
2f505dde SM |
932 | /* |
933 | * Calls logErrno() on `_logger` with the `Level::Error` level to log an | |
934 | * error and append a cause to the error of the current thread. | |
935 | */ | |
b5acd2ad | 936 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_logger, _initMsg, _fmt, ...) \ |
e40ac8f6 PP |
937 | (_logger).template logErrno<bt2c::Logger::Level::Error, true>( \ |
938 | __FILE__, __func__, __LINE__, (_initMsg), (_fmt), ##__VA_ARGS__) | |
2f505dde SM |
939 | |
940 | /* | |
941 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC() using the default logger. | |
942 | */ | |
b5acd2ad PP |
943 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE(_initMsg, _fmt, ...) \ |
944 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), ##__VA_ARGS__) | |
2f505dde SM |
945 | |
946 | /* | |
947 | * Calls logErrnoStr() on `_logger` with the `Level::Error` level to log | |
948 | * an error and append a cause to the error of the current thread. | |
949 | */ | |
b5acd2ad | 950 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_logger, _initMsg, _msg) \ |
e40ac8f6 PP |
951 | (_logger).template logErrnoStr<bt2c::Logger::Level::Error, true>(__FILE__, __func__, __LINE__, \ |
952 | (_initMsg), (_msg)) | |
2f505dde SM |
953 | |
954 | /* | |
955 | * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC() using the default logger. | |
956 | */ | |
b5acd2ad PP |
957 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE(_initMsg, _msg) \ |
958 | BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_msg)) | |
2f505dde | 959 | |
7a72f18a PP |
960 | /* |
961 | * Calls logErrorErrnoAndThrow() on `_logger` to log an error, append a | |
962 | * cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 963 | * `_excCls`. |
7a72f18a | 964 | */ |
b5acd2ad | 965 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_logger, _excCls, _initMsg, _fmt, ...) \ |
e40ac8f6 PP |
966 | (_logger).template logErrorErrnoAndThrow<true, _excCls>(__FILE__, __func__, __LINE__, \ |
967 | (_initMsg), (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
968 | |
969 | /* | |
970 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC() using the default | |
971 | * logger. | |
972 | */ | |
b5acd2ad PP |
973 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_excCls, _initMsg, _fmt, ...) \ |
974 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _excCls, (_initMsg), \ | |
7a72f18a PP |
975 | (_fmt), ##__VA_ARGS__) |
976 | ||
977 | /* | |
978 | * Calls logErrorErrnoStrAndThrow() on `_logger` to log an error, append | |
979 | * a cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 980 | * `_excCls`. |
7a72f18a | 981 | */ |
b5acd2ad | 982 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_logger, _excCls, _initMsg, _msg) \ |
e40ac8f6 PP |
983 | (_logger).template logErrorErrnoStrAndThrow<true, _excCls>(__FILE__, __func__, __LINE__, \ |
984 | (_initMsg), (_msg)) | |
7a72f18a PP |
985 | |
986 | /* | |
987 | * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC() using the default | |
988 | * logger. | |
989 | */ | |
b5acd2ad PP |
990 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW(_excCls, _initMsg, _msg) \ |
991 | BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_THROW_SPEC(_BT_CPPLOG_DEF_LOGGER, _excCls, (_initMsg), \ | |
7a72f18a PP |
992 | (_msg)) |
993 | ||
994 | /* | |
995 | * Calls logErrorErrnoAndRethrow() on `_logger` to log an error, append | |
996 | * a cause to the error of the current thread, and throw an instance of | |
b5acd2ad | 997 | * `_excCls`. |
7a72f18a | 998 | */ |
b5acd2ad | 999 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _initMsg, _fmt, ...) \ |
e40ac8f6 PP |
1000 | (_logger).template logErrorErrnoAndRethrow<true>(__FILE__, __func__, __LINE__, (_initMsg), \ |
1001 | (_fmt), ##__VA_ARGS__) | |
7a72f18a PP |
1002 | |
1003 | /* | |
1004 | * BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC() using the default | |
1005 | * logger. | |
1006 | */ | |
b5acd2ad PP |
1007 | #define BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_initMsg, _fmt, ...) \ |
1008 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_fmt), \ | |
7a72f18a PP |
1009 | ##__VA_ARGS__) |
1010 | ||
1011 | /* | |
1012 | * Calls logErrorErrnoStrAndRethrow() on `_logger` to log an error, | |
1013 | * append a cause to the error of the current thread, and throw an | |
b5acd2ad | 1014 | * instance of `_excCls`. |
7a72f18a | 1015 | */ |
b5acd2ad | 1016 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_logger, _initMsg, _msg) \ |
e40ac8f6 PP |
1017 | (_logger).template logErrorErrnoStrAndRethrow<true>(__FILE__, __func__, __LINE__, (_initMsg), \ |
1018 | (_msg)) | |
7a72f18a PP |
1019 | |
1020 | /* | |
1021 | * BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC() using the | |
1022 | * default logger. | |
1023 | */ | |
b5acd2ad PP |
1024 | #define BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW(_initMsg, _msg) \ |
1025 | BT_CPPLOGE_ERRNO_STR_APPEND_CAUSE_AND_RETHROW_SPEC(_BT_CPPLOG_DEF_LOGGER, (_initMsg), (_msg)) | |
7a72f18a PP |
1026 | |
1027 | #endif /* BABELTRACE_CPP_COMMON_BT2C_LOGGING_HPP */ |