logger_impl.h 8.84 KB
 Stephen Morris committed Feb 09, 2011 1 ``````// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC") `````` Stephen Morris committed Feb 04, 2011 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ``````// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. #ifndef __LOGGER_IMPL_H #define __LOGGER_IMPL_H `````` Stephen Morris committed Feb 09, 2011 18 ``````#include `````` Stephen Morris committed Feb 04, 2011 19 20 ``````#include `````` Stephen Morris committed Feb 04, 2011 21 22 ``````#include #include `````` Stephen Morris committed Feb 04, 2011 23 24 ``````#include #include `````` Stephen Morris committed Feb 04, 2011 25 `````` `````` Stephen Morris committed Feb 04, 2011 26 ``````#include `````` Stephen Morris committed Feb 04, 2011 27 28 ``````#include #include `````` Stephen Morris committed Feb 04, 2011 29 ``````#include `````` Stephen Morris committed Feb 04, 2011 30 31 32 33 `````` namespace isc { namespace log { `````` Stephen Morris committed Feb 04, 2011 34 ``````/// \brief Console Logger Implementation `````` Stephen Morris committed Feb 04, 2011 35 36 37 38 ``````/// /// The logger uses a "pimpl" idiom for implementation, where the base logger /// class contains little more than a pointer to the implementation class, and /// all actions are carried out by the latter. This class is an implementation `````` Stephen Morris committed Feb 04, 2011 39 ``````/// class that just outputs to stdout. `````` Stephen Morris committed Feb 04, 2011 40 41 42 43 `````` class LoggerImpl { public: `````` Stephen Morris committed Feb 04, 2011 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 `````` /// \brief Information About Logger /// /// Holds a information about a logger, namely its severity and its debug /// level. This could be a std::pair, except that it gets confusing when /// accessing the LoggerInfoMap: that returns a pair, so we to reference /// elements we would use constructs like ((i->first).second); struct LoggerInfo { isc::log::Severity severity; int dbglevel; LoggerInfo(isc::log::Severity sev = isc::log::INFO, int dbg = MIN_DEBUG_LEVEL) : severity(sev), dbglevel(dbg) {} }; /// \brief Information About All Loggers /// /// Information about all loggers in the system - except the root logger - /// is held in a map, linking name of the logger (excluding the root /// name component) and its set severity and debug levels. The root /// logger information is held separately. typedef std::map LoggerInfoMap; `````` Stephen Morris committed Feb 04, 2011 69 70 `````` /// \brief Constructor /// `````` Stephen Morris committed Feb 04, 2011 71 `````` /// Creates a logger of the specific name. `````` Stephen Morris committed Feb 04, 2011 72 `````` /// `````` Stephen Morris committed Feb 04, 2011 73 `````` /// \param name Name of the logger. `````` Stephen Morris committed Feb 04, 2011 74 75 `````` /// /// \param exit_delete This argument is present to get round a bug in `````` Stephen Morris committed Feb 04, 2011 76 `````` /// the log4cxx implementation. It is unused here. `````` Stephen Morris committed Feb 09, 2011 77 `````` LoggerImpl(const std::string& name, bool); `````` Stephen Morris committed Feb 04, 2011 78 79 80 81 82 83 84 `````` /// \brief Destructor virtual ~LoggerImpl(); /// \brief Get the full name of the logger (including the root name) `````` Stephen Morris committed Feb 09, 2011 85 86 `````` virtual std::string getName() { return (name_); `````` Stephen Morris committed Feb 04, 2011 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 `````` } /// \brief Set Severity Level for Logger /// /// Sets the level at which this logger will log messages. If none is set, /// the level is inherited from the parent. /// /// \param severity Severity level to log /// \param dbglevel If the severity is DEBUG, this is the debug level. /// This can be in the range 1 to 100 and controls the verbosity. A value /// outside these limits is silently coerced to the nearest boundary. virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1); /// \brief Get Severity Level for Logger /// /// \return The current logging level of this logger. In most cases though, /// the effective logging level is what is required. `````` Stephen Morris committed Feb 04, 2011 106 `````` virtual isc::log::Severity getSeverity(); `````` Stephen Morris committed Feb 04, 2011 107 108 109 110 111 112 113 `````` /// \brief Get Effective Severity Level for Logger /// /// \return The effective severity level of the logger. This is the same /// as getSeverity() if the logger has a severity level set, but otherwise /// is the severity of the parent. `````` Stephen Morris committed Feb 04, 2011 114 `````` virtual isc::log::Severity getEffectiveSeverity(); `````` Stephen Morris committed Feb 04, 2011 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 `````` /// \brief Return DEBUG Level /// /// \return Current setting of debug level. This is returned regardless of /// whether the virtual int getDebugLevel(); /// \brief Returns if Debug Message Should Be Output /// /// \param dbglevel Level for which debugging is checked. Debugging is /// enabled only if the logger has DEBUG enabled and if the dbglevel /// checked is less than or equal to the debug level set for the logger. virtual bool `````` Stephen Morris committed Feb 04, 2011 130 `````` isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL); `````` Stephen Morris committed Feb 04, 2011 131 132 133 `````` /// \brief Is INFO Enabled? virtual bool isInfoEnabled() { `````` Stephen Morris committed Feb 04, 2011 134 `````` return (isEnabled(isc::log::INFO)); `````` Stephen Morris committed Feb 04, 2011 135 136 137 138 `````` } /// \brief Is WARNING Enabled? virtual bool isWarnEnabled() { `````` Stephen Morris committed Feb 04, 2011 139 `````` return (isEnabled(isc::log::WARN)); `````` Stephen Morris committed Feb 04, 2011 140 141 142 143 `````` } /// \brief Is ERROR Enabled? virtual bool isErrorEnabled() { `````` Stephen Morris committed Feb 04, 2011 144 `````` return (isEnabled(isc::log::ERROR)); `````` Stephen Morris committed Feb 04, 2011 145 146 147 148 `````` } /// \brief Is FATAL Enabled? virtual bool isFatalEnabled() { `````` Stephen Morris committed Feb 04, 2011 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 `````` return (isEnabled(isc::log::FATAL)); } /// \brief Common Severity check /// /// Implements the common severity check. As an optimisation, this checks /// to see if any logger-specific levels have been set (a quick check as it /// just involves seeing if the collection of logger information is empty). /// if not, it returns the information for the root level; if so, it has /// to take longer and look up the information in the map holding the /// logging details. virtual bool isEnabled(isc::log::Severity severity) { if (logger_info_.empty()) { return (root_logger_info_.severity <= severity); } else { return (getSeverity() <= severity); } `````` Stephen Morris committed Feb 04, 2011 168 169 170 `````` } `````` Stephen Morris committed Feb 04, 2011 171 172 173 174 175 `````` /// \brief Output General Message /// /// The message is formatted to include the date and time, the severity /// and the logger generating the message. /// `````` Stephen Morris committed Feb 08, 2011 176 177 `````` /// \param sev_text Severity level as a text string /// \param ident Message identification `````` Stephen Morris committed Feb 09, 2011 178 `````` /// \param ap Variable argument list holding message arguments `````` Stephen Morris committed Feb 08, 2011 179 `````` void output(const char* sev_text, const MessageID& ident, `````` Stephen Morris committed Feb 09, 2011 180 `````` va_list ap); `````` Stephen Morris committed Feb 04, 2011 181 `````` `````` Michal 'vorner' Vaner committed May 05, 2011 182 183 `````` void outputRaw(const char* sev_text, const std::string& message); std::string* lookupMessage(const MessageID& id); `````` Stephen Morris committed Feb 04, 2011 184 `````` `````` Stephen Morris committed Feb 04, 2011 185 186 187 188 `````` /// \brief Output Debug Message /// /// \param ident Message identification. /// \param text Text to log `````` Stephen Morris committed Feb 09, 2011 189 190 191 `````` /// \param ap Variable argument list holding message arguments void debug(const MessageID& ident, va_list ap) { output("DEBUG", ident, ap); `````` Stephen Morris committed Feb 08, 2011 192 `````` } `````` Stephen Morris committed Feb 04, 2011 193 194 195 196 197 198 `````` /// \brief Output Informational Message /// /// \param ident Message identification. /// \param text Text to log `````` Stephen Morris committed Feb 09, 2011 199 200 201 `````` /// \param ap Variable argument list holding message arguments void info(const MessageID& ident, va_list ap) { output("INFO ", ident, ap); `````` Stephen Morris committed Feb 08, 2011 202 `````` } `````` Stephen Morris committed Feb 04, 2011 203 204 205 206 207 `````` /// \brief Output Warning Message /// /// \param ident Message identification. /// \param text Text to log `````` Stephen Morris committed Feb 09, 2011 208 209 210 `````` /// \param ap Variable argument list holding message arguments void warn(const MessageID& ident, va_list ap) { output("WARN ", ident, ap); `````` Stephen Morris committed Feb 08, 2011 211 `````` } `````` Stephen Morris committed Feb 04, 2011 212 213 214 215 216 `````` /// \brief Output Error Message /// /// \param ident Message identification. /// \param text Text to log `````` Stephen Morris committed Feb 09, 2011 217 218 219 `````` /// \param ap Variable argument list holding message arguments void error(const MessageID& ident, va_list ap) { output("ERROR", ident, ap); `````` Stephen Morris committed Feb 08, 2011 220 `````` } `````` Stephen Morris committed Feb 04, 2011 221 222 223 224 225 `````` /// \brief Output Fatal Message /// /// \param ident Message identification. /// \param text Text to log `````` Stephen Morris committed Feb 09, 2011 226 227 228 `````` /// \param ap Variable argument list holding message arguments void fatal(const MessageID& ident, va_list ap) { output("FATAL", ident, ap); `````` Stephen Morris committed Feb 08, 2011 229 `````` } `````` Stephen Morris committed Feb 04, 2011 230 231 232 233 234 235 236 237 `````` /// \brief Equality /// /// Check if two instances of this logger refer to the same stream. /// (This method is principally for testing.) /// /// \return true if the logger objects are instances of the same logger. bool operator==(const LoggerImpl& other) { `````` Stephen Morris committed Feb 04, 2011 238 `````` return (name_ == other.name_); `````` Stephen Morris committed Feb 04, 2011 239 240 241 `````` } `````` Stephen Morris committed Feb 04, 2011 242 `````` /// \brief Reset Global Data `````` Stephen Morris committed Feb 04, 2011 243 `````` /// `````` Stephen Morris committed Feb 04, 2011 244 245 246 247 248 `````` /// Only used for testing, this clears all the logger information and /// resets it back to default values. static void reset() { root_logger_info_ = LoggerInfo(isc::log::INFO, MIN_DEBUG_LEVEL); logger_info_.clear(); `````` Stephen Morris committed Feb 04, 2011 249 250 251 252 `````` } private: `````` Stephen Morris committed Feb 09, 2011 253 254 `````` bool is_root_; ///< true if a root logger std::string name_; ///< Name of this logger `````` JINMEI Tatuya committed Feb 08, 2011 255 `````` `````` Stephen Morris committed Feb 04, 2011 256 257 258 259 260 261 262 `````` // Split the status of the root logger from this logger. If - is will // probably be the usual case - no per-logger setting is enabled, a // quick check of logger_info_.empty() will return true and we can quickly // return the root logger status without a length lookup in the map. static LoggerInfo root_logger_info_; ///< Status of root logger static LoggerInfoMap logger_info_; ///< Store of debug levels etc. `````` Stephen Morris committed Feb 04, 2011 263 264 265 266 267 268 269 ``````}; } // namespace log } // namespace isc #endif // __LOGGER_IMPL_H``````