Commit | Line | Data |
---|---|---|
f6368b95 FD |
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> | |
c802cacb | 12 | |
f6368b95 FD |
13 | #include "common/assert.h" |
14 | ||
15 | namespace bt2_common { | |
16 | ||
17 | /* | |
18 | * Logging configuration. | |
19 | * | |
20 | * A logging configuration object contains all the execution context | |
21 | * needed to: | |
22 | * | |
23 | * • Log, providing the name of some current component or component | |
24 | * class. | |
25 | * | |
26 | * • Append a cause to the error of the current thread from the right | |
27 | * actor. | |
28 | * | |
29 | * For a given logging configuration `L`, the guarantees are as such: | |
30 | * | |
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`. | |
37 | * | |
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`. | |
43 | * | |
44 | * If `L.selfCompCls()` isn't `nullptr`: | |
45 | * • `L.compCls()` isn't `nullptr`. | |
46 | * • `L.compClsName()` isn't `nullptr`. | |
47 | * • `L.moduleName()` is `nullptr`. | |
48 | * | |
49 | * If `L.compCls()` isn't `nullptr`: | |
50 | * • `L.compClsName()` isn't `nullptr`. | |
51 | * • `L.moduleName()` is `nullptr`. | |
52 | * | |
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`. | |
59 | */ | |
60 | class LogCfg final | |
61 | { | |
62 | public: | |
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)} | |
67 | { | |
68 | } | |
69 | ||
70 | explicit LogCfg(const bt_logging_level logLevel, bt_self_component& selfComp) : | |
71 | _mLogLevel {logLevel}, _mSelfComp {&selfComp}, _mCompCls { | |
72 | &this->_compClsFromSelfComp(*_mSelfComp)} | |
73 | { | |
74 | } | |
75 | ||
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)} | |
79 | { | |
80 | } | |
81 | ||
82 | explicit LogCfg(const bt_logging_level logLevel, const char * const moduleName) : | |
83 | _mLogLevel {logLevel}, _mModuleName {moduleName} | |
84 | { | |
85 | BT_ASSERT_DBG(_mModuleName); | |
86 | } | |
87 | ||
88 | LogCfg(const LogCfg&) = default; | |
89 | LogCfg& operator=(const LogCfg&) = default; | |
90 | ||
91 | bt_logging_level logLevel() const noexcept | |
92 | { | |
93 | return _mLogLevel; | |
94 | } | |
95 | ||
96 | bt_self_component *selfComp() const noexcept | |
97 | { | |
98 | return _mSelfComp; | |
99 | } | |
100 | ||
101 | const char *compName() const noexcept | |
102 | { | |
103 | BT_ASSERT_DBG(_mSelfComp); | |
104 | return bt_component_get_name(bt_self_component_as_component(_mSelfComp)); | |
105 | } | |
106 | ||
107 | bt_self_component_class *selfCompCls() const noexcept | |
108 | { | |
109 | return _mSelfCompCls; | |
110 | } | |
111 | ||
112 | const bt_component_class *compCls() const noexcept | |
113 | { | |
114 | return _mCompCls; | |
115 | } | |
116 | ||
117 | const char *compClsName() const noexcept | |
118 | { | |
119 | BT_ASSERT_DBG(_mCompCls); | |
120 | return bt_component_class_get_name(_mCompCls); | |
121 | } | |
122 | ||
123 | bt_self_message_iterator *selfMsgIter() const noexcept | |
124 | { | |
125 | return _mSelfMsgIter; | |
126 | } | |
127 | ||
128 | const char *moduleName() const noexcept | |
129 | { | |
130 | return _mModuleName; | |
131 | } | |
132 | ||
133 | private: | |
134 | static const bt_component_class& _compClsFromSelfComp(bt_self_component& selfComp) noexcept | |
135 | { | |
136 | return *bt_component_borrow_class_const(bt_self_component_as_component(&selfComp)); | |
137 | } | |
138 | ||
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; | |
145 | }; | |
146 | ||
147 | } /* namespace bt2_common */ | |
148 | ||
149 | #endif /* BABELTRACE_CPP_COMMON_LOG_CFG_HPP */ |