/src/opendnp3/cpp/lib/include/opendnp3/logging/LogLevels.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2013-2022 Step Function I/O, LLC |
3 | | * |
4 | | * Licensed to Green Energy Corp (www.greenenergycorp.com) and Step Function I/O |
5 | | * LLC (https://stepfunc.io) under one or more contributor license agreements. |
6 | | * See the NOTICE file distributed with this work for additional information |
7 | | * regarding copyright ownership. Green Energy Corp and Step Function I/O LLC license |
8 | | * this file to you under the Apache License, Version 2.0 (the "License"); you |
9 | | * may not use this file except in compliance with the License. You may obtain |
10 | | * a copy of the License at: |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | #ifndef OPENDNP3_LOGLEVELS_H |
21 | | #define OPENDNP3_LOGLEVELS_H |
22 | | |
23 | | #include <cstdint> |
24 | | |
25 | | namespace opendnp3 |
26 | | { |
27 | | |
28 | | struct ModuleId |
29 | | { |
30 | | public: |
31 | 6.90k | ModuleId() = default; |
32 | | |
33 | 15.7k | explicit ModuleId(int32_t level) : value(level) {} |
34 | | |
35 | | int32_t value = 0; |
36 | | }; |
37 | | |
38 | | struct LogLevel |
39 | | { |
40 | | public: |
41 | | LogLevel() = default; |
42 | | |
43 | 2.10k | explicit LogLevel(int32_t level) : value(level) {} |
44 | | |
45 | | LogLevel next() const |
46 | 1.98k | { |
47 | 1.98k | return LogLevel(value << 1); |
48 | 1.98k | } |
49 | | |
50 | | bool operator==(const LogLevel& other) const |
51 | 0 | { |
52 | 0 | return this->value == other.value; |
53 | 0 | } |
54 | | |
55 | | int32_t value = 0; |
56 | | }; |
57 | | |
58 | | /** |
59 | | * Strongly typed wrapper for flags bitfield |
60 | | */ |
61 | | class LogLevels |
62 | | { |
63 | | public: |
64 | | LogLevels() = default; |
65 | | |
66 | 17.7k | explicit LogLevels(int32_t levels) : levels(levels) {} |
67 | | |
68 | 0 | LogLevels(LogLevel level) : levels(level.value) {} |
69 | | |
70 | | static LogLevels none() |
71 | 124 | { |
72 | 124 | return LogLevels(0); |
73 | 124 | } |
74 | | |
75 | | static LogLevels everything() |
76 | 124 | { |
77 | 124 | return LogLevels(~0); |
78 | 124 | } |
79 | | |
80 | | inline bool is_set(const LogLevel& level) const |
81 | 534k | { |
82 | 534k | return (level.value & levels) != 0; |
83 | 534k | } |
84 | | |
85 | | LogLevels operator~() const |
86 | 0 | { |
87 | 0 | return LogLevels(~this->levels); |
88 | 0 | } |
89 | | |
90 | | LogLevels& operator|=(const LogLevel& other) |
91 | 0 | { |
92 | 0 | this->levels |= other.value; |
93 | 0 | return *this; |
94 | 0 | } |
95 | | |
96 | | LogLevels operator|(const LogLevel& other) const |
97 | 1.73k | { |
98 | 1.73k | return LogLevels(this->levels | other.value); |
99 | 1.73k | } |
100 | | |
101 | | LogLevels& operator|=(const LogLevels& other) |
102 | 0 | { |
103 | 0 | this->levels |= other.levels; |
104 | 0 | return *this; |
105 | 0 | } |
106 | | |
107 | | LogLevels operator|(const LogLevels& other) const |
108 | 0 | { |
109 | 0 | return LogLevels(this->levels | other.levels); |
110 | 0 | } |
111 | | |
112 | | inline int32_t get_value() const |
113 | 0 | { |
114 | 0 | return levels; |
115 | 0 | } |
116 | | |
117 | | private: |
118 | | int32_t levels = 0; |
119 | | }; |
120 | | |
121 | | namespace flags |
122 | | { |
123 | | |
124 | | // base filters |
125 | | const LogLevel EVENT = LogLevel(1); |
126 | | const LogLevel ERR = EVENT.next(); |
127 | | const LogLevel WARN = ERR.next(); |
128 | | const LogLevel INFO = WARN.next(); |
129 | | const LogLevel DBG = INFO.next(); |
130 | | |
131 | | // up-shift the custom dnp3 filters |
132 | | |
133 | | const LogLevel LINK_RX = DBG.next(); |
134 | | const LogLevel LINK_RX_HEX = LINK_RX.next(); |
135 | | |
136 | | const LogLevel LINK_TX = LINK_RX_HEX.next(); |
137 | | const LogLevel LINK_TX_HEX = LINK_TX.next(); |
138 | | |
139 | | const LogLevel TRANSPORT_RX = LINK_TX_HEX.next(); |
140 | | const LogLevel TRANSPORT_TX = TRANSPORT_RX.next(); |
141 | | |
142 | | const LogLevel APP_HEADER_RX = TRANSPORT_TX.next(); |
143 | | const LogLevel APP_HEADER_TX = APP_HEADER_RX.next(); |
144 | | |
145 | | const LogLevel APP_OBJECT_RX = APP_HEADER_TX.next(); |
146 | | const LogLevel APP_OBJECT_TX = APP_OBJECT_RX.next(); |
147 | | |
148 | | const LogLevel APP_HEX_RX = APP_OBJECT_TX.next(); |
149 | | const LogLevel APP_HEX_TX = APP_HEX_RX.next(); |
150 | | |
151 | | } // namespace flags |
152 | | |
153 | | namespace levels |
154 | | { |
155 | | const LogLevels NOTHING = LogLevels::none(); |
156 | | const LogLevels ALL = LogLevels::everything(); |
157 | | const LogLevels NORMAL = NOTHING | flags::EVENT | flags::ERR | flags::WARN | flags::INFO; |
158 | | const LogLevels ALL_APP_COMMS = NOTHING | flags::APP_HEADER_RX | flags::APP_HEADER_TX | flags::APP_OBJECT_RX |
159 | | | flags::APP_OBJECT_TX | flags::APP_HEX_RX | flags::APP_HEX_TX; |
160 | | const LogLevels ALL_COMMS |
161 | | = ALL_APP_COMMS | flags::LINK_RX | flags::LINK_TX | flags::TRANSPORT_RX | flags::TRANSPORT_TX; |
162 | | } // namespace levels |
163 | | |
164 | | const char* LogFlagToString(const LogLevel& flag); |
165 | | |
166 | | } // namespace opendnp3 |
167 | | |
168 | | #endif // OPENDNP3_LOGLEVELS_H |