summaryrefslogtreecommitdiff
path: root/src/main/include/log4cxx/level.h
blob: e675e45e9e4ba5a987568b167da503d46a4f2b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _LOG4CXX_LEVEL_H
#define _LOG4CXX_LEVEL_H


#include <log4cxx/logstring.h>
#include <limits.h>
#include <log4cxx/helpers/objectimpl.h>
#include <log4cxx/helpers/objectptr.h>


namespace log4cxx
{
    class Level;
    /** smart pointer to a Level instance */
    LOG4CXX_PTR_DEF(Level);

        /**
        Defines the minimum set of levels recognized by the system, that is
        <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>,
        <code>WARN</code>, <code>INFO</code>, <code>DEBUG</code> and
        <code>ALL</code>.
        <p>The <code>Level</code> class may be subclassed to define a larger
        level set.
        */
        class LOG4CXX_EXPORT Level : public helpers::ObjectImpl
        {
        public:
                class LOG4CXX_EXPORT LevelClass : public helpers::Class
                {
                public:
                        LevelClass() : helpers::Class() {}

                        virtual LogString getName() const {
                            return LOG4CXX_STR("Level");
                        }

                        virtual LevelPtr toLevel(const LogString& sArg) const
                        { return Level::toLevelLS(sArg); }

                        virtual LevelPtr toLevel(int val) const
                        { return Level::toLevel(val); }
                };

                DECLARE_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(Level, LevelClass)
                BEGIN_LOG4CXX_CAST_MAP()
                        LOG4CXX_CAST_ENTRY(Level)
                END_LOG4CXX_CAST_MAP()

                /**
                Instantiate a Level object.
                */
                Level(int level,
                       const LogString& name,
                       int syslogEquivalent);

                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns DEBUG.
                * @param sArg level name.
                */
                static LevelPtr toLevel(const std::string& sArg);
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns the value of
                <code>defaultLevel</code>.
                * @param sArg level name.
                * @param defaultLevel level to return if no match.
                * @return 
                */
                static LevelPtr toLevel(const std::string& sArg,
                        const LevelPtr& defaultLevel);
                /**
                 *  Get the name of the level in the current encoding.
                 *  @param name buffer to which name is appended.
                 */
                void toString(std::string& name) const;

#if LOG4CXX_WCHAR_T_API
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns DEBUG.
                * @param sArg level name.
                */
                static LevelPtr toLevel(const std::wstring& sArg);
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns the value of
                <code>defaultLevel</code>.
                * @param sArg level name.
                * @param defaultLevel level to return if no match.
                * @return 
                */
                static LevelPtr toLevel(const std::wstring& sArg,
                        const LevelPtr& defaultLevel);
                /**
                 *  Get the name of the level.
                 *  @param name buffer to which name is appended.
                 */
                void toString(std::wstring& name) const;
#endif
#if LOG4CXX_UNICHAR_API
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns DEBUG.
                * @param sArg level name.
                */
                static LevelPtr toLevel(const std::basic_string<UniChar>& sArg);
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns the value of
                <code>defaultLevel</code>.
                * @param sArg level name.
                * @param defaultLevel level to return if no match.
                * @return 
                */
                static LevelPtr toLevel(const std::basic_string<UniChar>& sArg,
                        const LevelPtr& defaultLevel);
                /**
                 *  Get the name of the level.
                 *  @param name buffer to which name is appended.
                 */
                void toString(std::basic_string<UniChar>& name) const;
#endif
#if LOG4CXX_CFSTRING_API
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns DEBUG.
                * @param sArg level name.
                */
                static LevelPtr toLevel(const CFStringRef& sArg);
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns the value of
                <code>defaultLevel</code>.
                * @param sArg level name.
                * @param defaultLevel level to return if no match.
                * @return 
                */
                static LevelPtr toLevel(const CFStringRef& sArg,
                        const LevelPtr& defaultLevel);
                /**
                 *  Get the name of the level.
                 *  @param name buffer to which name is appended.
                 */
                void toString(CFStringRef& name) const;
#endif
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns DEBUG.
                * @param sArg level name.
                */
                static LevelPtr toLevelLS(const LogString& sArg);
                /**
                Convert the string passed as argument to a level. If the
                conversion fails, then this method returns the value of
                <code>defaultLevel</code>.
                * @param sArg level name.
                * @param defaultLevel level to return if no match.
                * @return 
                */
                static LevelPtr toLevelLS(const LogString& sArg,
                        const LevelPtr& defaultLevel);
                /**
                Returns the string representation of this level.
                * @return level name.
                */
                LogString toString() const;

                /**
                Convert an integer passed as argument to a level. If the
                conversion fails, then this method returns DEBUG.
                */
                static LevelPtr toLevel(int val);

                /**
                Convert an integer passed as argument to a level. If the
                conversion fails, then this method returns the specified default.
                */
                static LevelPtr toLevel(int val, const LevelPtr& defaultLevel);

                enum {
                    OFF_INT = INT_MAX,
                    FATAL_INT = 50000,
                    ERROR_INT = 40000,
                    WARN_INT = 30000,
                    INFO_INT = 20000,
                    DEBUG_INT = 10000,
                    TRACE_INT = 5000,
                    ALL_INT = INT_MIN
                };


                static LevelPtr getAll();
                static LevelPtr getFatal();
                static LevelPtr getError();
                static LevelPtr getWarn();
                static LevelPtr getInfo();
                static LevelPtr getDebug();
                static LevelPtr getTrace();
                static LevelPtr getOff();


                /**
                Two levels are equal if their level fields are equal.
                */
                virtual bool equals(const LevelPtr& level) const;

                inline bool operator==(const Level& level1) const
                        { return (this->level == level1.level); }

                inline bool operator!=(const Level& level1) const
                        { return (this->level != level1.level); }

                /**
                Return the syslog equivalent of this level as an integer.
                */
                inline int getSyslogEquivalent() const {
                  return syslogEquivalent;
                }


                /**
                Returns <code>true</code> if this level has a higher or equal
                level than the level passed as argument, <code>false</code>
                otherwise.

                <p>You should think twice before overriding the default
                implementation of <code>isGreaterOrEqual</code> method.

                */
                virtual bool isGreaterOrEqual(const LevelPtr& level) const;


                /**
                Returns the integer representation of this level.
                */
                inline int toInt() const {
                  return level;
                }

        private:
                int level;
                LogString name;
                int syslogEquivalent;
                Level(const Level&);
                Level& operator=(const Level&);
        };
}

#define DECLARE_LOG4CXX_LEVEL(level)\
public:\
        class Class##level : public Level::LevelClass\
{\
public:\
        Class##level() : Level::LevelClass() {}\
        virtual LogString getName() const { return LOG4CXX_STR(#level); } \
        virtual LevelPtr toLevel(const LogString& sArg) const\
        { return level::toLevelLS(sArg); }\
        virtual LevelPtr toLevel(int val) const\
        { return level::toLevel(val); }\
};\
DECLARE_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(level, Class##level)

#define IMPLEMENT_LOG4CXX_LEVEL(level) \
IMPLEMENT_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(level, Class##level)


#endif //_LOG4CXX_LEVEL_H