logger_impl.h 8.84 KB
Newer Older
1
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
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

18
#include <stdarg.h>
19
20
#include <time.h>

21
22
#include <cstdlib>
#include <string>
23
24
#include <map>
#include <utility>
25

26
#include <log/debug_levels.h>
27
28
#include <log/logger.h>
#include <log/message_types.h>
29
#include <log/root_logger_name.h>
30
31
32
33

namespace isc {
namespace log {

34
/// \brief Console Logger Implementation
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
39
/// class that just outputs to stdout.
40
41
42
43

class LoggerImpl {
public:

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<std::string, LoggerInfo>   LoggerInfoMap;


69
70
    /// \brief Constructor
    ///
71
    /// Creates a logger of the specific name.
72
    ///
73
    /// \param name Name of the logger.
74
75
    ///
    /// \param exit_delete This argument is present to get round a bug in
76
    /// the log4cxx implementation.  It is unused here.
77
    LoggerImpl(const std::string& name, bool);
78
79
80
81
82
83
84


    /// \brief Destructor
    virtual ~LoggerImpl();


    /// \brief Get the full name of the logger (including the root name)
85
86
    virtual std::string getName() {
        return (name_);
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.
106
    virtual isc::log::Severity getSeverity();
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.
114
    virtual isc::log::Severity getEffectiveSeverity();
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
130
    isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
131
132
133

    /// \brief Is INFO Enabled?
    virtual bool isInfoEnabled() {
134
        return (isEnabled(isc::log::INFO));
135
136
137
138
    }

    /// \brief Is WARNING Enabled?
    virtual bool isWarnEnabled() {
139
        return (isEnabled(isc::log::WARN));
140
141
142
143
    }

    /// \brief Is ERROR Enabled?
    virtual bool isErrorEnabled() {
144
        return (isEnabled(isc::log::ERROR));
145
146
147
148
    }

    /// \brief Is FATAL Enabled?
    virtual bool isFatalEnabled() {
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);
        }
168
169
170
    }


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.
    ///
176
177
    /// \param sev_text Severity level as a text string
    /// \param ident Message identification
178
    /// \param ap Variable argument list holding message arguments
179
    void output(const char* sev_text, const MessageID& ident,
180
        va_list ap);
181

182
183
    void outputRaw(const char* sev_text, const std::string& message);
    std::string* lookupMessage(const MessageID& id);
184

185
186
187
188
    /// \brief Output Debug Message
    ///
    /// \param ident Message identification.
    /// \param text Text to log
189
190
191
    /// \param ap Variable argument list holding message arguments
    void debug(const MessageID& ident, va_list ap) {
        output("DEBUG", ident, ap);
192
    }
193
194
195
196
197
198


    /// \brief Output Informational Message
    ///
    /// \param ident Message identification.
    /// \param text Text to log
199
200
201
    /// \param ap Variable argument list holding message arguments
    void info(const MessageID& ident, va_list ap) {
        output("INFO ", ident, ap);
202
    }
203
204
205
206
207

    /// \brief Output Warning Message
    ///
    /// \param ident Message identification.
    /// \param text Text to log
208
209
210
    /// \param ap Variable argument list holding message arguments
    void warn(const MessageID& ident, va_list ap) {
        output("WARN ", ident, ap);
211
    }
212
213
214
215
216

    /// \brief Output Error Message
    ///
    /// \param ident Message identification.
    /// \param text Text to log
217
218
219
    /// \param ap Variable argument list holding message arguments
    void error(const MessageID& ident, va_list ap) {
        output("ERROR", ident, ap);
220
    }
221
222
223
224
225

    /// \brief Output Fatal Message
    ///
    /// \param ident Message identification.
    /// \param text Text to log
226
227
228
    /// \param ap Variable argument list holding message arguments
    void fatal(const MessageID& ident, va_list ap) {
        output("FATAL", ident, ap);
229
    }
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) {
238
        return (name_ == other.name_);
239
240
241
    }


242
    /// \brief Reset Global Data
243
    ///
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();
249
250
251
252
    }


private:
253
254
    bool                is_root_;           ///< true if a root logger
    std::string         name_;              ///< Name of this logger
255

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.
263
264
265
266
267
268
269
};

} // namespace log
} // namespace isc


#endif // __LOGGER_IMPL_H