Coverage Report

Created: 2025-07-11 07:00

/src/logging-log4cxx/src/main/include/log4cxx/level.h
Line
Count
Source (jump to first uncovered line)
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
#include <mutex>
26
27
namespace LOG4CXX_NS
28
{
29
/**
30
 * LOG4CXX_PTR_DEF can't be used to get a smart pointer for Level because we need to override
31
 * the comparison operator and this doesn't work if the template has alread been initialized,
32
 * which is what the macro does on some platforms. The overriding takes place underneath the
33
 * definition of Level because we need one of it's methods.
34
 *
35
 * https://issues.apache.org/jira/browse/LOGCXX-394
36
 */
37
class Level;
38
typedef std::shared_ptr<Level> LevelPtr;
39
40
/**
41
Defines the minimum set of levels recognized by the system, that is
42
<code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>,
43
<code>WARN</code>, <code>INFO</code>, <code>DEBUG</code> and
44
<code>ALL</code>.
45
<p>The <code>Level</code> class may be subclassed to define a larger
46
level set.
47
*/
48
class LOG4CXX_EXPORT Level : public helpers::Object
49
{
50
  public:
51
    class LOG4CXX_EXPORT LevelClass : public helpers::Class
52
    {
53
      public:
54
2
        LevelClass() : helpers::Class() {}
55
56
        virtual LogString getName() const
57
2
        {
58
2
          return LOG4CXX_STR("Level");
59
2
        }
60
61
        virtual LevelPtr toLevel(const LogString& sArg) const
62
0
        {
63
0
          return Level::toLevelLS(sArg);
64
0
        }
65
66
        virtual LevelPtr toLevel(int val) const
67
0
        {
68
0
          return Level::toLevel(val);
69
0
        }
70
    };
71
72
    DECLARE_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(Level, LevelClass)
73
0
    BEGIN_LOG4CXX_CAST_MAP()
74
0
    LOG4CXX_CAST_ENTRY(Level)
75
0
    END_LOG4CXX_CAST_MAP()
76
77
    /**
78
    Instantiate a Level object.
79
    */
80
    Level(int level,
81
      const LogString& name,
82
      int syslogEquivalent);
83
84
    /**
85
    Convert the string passed as argument to a level. If the
86
    conversion fails, then this method returns DEBUG.
87
    * @param sArg level name.
88
    */
89
    static LevelPtr toLevel(const std::string& sArg);
90
    /**
91
    Convert the string passed as argument to a level. If the
92
    conversion fails, then this method returns the value of
93
    <code>defaultLevel</code>.
94
    * @param sArg level name.
95
    * @param defaultLevel level to return if no match.
96
    * @return
97
    */
98
    static LevelPtr toLevel(const std::string& sArg,
99
      const LevelPtr& defaultLevel);
100
    /**
101
     *  Get the name of the level in the current encoding.
102
     *  @param name buffer to which name is appended.
103
     */
104
    void toString(std::string& name) const;
105
106
#if LOG4CXX_WCHAR_T_API
107
    /**
108
    Convert the string passed as argument to a level. If the
109
    conversion fails, then this method returns DEBUG.
110
    * @param sArg level name.
111
    */
112
    static LevelPtr toLevel(const std::wstring& sArg);
113
    /**
114
    Convert the string passed as argument to a level. If the
115
    conversion fails, then this method returns the value of
116
    <code>defaultLevel</code>.
117
    * @param sArg level name.
118
    * @param defaultLevel level to return if no match.
119
    * @return
120
    */
121
    static LevelPtr toLevel(const std::wstring& sArg,
122
      const LevelPtr& defaultLevel);
123
    /**
124
     *  Get the name of the level.
125
     *  @param name buffer to which name is appended.
126
     */
127
    void toString(std::wstring& name) const;
128
#endif
129
#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
130
    /**
131
    Convert the string passed as argument to a level. If the
132
    conversion fails, then this method returns DEBUG.
133
    * @param sArg level name.
134
    */
135
    static LevelPtr toLevel(const std::basic_string<UniChar>& sArg);
136
    /**
137
    Convert the string passed as argument to a level. If the
138
    conversion fails, then this method returns the value of
139
    <code>defaultLevel</code>.
140
    * @param sArg level name.
141
    * @param defaultLevel level to return if no match.
142
    * @return
143
    */
144
    static LevelPtr toLevel(const std::basic_string<UniChar>& sArg,
145
      const LevelPtr& defaultLevel);
146
    /**
147
     *  Get the name of the level.
148
     *  @param name buffer to which name is appended.
149
     */
150
    void toString(std::basic_string<UniChar>& name) const;
151
#endif
152
#if LOG4CXX_CFSTRING_API
153
    /**
154
    Convert the string passed as argument to a level. If the
155
    conversion fails, then this method returns DEBUG.
156
    * @param sArg level name.
157
    */
158
    static LevelPtr toLevel(const CFStringRef& sArg);
159
    /**
160
    Convert the string passed as argument to a level. If the
161
    conversion fails, then this method returns the value of
162
    <code>defaultLevel</code>.
163
    * @param sArg level name.
164
    * @param defaultLevel level to return if no match.
165
    * @return
166
    */
167
    static LevelPtr toLevel(const CFStringRef& sArg,
168
      const LevelPtr& defaultLevel);
169
    /**
170
     *  Get the name of the level.
171
     *  @param name buffer to which name is appended.
172
     */
173
    void toString(CFStringRef& name) const;
174
#endif
175
    /**
176
    Convert the string passed as argument to a level. If the
177
    conversion fails, then this method returns DEBUG.
178
    * @param sArg level name.
179
    */
180
    static LevelPtr toLevelLS(const LogString& sArg);
181
    /**
182
    Convert the string passed as argument to a level. If the
183
    conversion fails, then this method returns the value of
184
    <code>defaultLevel</code>.
185
    * @param sArg level name.
186
    * @param defaultLevel level to return if no match.
187
    * @return
188
    */
189
    static LevelPtr toLevelLS(const LogString& sArg,
190
      const LevelPtr& defaultLevel);
191
    /**
192
    Returns the string representation of this level.
193
    * @return level name.
194
    */
195
    LogString toString() const;
196
197
    /**
198
    Convert an integer passed as argument to a level. If the
199
    conversion fails, then this method returns DEBUG.
200
    */
201
    static LevelPtr toLevel(int val);
202
203
    /**
204
    Convert an integer passed as argument to a level. If the
205
    conversion fails, then this method returns the specified default.
206
    */
207
    static LevelPtr toLevel(int val, const LevelPtr& defaultLevel);
208
209
    enum
210
    {
211
      OFF_INT = INT_MAX,
212
      FATAL_INT = 50000,
213
      ERROR_INT = 40000,
214
      WARN_INT = 30000,
215
      INFO_INT = 20000,
216
      DEBUG_INT = 10000,
217
      TRACE_INT = 5000,
218
      ALL_INT = INT_MIN
219
    };
220
221
    struct Data
222
    {
223
      LevelPtr Off;
224
      LevelPtr Fatal;
225
      LevelPtr Error;
226
      LevelPtr Warn;
227
      LevelPtr Info;
228
      LevelPtr Debug;
229
      LevelPtr Trace;
230
      LevelPtr All;
231
    };
232
    using DataPtr = std::shared_ptr<Data>;
233
234
    static const DataPtr& getData();
235
    static LevelPtr getAll();
236
    static LevelPtr getFatal();
237
    static LevelPtr getError();
238
    static LevelPtr getWarn();
239
    static LevelPtr getInfo();
240
    static LevelPtr getDebug();
241
    static LevelPtr getTrace();
242
    static LevelPtr getOff();
243
244
245
    /**
246
    Two levels are equal if their level fields are equal.
247
    */
248
    virtual bool equals(const LevelPtr& level) const;
249
250
    inline bool operator==(const Level& level1) const
251
0
    {
252
0
      return (this->level == level1.level);
253
0
    }
254
255
    inline bool operator!=(const Level& level1) const
256
0
    {
257
0
      return (this->level != level1.level);
258
0
    }
259
260
    /**
261
    Return the syslog equivalent of this level as an integer.
262
    */
263
    inline int getSyslogEquivalent() const
264
0
    {
265
0
      return syslogEquivalent;
266
0
    }
267
268
269
    /**
270
    Returns <code>true</code> if this level has a higher or equal
271
    level than the level passed as argument, <code>false</code>
272
    otherwise.
273
274
    <p>You should think twice before overriding the default
275
    implementation of <code>isGreaterOrEqual</code> method.
276
277
    */
278
    virtual bool isGreaterOrEqual(const LevelPtr& level) const;
279
280
281
    /**
282
    Returns the integer representation of this level.
283
    */
284
    inline int toInt() const
285
0
    {
286
0
      return level;
287
0
    }
288
289
  private:
290
    LOG4CXX_DECLARE_PRIVATE_MEMBER(LogString, name)
291
    int level;
292
    int syslogEquivalent;
293
    Level(const Level&);
294
    Level& operator=(const Level&);
295
};
296
297
}
298
299
#define DECLARE_LOG4CXX_LEVEL(level)\
300
  public:\
301
  class Class##level : public Level::LevelClass\
302
  {\
303
    public:\
304
      Class##level() : Level::LevelClass() {}\
305
      virtual LogString getName() const { return LOG4CXX_STR(#level); } \
306
      virtual LevelPtr toLevel(const LogString& sArg) const\
307
      { return level::toLevelLS(sArg); }\
308
      virtual LevelPtr toLevel(int val) const\
309
      { return level::toLevel(val); }\
310
  };\
311
  DECLARE_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(level, Class##level)
312
313
#define IMPLEMENT_LOG4CXX_LEVEL(level) \
314
  IMPLEMENT_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(level, Class##level)
315
316
#endif //_LOG4CXX_LEVEL_H