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>
13 #include "common/assert.h"
15 namespace bt2_common {
18 * Logging configuration.
20 * A logging configuration object contains all the execution context
23 * • Log, providing the name of some current component or component
26 * • Append a cause to the error of the current thread from the right
29 * For a given logging configuration `L`, the guarantees are as such:
31 * If `L.selfMsgIter()` isn't `nullptr`:
32 * • `L.selfComp()` isn't `nullptr`.
33 * • `L.compCls()` isn't `nullptr`.
34 * • `L.compName()` isn't `nullptr`.
35 * • `L.compClsName()` isn't `nullptr`.
36 * • `L.moduleName()` is `nullptr`.
38 * If `L.selfComp()` isn't `nullptr`:
39 * • `L.compCls()` isn't `nullptr`.
40 * • `L.compName()` isn't `nullptr`.
41 * • `L.compClsName()` isn't `nullptr`.
42 * • `L.moduleName()` is `nullptr`.
44 * If `L.selfCompCls()` isn't `nullptr`:
45 * • `L.compCls()` isn't `nullptr`.
46 * • `L.compClsName()` isn't `nullptr`.
47 * • `L.moduleName()` is `nullptr`.
49 * If `L.compCls()` isn't `nullptr`:
50 * • `L.compClsName()` isn't `nullptr`.
51 * • `L.moduleName()` is `nullptr`.
53 * If `L.moduleName()` isn't `nullptr`:
54 * • `L.selfMsgIter()` is `nullptr`.
55 * • `L.selfComp()` is `nullptr`.
56 * • `L.compCls()` is `nullptr`.
57 * • `L.compName()` is `nullptr`.
58 * • `L.compClsName()` is `nullptr`.
63 explicit LogCfg(const bt_logging_level logLevel, bt_self_message_iterator& selfMsgIter) :
64 _mLogLevel {logLevel}, _mSelfMsgIter {&selfMsgIter},
65 _mSelfComp {bt_self_message_iterator_borrow_component(&selfMsgIter)},
66 _mCompCls {&this->_compClsFromSelfComp(*_mSelfComp)}
70 explicit LogCfg(const bt_logging_level logLevel, bt_self_component& selfComp) :
71 _mLogLevel {logLevel}, _mSelfComp {&selfComp}, _mCompCls {
72 &this->_compClsFromSelfComp(*_mSelfComp)}
76 explicit LogCfg(const bt_logging_level logLevel, bt_self_component_class& selfCompCls) :
77 _mLogLevel {logLevel}, _mSelfCompCls {&selfCompCls},
78 _mCompCls {bt_self_component_class_as_component_class(&selfCompCls)}
82 explicit LogCfg(const bt_logging_level logLevel, const char * const moduleName) :
83 _mLogLevel {logLevel}, _mModuleName {moduleName}
85 BT_ASSERT_DBG(_mModuleName);
88 LogCfg(const LogCfg&) = default;
89 LogCfg& operator=(const LogCfg&) = default;
91 bt_logging_level logLevel() const noexcept
96 bt_self_component *selfComp() const noexcept
101 const char *compName() const noexcept
103 BT_ASSERT_DBG(_mSelfComp);
104 return bt_component_get_name(bt_self_component_as_component(_mSelfComp));
107 bt_self_component_class *selfCompCls() const noexcept
109 return _mSelfCompCls;
112 const bt_component_class *compCls() const noexcept
117 const char *compClsName() const noexcept
119 BT_ASSERT_DBG(_mCompCls);
120 return bt_component_class_get_name(_mCompCls);
123 bt_self_message_iterator *selfMsgIter() const noexcept
125 return _mSelfMsgIter;
128 const char *moduleName() const noexcept
134 static const bt_component_class& _compClsFromSelfComp(bt_self_component& selfComp) noexcept
136 return *bt_component_borrow_class_const(bt_self_component_as_component(&selfComp));
139 bt_logging_level _mLogLevel;
140 bt_self_message_iterator *_mSelfMsgIter = nullptr;
141 bt_self_component *_mSelfComp = nullptr;
142 bt_self_component_class *_mSelfCompCls = nullptr;
143 const bt_component_class *_mCompCls = nullptr;
144 const char *_mModuleName = nullptr;
147 } /* namespace bt2_common */
149 #endif /* BABELTRACE_CPP_COMMON_LOG_CFG_HPP */