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