/src/logging-log4cxx/src/main/include/log4cxx/level.h
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #ifndef _LOG4CXX_LEVEL_H |
19 | | #define _LOG4CXX_LEVEL_H |
20 | | |
21 | | |
22 | | #include <log4cxx/logstring.h> |
23 | | #include <limits.h> |
24 | | #include <log4cxx/helpers/object.h> |
25 | | |
26 | | namespace LOG4CXX_NS |
27 | | { |
28 | | /** |
29 | | * LOG4CXX_PTR_DEF can't be used to get a smart pointer for Level because we need to override |
30 | | * the comparison operator and this doesn't work if the template has alread been initialized, |
31 | | * which is what the macro does on some platforms. The overriding takes place underneath the |
32 | | * definition of Level because we need one of it's methods. |
33 | | * |
34 | | * https://issues.apache.org/jira/browse/LOGCXX-394 |
35 | | */ |
36 | | class Level; |
37 | | typedef std::shared_ptr<Level> LevelPtr; |
38 | | |
39 | | /** |
40 | | Defines the minimum set of levels recognized by the system, that is |
41 | | <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>, |
42 | | <code>WARN</code>, <code>INFO</code>, <code>DEBUG</code> and |
43 | | <code>ALL</code>. |
44 | | <p>The <code>Level</code> class may be subclassed to define a larger |
45 | | level set. |
46 | | */ |
47 | | class LOG4CXX_EXPORT Level : public helpers::Object |
48 | | { |
49 | | public: |
50 | | class LOG4CXX_EXPORT LevelClass : public helpers::Class |
51 | | { |
52 | | public: |
53 | 46 | LevelClass() : helpers::Class() {} |
54 | | |
55 | | LogString getName() const override |
56 | 8.68k | { |
57 | 8.68k | return LOG4CXX_STR("Level"); |
58 | 8.68k | } |
59 | | |
60 | | virtual LevelPtr toLevel(const LogString& sArg) const |
61 | 425 | { |
62 | 425 | return Level::toLevelLS(sArg); |
63 | 425 | } log4cxx::Level::LevelClass::toLevel(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) const Line | Count | Source | 61 | 10 | { | 62 | 10 | return Level::toLevelLS(sArg); | 63 | 10 | } |
log4cxx::Level::LevelClass::toLevel(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const Line | Count | Source | 61 | 415 | { | 62 | 415 | return Level::toLevelLS(sArg); | 63 | 415 | } |
|
64 | | |
65 | | virtual LevelPtr toLevel(int val) const |
66 | 0 | { |
67 | 0 | return Level::toLevel(val); |
68 | 0 | } |
69 | | }; |
70 | | |
71 | | DECLARE_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(Level, LevelClass) |
72 | 0 | BEGIN_LOG4CXX_CAST_MAP() |
73 | 0 | LOG4CXX_CAST_ENTRY(Level) |
74 | 0 | END_LOG4CXX_CAST_MAP() |
75 | | |
76 | | /** |
77 | | Instantiate a Level object. |
78 | | */ |
79 | | Level(int level, |
80 | | const LogString& name, |
81 | | int syslogEquivalent); |
82 | | |
83 | | /** |
84 | | Convert the string passed as argument to a level. If the |
85 | | conversion fails, then this method returns DEBUG. |
86 | | * @param sArg level name. |
87 | | */ |
88 | | static LevelPtr toLevel(const std::string& sArg); |
89 | | /** |
90 | | Convert the string passed as argument to a level. If the |
91 | | conversion fails, then this method returns the value of |
92 | | <code>defaultLevel</code>. |
93 | | * @param sArg level name. |
94 | | * @param defaultLevel level to return if no match. |
95 | | * @return |
96 | | */ |
97 | | static LevelPtr toLevel(const std::string& sArg, |
98 | | const LevelPtr& defaultLevel); |
99 | | /** |
100 | | * Get the name of the level in the current encoding. |
101 | | * @param name buffer to which name is appended. |
102 | | */ |
103 | | void toString(std::string& name) const; |
104 | | |
105 | | #if LOG4CXX_WCHAR_T_API |
106 | | /** |
107 | | Convert the string passed as argument to a level. If the |
108 | | conversion fails, then this method returns DEBUG. |
109 | | * @param sArg level name. |
110 | | */ |
111 | | static LevelPtr toLevel(const std::wstring& sArg); |
112 | | /** |
113 | | Convert the string passed as argument to a level. If the |
114 | | conversion fails, then this method returns the value of |
115 | | <code>defaultLevel</code>. |
116 | | * @param sArg level name. |
117 | | * @param defaultLevel level to return if no match. |
118 | | * @return |
119 | | */ |
120 | | static LevelPtr toLevel(const std::wstring& sArg, |
121 | | const LevelPtr& defaultLevel); |
122 | | /** |
123 | | * Get the name of the level. |
124 | | * @param name buffer to which name is appended. |
125 | | */ |
126 | | void toString(std::wstring& name) const; |
127 | | #endif |
128 | | #if LOG4CXX_CFSTRING_API |
129 | | /** |
130 | | Convert the string passed as argument to a level. If the |
131 | | conversion fails, then this method returns DEBUG. |
132 | | * @param sArg level name. |
133 | | */ |
134 | | static LevelPtr toLevel(const CFStringRef& sArg); |
135 | | /** |
136 | | Convert the string passed as argument to a level. If the |
137 | | conversion fails, then this method returns the value of |
138 | | <code>defaultLevel</code>. |
139 | | * @param sArg level name. |
140 | | * @param defaultLevel level to return if no match. |
141 | | * @return |
142 | | */ |
143 | | static LevelPtr toLevel(const CFStringRef& sArg, |
144 | | const LevelPtr& defaultLevel); |
145 | | /** |
146 | | * Get the name of the level. |
147 | | * @param name buffer to which name is appended. |
148 | | */ |
149 | | void toString(CFStringRef& name) const; |
150 | | #endif |
151 | | /** |
152 | | Convert the string passed as argument to a level. If the |
153 | | conversion fails, then this method returns DEBUG. |
154 | | * @param sArg level name. |
155 | | */ |
156 | | static LevelPtr toLevelLS(const LogString& sArg); |
157 | | /** |
158 | | Convert the string passed as argument to a level. If the |
159 | | conversion fails, then this method returns the value of |
160 | | <code>defaultLevel</code>. |
161 | | * @param sArg level name. |
162 | | * @param defaultLevel level to return if no match. |
163 | | * @return |
164 | | */ |
165 | | static LevelPtr toLevelLS(const LogString& sArg, |
166 | | const LevelPtr& defaultLevel); |
167 | | /** |
168 | | Returns the string representation of this level. |
169 | | * @return level name. |
170 | | */ |
171 | | LogString toString() const; |
172 | | |
173 | | /** |
174 | | Convert an integer passed as argument to a level. If the |
175 | | conversion fails, then this method returns DEBUG. |
176 | | */ |
177 | | static LevelPtr toLevel(int val); |
178 | | |
179 | | /** |
180 | | Convert an integer passed as argument to a level. If the |
181 | | conversion fails, then this method returns the specified default. |
182 | | */ |
183 | | static LevelPtr toLevel(int val, const LevelPtr& defaultLevel); |
184 | | |
185 | | enum |
186 | | { |
187 | | OFF_INT = INT_MAX, |
188 | | FATAL_INT = 50000, |
189 | | ERROR_INT = 40000, |
190 | | WARN_INT = 30000, |
191 | | INFO_INT = 20000, |
192 | | DEBUG_INT = 10000, |
193 | | TRACE_INT = 5000, |
194 | | ALL_INT = INT_MIN |
195 | | }; |
196 | | |
197 | | struct Data |
198 | | { |
199 | | LevelPtr Off; |
200 | | LevelPtr Fatal; |
201 | | LevelPtr Error; |
202 | | LevelPtr Warn; |
203 | | LevelPtr Info; |
204 | | LevelPtr Debug; |
205 | | LevelPtr Trace; |
206 | | LevelPtr All; |
207 | | }; |
208 | | using DataPtr = std::shared_ptr<Data>; |
209 | | |
210 | | static const DataPtr& getData(); |
211 | | static LevelPtr getAll(); |
212 | | static LevelPtr getFatal(); |
213 | | static LevelPtr getError(); |
214 | | static LevelPtr getWarn(); |
215 | | static LevelPtr getInfo(); |
216 | | static LevelPtr getDebug(); |
217 | | static LevelPtr getTrace(); |
218 | | static LevelPtr getOff(); |
219 | | |
220 | | |
221 | | /** |
222 | | Two levels are equal if their level fields are equal. |
223 | | */ |
224 | | virtual bool equals(const LevelPtr& level) const; |
225 | | |
226 | | inline bool operator==(const Level& level1) const |
227 | 0 | { |
228 | 0 | return (this->level == level1.level); |
229 | 0 | } |
230 | | |
231 | | inline bool operator!=(const Level& level1) const |
232 | 0 | { |
233 | 0 | return (this->level != level1.level); |
234 | 0 | } |
235 | | |
236 | | /** |
237 | | Return the syslog equivalent of this level as an integer. |
238 | | */ |
239 | | inline int getSyslogEquivalent() const |
240 | 108k | { |
241 | 108k | return syslogEquivalent; |
242 | 108k | } |
243 | | |
244 | | |
245 | | /** |
246 | | Returns <code>true</code> if this level has a higher or equal |
247 | | level than the level passed as argument, <code>false</code> |
248 | | otherwise. |
249 | | |
250 | | <p>You should think twice before overriding the default |
251 | | implementation of <code>isGreaterOrEqual</code> method. |
252 | | |
253 | | */ |
254 | | virtual bool isGreaterOrEqual(const LevelPtr& level) const; |
255 | | |
256 | | |
257 | | /** |
258 | | Returns the integer representation of this level. |
259 | | */ |
260 | | inline int toInt() const |
261 | 23.9M | { |
262 | 23.9M | return level; |
263 | 23.9M | } |
264 | | |
265 | | private: |
266 | | LOG4CXX_DECLARE_PRIVATE_MEMBER(LogString, name) |
267 | | int level; |
268 | | int syslogEquivalent; |
269 | | Level(const Level&); |
270 | | Level& operator=(const Level&); |
271 | | }; |
272 | | |
273 | | } |
274 | | |
275 | | #define DECLARE_LOG4CXX_LEVEL(level)\ |
276 | | public:\ |
277 | | class Class##level : public Level::LevelClass\ |
278 | | {\ |
279 | | public:\ |
280 | | LogString getName() const override { return LOG4CXX_STR(#level); } \ |
281 | | LevelPtr toLevel(const LogString& sArg) const override\ |
282 | | { return level::toLevelLS(sArg); }\ |
283 | | LevelPtr toLevel(int val) const override\ |
284 | | { return level::toLevel(val); }\ |
285 | | };\ |
286 | | DECLARE_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(level, Class##level) |
287 | | |
288 | | #define IMPLEMENT_LOG4CXX_LEVEL(level) \ |
289 | | IMPLEMENT_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(level, Class##level) |
290 | | |
291 | | #endif //_LOG4CXX_LEVEL_H |