2ddfab07cc8952bc744683132600b8638ec9783b
[babeltrace.git] / src / cpp-common / log-cfg.hpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #ifndef BABELTRACE_CPP_COMMON_LOG_CFG_HPP
9 #define BABELTRACE_CPP_COMMON_LOG_CFG_HPP
10
11 #include <babeltrace2/babeltrace.h>
12 #include "common/assert.h"
13
14 namespace bt2_common {
15
16 /*
17 * Logging configuration.
18 *
19 * A logging configuration object contains all the execution context
20 * needed to:
21 *
22 * • Log, providing the name of some current component or component
23 * class.
24 *
25 * • Append a cause to the error of the current thread from the right
26 * actor.
27 *
28 * For a given logging configuration `L`, the guarantees are as such:
29 *
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`.
36 *
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`.
42 *
43 * If `L.selfCompCls()` isn't `nullptr`:
44 * • `L.compCls()` isn't `nullptr`.
45 * • `L.compClsName()` isn't `nullptr`.
46 * • `L.moduleName()` is `nullptr`.
47 *
48 * If `L.compCls()` isn't `nullptr`:
49 * • `L.compClsName()` isn't `nullptr`.
50 * • `L.moduleName()` is `nullptr`.
51 *
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`.
58 */
59 class LogCfg final
60 {
61 public:
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)}
66 {
67 }
68
69 explicit LogCfg(const bt_logging_level logLevel, bt_self_component& selfComp) :
70 _mLogLevel {logLevel}, _mSelfComp {&selfComp}, _mCompCls {
71 &this->_compClsFromSelfComp(*_mSelfComp)}
72 {
73 }
74
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)}
78 {
79 }
80
81 explicit LogCfg(const bt_logging_level logLevel, const char * const moduleName) :
82 _mLogLevel {logLevel}, _mModuleName {moduleName}
83 {
84 BT_ASSERT_DBG(_mModuleName);
85 }
86
87 LogCfg(const LogCfg&) = default;
88 LogCfg& operator=(const LogCfg&) = default;
89
90 bt_logging_level logLevel() const noexcept
91 {
92 return _mLogLevel;
93 }
94
95 bt_self_component *selfComp() const noexcept
96 {
97 return _mSelfComp;
98 }
99
100 const char *compName() const noexcept
101 {
102 BT_ASSERT_DBG(_mSelfComp);
103 return bt_component_get_name(bt_self_component_as_component(_mSelfComp));
104 }
105
106 bt_self_component_class *selfCompCls() const noexcept
107 {
108 return _mSelfCompCls;
109 }
110
111 const bt_component_class *compCls() const noexcept
112 {
113 return _mCompCls;
114 }
115
116 const char *compClsName() const noexcept
117 {
118 BT_ASSERT_DBG(_mCompCls);
119 return bt_component_class_get_name(_mCompCls);
120 }
121
122 bt_self_message_iterator *selfMsgIter() const noexcept
123 {
124 return _mSelfMsgIter;
125 }
126
127 const char *moduleName() const noexcept
128 {
129 return _mModuleName;
130 }
131
132 private:
133 static const bt_component_class& _compClsFromSelfComp(bt_self_component& selfComp) noexcept
134 {
135 return *bt_component_borrow_class_const(bt_self_component_as_component(&selfComp));
136 }
137
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;
144 };
145
146 } /* namespace bt2_common */
147
148 #endif /* BABELTRACE_CPP_COMMON_LOG_CFG_HPP */
This page took 0.0323 seconds and 3 git commands to generate.