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