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