Branch data Line data Source code
1 : : // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #ifndef __LOGGER_IMPL_H
16 : : #define __LOGGER_IMPL_H
17 : :
18 : : #include <stdarg.h>
19 : : #include <time.h>
20 : :
21 : : #include <iostream>
22 : : #include <cstdlib>
23 : : #include <string>
24 : : #include <map>
25 : : #include <utility>
26 : :
27 : :
28 : : // log4cplus logger header file
29 : : #include <log4cplus/logger.h>
30 : :
31 : : // BIND-10 logger files
32 : : #include <log/logger_level_impl.h>
33 : : #include <log/message_types.h>
34 : :
35 : : namespace isc {
36 : : namespace log {
37 : :
38 : : /// \brief Console Logger Implementation
39 : : ///
40 : : /// The logger uses a "pimpl" idiom for implementation, where the base logger
41 : : /// class contains little more than a pointer to the implementation class, and
42 : : /// all actions are carried out by the latter.
43 : : ///
44 : : /// This particular implementation is based on log4cplus (from sourceforge:
45 : : /// http://log4cplus.sourceforge.net). Particular items of note:
46 : : ///
47 : : /// a) BIND 10 loggers have names of the form "program.sublogger". In other
48 : : /// words, each of the loggers is a sub-logger of the main program logger.
49 : : /// In log4cplus, there is a root logger (called "root" according to the
50 : : /// documentation, but actually unnamed) and all loggers created are subloggers
51 : : /// if it.
52 : : ///
53 : : /// In this implementation, the log4cplus root logger is unused. Instead, the
54 : : /// BIND 10 root logger is created as a child of the log4cplus root logger,
55 : : /// and all other loggers used in the program are created as sub-loggers of
56 : : /// that. In this way, the logging system can just include the name of the
57 : : /// logger in each message without the need to specially consider if the
58 : : /// message is the root logger or not.
59 : : ///
60 : : /// b) The idea of debug levels is implemented. See logger_level.h and
61 : : /// logger_level_impl.h for more details on this.
62 : :
63 : : class LoggerImpl {
64 : : public:
65 : :
66 : : /// \brief Constructor
67 : : ///
68 : : /// Creates a logger of the specific name.
69 : : ///
70 : : /// \param name Name of the logger.
71 : : LoggerImpl(const std::string& name);
72 : :
73 : :
74 : : /// \brief Destructor
75 : : virtual ~LoggerImpl();
76 : :
77 : :
78 : : /// \brief Get the full name of the logger (including the root name)
79 : 4 : virtual std::string getName() {
80 : 4 : return (name_);
81 : : }
82 : :
83 : :
84 : : /// \brief Set Severity Level for Logger
85 : : ///
86 : : /// Sets the level at which this logger will log messages. If none is set,
87 : : /// the level is inherited from the parent.
88 : : ///
89 : : /// \param severity Severity level to log
90 : : /// \param dbglevel If the severity is DEBUG, this is the debug level.
91 : : /// This can be in the range 0 to 99 and controls the verbosity. A value
92 : : /// outside these limits is silently coerced to the nearest boundary.
93 : : virtual void setSeverity(Severity severity, int dbglevel = 1);
94 : :
95 : :
96 : : /// \brief Get Severity Level for Logger
97 : : ///
98 : : /// \return The current logging level of this logger. In most cases though,
99 : : /// the effective logging level is what is required.
100 : : virtual Severity getSeverity();
101 : :
102 : :
103 : : /// \brief Get Effective Severity Level for Logger
104 : : ///
105 : : /// \return The effective severity level of the logger. This is the same
106 : : /// as getSeverity() if the logger has a severity level set, but otherwise
107 : : /// is the severity of the parent.
108 : : virtual Severity getEffectiveSeverity();
109 : :
110 : :
111 : : /// \brief Return debug level
112 : : ///
113 : : /// \return Current setting of debug level. This will be zero if the
114 : : /// the current severity level is not DEBUG.
115 : : virtual int getDebugLevel();
116 : :
117 : :
118 : : /// \brief Return effective debug level
119 : : ///
120 : : /// \return Current setting of effective debug level. This will be zero if
121 : : /// the current effective severity level is not DEBUG.
122 : : virtual int getEffectiveDebugLevel();
123 : :
124 : :
125 : : /// \brief Returns if Debug Message Should Be Output
126 : : ///
127 : : /// \param dbglevel Level for which debugging is checked. Debugging is
128 : : /// enabled only if the logger has DEBUG enabled and if the dbglevel
129 : : /// checked is less than or equal to the debug level set for the logger.
130 : 78679 : virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL) {
131 : : Level level(DEBUG, dbglevel);
132 : 78679 : return logger_.isEnabledFor(LoggerLevelImpl::convertFromBindLevel(level));
133 : : }
134 : :
135 : : /// \brief Is INFO Enabled?
136 : 920 : virtual bool isInfoEnabled() {
137 : 920 : return (logger_.isEnabledFor(log4cplus::INFO_LOG_LEVEL));
138 : : }
139 : :
140 : : /// \brief Is WARNING Enabled?
141 : 214 : virtual bool isWarnEnabled() {
142 : 214 : return (logger_.isEnabledFor(log4cplus::WARN_LOG_LEVEL));
143 : : }
144 : :
145 : : /// \brief Is ERROR Enabled?
146 : 534 : virtual bool isErrorEnabled() {
147 : 534 : return (logger_.isEnabledFor(log4cplus::ERROR_LOG_LEVEL));
148 : : }
149 : :
150 : : /// \brief Is FATAL Enabled?
151 : 105 : virtual bool isFatalEnabled() {
152 : 105 : return (logger_.isEnabledFor(log4cplus::FATAL_LOG_LEVEL));
153 : : }
154 : :
155 : : /// \brief Raw output
156 : : ///
157 : : /// Writes the message with time into the log. Used by the Formatter
158 : : /// to produce output.
159 : : ///
160 : : /// \param severity Severity of the message. (This controls the prefix
161 : : /// label output with the message text.)
162 : : /// \param message Text of the message.
163 : : void outputRaw(const Severity& severity, const std::string& message);
164 : :
165 : : /// \brief Look up message text in dictionary
166 : : ///
167 : : /// This gets you the unformatted text of message for given ID.
168 : : std::string* lookupMessage(const MessageID& id);
169 : :
170 : : /// \brief Equality
171 : : ///
172 : : /// Check if two instances of this logger refer to the same stream.
173 : : /// (This method is principally for testing.)
174 : : ///
175 : : /// \return true if the logger objects are instances of the same logger.
176 : : bool operator==(const LoggerImpl& other) {
177 : 2 : return (name_ == other.name_);
178 : : }
179 : :
180 : : private:
181 : : std::string name_; ///< Full name of this logger
182 : : log4cplus::Logger logger_; ///< Underlying log4cplus logger
183 : : };
184 : :
185 : : } // namespace log
186 : : } // namespace isc
187 : :
188 : :
189 : : #endif // __LOGGER_IMPL_H
|