2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
8 #ifndef BABELTRACE_CPP_COMMON_LOG_CFG_HPP
9 #define BABELTRACE_CPP_COMMON_LOG_CFG_HPP
11 #include <babeltrace2/babeltrace.h>
12 #include "common/assert.h"
14 namespace bt2_common {
17 * Logging configuration.
19 * A logging configuration object contains all the execution context
22 * • Log, providing the name of some current component or component
25 * • Append a cause to the error of the current thread from the right
28 * For a given logging configuration `L`, the guarantees are as such:
30 * If `L.selfMsgIter()` isn't `nullptr`:
31 * • `L.selfComp()` isn't `nullptr`.
32 * • `L.compCls()` isn't `nullptr`.
33 * • `L.compName()` isn't `nullptr`.
34 * • `L.compClsName()` isn't `nullptr`.
35 * • `L.moduleName()` is `nullptr`.
37 * If `L.selfComp()` isn't `nullptr`:
38 * • `L.compCls()` isn't `nullptr`.
39 * • `L.compName()` isn't `nullptr`.
40 * • `L.compClsName()` isn't `nullptr`.
41 * • `L.moduleName()` is `nullptr`.
43 * If `L.selfCompCls()` isn't `nullptr`:
44 * • `L.compCls()` isn't `nullptr`.
45 * • `L.compClsName()` isn't `nullptr`.
46 * • `L.moduleName()` is `nullptr`.
48 * If `L.compCls()` isn't `nullptr`:
49 * • `L.compClsName()` isn't `nullptr`.
50 * • `L.moduleName()` is `nullptr`.
52 * If `L.moduleName()` isn't `nullptr`:
53 * • `L.selfMsgIter()` is `nullptr`.
54 * • `L.selfComp()` is `nullptr`.
55 * • `L.compCls()` is `nullptr`.
56 * • `L.compName()` is `nullptr`.
57 * • `L.compClsName()` is `nullptr`.
62 explicit LogCfg(const bt_logging_level logLevel, bt_self_message_iterator& selfMsgIter) :
63 _mLogLevel {logLevel}, _mSelfMsgIter {&selfMsgIter},
64 _mSelfComp {bt_self_message_iterator_borrow_component(&selfMsgIter)},
65 _mCompCls {&this->_compClsFromSelfComp(*_mSelfComp)}
69 explicit LogCfg(const bt_logging_level logLevel, bt_self_component& selfComp) :
70 _mLogLevel {logLevel}, _mSelfComp {&selfComp}, _mCompCls {
71 &this->_compClsFromSelfComp(*_mSelfComp)}
75 explicit LogCfg(const bt_logging_level logLevel, bt_self_component_class& selfCompCls) :
76 _mLogLevel {logLevel}, _mSelfCompCls {&selfCompCls},
77 _mCompCls {bt_self_component_class_as_component_class(&selfCompCls)}
81 explicit LogCfg(const bt_logging_level logLevel, const char * const moduleName) :
82 _mLogLevel {logLevel}, _mModuleName {moduleName}
84 BT_ASSERT_DBG(_mModuleName);
87 LogCfg(const LogCfg&) = default;
88 LogCfg& operator=(const LogCfg&) = default;
90 bt_logging_level logLevel() const noexcept
95 bt_self_component *selfComp() const noexcept
100 const char *compName() const noexcept
102 BT_ASSERT_DBG(_mSelfComp);
103 return bt_component_get_name(bt_self_component_as_component(_mSelfComp));
106 bt_self_component_class *selfCompCls() const noexcept
108 return _mSelfCompCls;
111 const bt_component_class *compCls() const noexcept
116 const char *compClsName() const noexcept
118 BT_ASSERT_DBG(_mCompCls);
119 return bt_component_class_get_name(_mCompCls);
122 bt_self_message_iterator *selfMsgIter() const noexcept
124 return _mSelfMsgIter;
127 const char *moduleName() const noexcept
133 static const bt_component_class& _compClsFromSelfComp(bt_self_component& selfComp) noexcept
135 return *bt_component_borrow_class_const(bt_self_component_as_component(&selfComp));
138 bt_logging_level _mLogLevel;
139 bt_self_message_iterator *_mSelfMsgIter = nullptr;
140 bt_self_component *_mSelfComp = nullptr;
141 bt_self_component_class *_mSelfCompCls = nullptr;
142 const bt_component_class *_mCompCls = nullptr;
143 const char *_mModuleName = nullptr;
146 } /* namespace bt2_common */
148 #endif /* BABELTRACE_CPP_COMMON_LOG_CFG_HPP */