logger_manager_impl.cc 9.65 KB
Newer Older
Stephen Morris's avatar
Stephen Morris committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
// 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.

15
16
#include <config.h>

17
#include <algorithm>
Stephen Morris's avatar
Stephen Morris committed
18
#include <iostream>
19

Stephen Morris's avatar
Stephen Morris committed
20
21
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
22
#include <log4cplus/hierarchy.h>
Stephen Morris's avatar
Stephen Morris committed
23
#include <log4cplus/consoleappender.h>
Stephen Morris's avatar
Stephen Morris committed
24
#include <log4cplus/fileappender.h>
25
#include <log4cplus/syslogappender.h>
26
#include <log4cplus/helpers/loglog.h>
Stephen Morris's avatar
Stephen Morris committed
27

28
#include <log/logger.h>
Jelte Jansen's avatar
Jelte Jansen committed
29
#include <log/logger_support.h>
30
31
32
33
34
35
#include <log/logger_level_impl.h>
#include <log/logger_manager.h>
#include <log/logger_manager_impl.h>
#include <log/log_messages.h>
#include <log/logger_name.h>
#include <log/logger_specification.h>
36
#include <log/buffer_appender_impl.h>
37
38
39

using namespace std;

Stephen Morris's avatar
Stephen Morris committed
40
41
42
namespace isc {
namespace log {

Stephen Morris's avatar
Stephen Morris committed
43
44
45
46
47
// Reset hierarchy of loggers back to default settings.  This removes all
// appenders from loggers, sets their severity to NOT_SET (so that events are
// passed back to the parent) and resets the root logger to logging
// informational messages.  (This last is not a log4cplus default, so we have to
// explicitly reset the logging severity.)
Stephen Morris's avatar
Stephen Morris committed
48
void
49
LoggerManagerImpl::processInit() {
50
51
    storeBufferAppenders();

Stephen Morris's avatar
Stephen Morris committed
52
    log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
53
    initRootLogger();
Stephen Morris's avatar
Stephen Morris committed
54
55
}

Jelte Jansen's avatar
Jelte Jansen committed
56
// Flush the BufferAppenders at the end of processing a new specification
57
58
void
LoggerManagerImpl::processEnd() {
59
    flushBufferAppenders();
60
61
}

Stephen Morris's avatar
Stephen Morris committed
62
63
64
65
// Process logging specification.  Set up the common states then dispatch to
// add output specifications.
void
LoggerManagerImpl::processSpecification(const LoggerSpecification& spec) {
66
67
    log4cplus::Logger logger = log4cplus::Logger::getInstance(
                                   expandLoggerName(spec.getName()));
Stephen Morris's avatar
Stephen Morris committed
68
69
70
71
72
73
74
75

    // Set severity level according to specification entry.
    logger.setLogLevel(LoggerLevelImpl::convertFromBindLevel(
                       Level(spec.getSeverity(), spec.getDbglevel())));

    // Set the additive flag.
    logger.setAdditivity(spec.getAdditive());

76
77
    // Output options given?
    if (spec.optionCount() > 0) {
78
79
        // Replace all appenders for this logger.
        logger.removeAllAppenders();
80
81
82
83
84
85
86
87
88
89
90
91
92
93

        // Now process output specifications.
        for (LoggerSpecification::const_iterator i = spec.begin();
             i != spec.end(); ++i) {
            switch (i->destination) {
            case OutputOption::DEST_CONSOLE:
                createConsoleAppender(logger, *i);
                break;

            case OutputOption::DEST_FILE:
                createFileAppender(logger, *i);
                break;

            case OutputOption::DEST_SYSLOG:
94
                createSyslogAppender(logger, *i);
95
96
97
                break;

            default:
98
99
100
101
                // Not a valid destination.  As we are in the middle of updating
                // logging destinations, we could be in the situation where
                // there are no valid appenders.  For this reason, throw an
                // exception.
102
103
104
105
                isc_throw(UnknownLoggingDestination,
                          "Unknown logging destination, code = " <<
                          i->destination);
            }
Stephen Morris's avatar
Stephen Morris committed
106
107
108
109
110
111
112
113
114
115
116
117
        }
    }
}

// Console appender - log to either stdout or stderr.
void
LoggerManagerImpl::createConsoleAppender(log4cplus::Logger& logger,
                                         const OutputOption& opt)
{
    log4cplus::SharedAppenderPtr console(
        new log4cplus::ConsoleAppender(
            (opt.stream == OutputOption::STR_STDERR), opt.flush));
118
    setConsoleAppenderLayout(console);
Stephen Morris's avatar
Stephen Morris committed
119
120
121
    logger.addAppender(console);
}

Stephen Morris's avatar
Stephen Morris committed
122
123
124
125
126
127
// File appender.  Depending on whether a maximum size is given, either
// a standard file appender or a rolling file appender will be created.
void
LoggerManagerImpl::createFileAppender(log4cplus::Logger& logger,
                                         const OutputOption& opt)
{
128
129
    // Append to existing file
    std::ios::openmode mode = std::ios::app;
Stephen Morris's avatar
Stephen Morris committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

    log4cplus::SharedAppenderPtr fileapp;
    if (opt.maxsize == 0) {
        fileapp = log4cplus::SharedAppenderPtr(new log4cplus::FileAppender(
            opt.filename, mode, opt.flush));
    } else {
        fileapp = log4cplus::SharedAppenderPtr(
            new log4cplus::RollingFileAppender(opt.filename, opt.maxsize,
                                               opt.maxver, opt.flush));
    }

    // use the same console layout for the files.
    setConsoleAppenderLayout(fileapp);
    logger.addAppender(fileapp);
}

Jelte Jansen's avatar
Jelte Jansen committed
146
void
147
LoggerManagerImpl::createBufferAppender(log4cplus::Logger& logger) {
148
    log4cplus::SharedAppenderPtr bufferapp(new internal::BufferAppender());
Jelte Jansen's avatar
Jelte Jansen committed
149
150
    bufferapp->setName("buffer");
    logger.addAppender(bufferapp);
151
152
153
    // Since we do not know at what level the loggers will end up
    // running, set it to the highest for now
    logger.setLogLevel(log4cplus::TRACE_LOG_LEVEL);
Jelte Jansen's avatar
Jelte Jansen committed
154
155
156
}

// Syslog appender.
157
void
158
LoggerManagerImpl::createSyslogAppender(log4cplus::Logger& logger,
159
160
161
162
                                         const OutputOption& opt)
{
    log4cplus::SharedAppenderPtr syslogapp(
        new log4cplus::SysLogAppender(opt.facility));
163
    setSyslogAppenderLayout(syslogapp);
164
165
166
    logger.addAppender(syslogapp);
}

Stephen Morris's avatar
Stephen Morris committed
167

168
169
// One-time initialization of the log4cplus system
void
170
171
172
LoggerManagerImpl::init(isc::log::Severity severity, int dbglevel,
                        bool buffer)
{
173
174
175
176
177
178
179
180
181
182
    // Set up basic configurator.  This attaches a ConsoleAppender to the
    // root logger with suitable output.  This is used until we we have
    // actually read the logging configuration, in which case the output
    // may well be changed.
    log4cplus::BasicConfigurator config;
    config.configure();

    // Add the additional debug levels
    LoggerLevelImpl::init();

183
    initRootLogger(severity, dbglevel, buffer);
Stephen Morris's avatar
Stephen Morris committed
184
185
186
187
188
189
}

// Reset logging to default configuration.  This closes all appenders
// and resets the root logger to output INFO messages to the console.
// It is principally used in testing.
void
190
LoggerManagerImpl::reset(isc::log::Severity severity, int dbglevel)
191
{
Stephen Morris's avatar
Stephen Morris committed
192
    // Initialize the root logger
193
    initRootLogger(severity, dbglevel);
194
195
196
}

// Initialize the root logger
197
void LoggerManagerImpl::initRootLogger(isc::log::Severity severity,
198
                                       int dbglevel, bool buffer)
Stephen Morris's avatar
Stephen Morris committed
199
200
{
    log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
201

202
203
204
205
206
207
208
    // Disable log4cplus' own logging, unless --enable-debug was
    // specified to configure. Note that this does not change
    // LogLog's levels (that is still just INFO).
#ifndef ENABLE_DEBUG
    log4cplus::helpers::LogLog::getLogLog()->setQuietMode(true);
#endif

209
210
211
    // Set the log4cplus root to not output anything - effectively we are
    // ignoring it.
    log4cplus::Logger::getRoot().setLogLevel(log4cplus::OFF_LOG_LEVEL);
212

213
214
215
216
217
218
219
    // Set the level for the BIND 10 root logger to the given severity and
    // debug level.
    log4cplus::Logger b10root = log4cplus::Logger::getInstance(
                                                    getRootLoggerName());
    b10root.setLogLevel(LoggerLevelImpl::convertFromBindLevel(
                                                    Level(severity, dbglevel)));

220
221
222
223
224
225
    if (buffer) {
        createBufferAppender(b10root);
    } else {
        OutputOption opt;
        createConsoleAppender(b10root, opt);
    }
226
227
228
}

void LoggerManagerImpl::setConsoleAppenderLayout(
229
        log4cplus::SharedAppenderPtr& appender)
230
231
{
    // Create the pattern we want for the output - local time.
232
    string pattern = "%D{%Y-%m-%d %H:%M:%S.%q} %-5p [%c/%i] %m\n";
233
234
235
236
237
238

    // Finally the text of the message
    auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(pattern));
    appender->setLayout(layout);
}

239
240
241
242
// Set the the "syslog" layout for the given appenders.  This is the same
// as the console, but without the timestamp (which is expected to be
// set by syslogd).

243
void LoggerManagerImpl::setSyslogAppenderLayout(
244
245
246
        log4cplus::SharedAppenderPtr& appender)
{
    // Create the pattern we want for the output - local time.
247
    string pattern = "%-5p [%c] %m\n";
248
249
250
251
252
253

    // Finally the text of the message
    auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(pattern));
    appender->setLayout(layout);
}

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
void LoggerManagerImpl::storeBufferAppenders() {
    // Walk through all loggers, and find any buffer appenders there
    log4cplus::LoggerList loggers = log4cplus::Logger::getCurrentLoggers();
    log4cplus::LoggerList::iterator it;
    for (it = loggers.begin(); it != loggers.end(); ++it) {
        log4cplus::SharedAppenderPtr buffer_appender =
            it->getAppender("buffer");
        if (buffer_appender) {
            buffer_appender_store_.push_back(buffer_appender);
        }
    }
}

void LoggerManagerImpl::flushBufferAppenders() {
    std::vector<log4cplus::SharedAppenderPtr> copy;
    buffer_appender_store_.swap(copy);

    std::vector<log4cplus::SharedAppenderPtr>::iterator it;
    for (it = copy.begin(); it != copy.end(); ++it) {
        internal::BufferAppender* app =
            dynamic_cast<internal::BufferAppender*>(it->get());
275
        assert(app != NULL);
276
277
278
279
        app->flush();
    }
}

Stephen Morris's avatar
Stephen Morris committed
280
281
} // namespace log
} // namespace isc