logger_manager_impl.cc 9.33 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
#include <algorithm>
Stephen Morris's avatar
Stephen Morris committed
16
#include <iostream>
17

Stephen Morris's avatar
Stephen Morris committed
18
19
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
20
#include <log4cplus/hierarchy.h>
Stephen Morris's avatar
Stephen Morris committed
21
#include <log4cplus/consoleappender.h>
Stephen Morris's avatar
Stephen Morris committed
22
#include <log4cplus/fileappender.h>
23
#include <log4cplus/syslogappender.h>
Stephen Morris's avatar
Stephen Morris committed
24

25
#include <log/logger.h>
Jelte Jansen's avatar
Jelte Jansen committed
26
#include <log/logger_support.h>
27
28
29
30
31
32
#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>
33
#include <log/buffer_appender_impl.h>
34
35
36

using namespace std;

Stephen Morris's avatar
Stephen Morris committed
37
38
39
namespace isc {
namespace log {

Stephen Morris's avatar
Stephen Morris committed
40
41
42
43
44
// 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
45
void
46
LoggerManagerImpl::processInit() {
47
48
    storeBufferAppenders();

Stephen Morris's avatar
Stephen Morris committed
49
    log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
50
    initRootLogger();
Stephen Morris's avatar
Stephen Morris committed
51
52
}

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

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

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

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

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

        // 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:
91
                createSyslogAppender(logger, *i);
92
93
94
                break;

            default:
95
96
97
98
                // 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.
99
100
101
102
                isc_throw(UnknownLoggingDestination,
                          "Unknown logging destination, code = " <<
                          i->destination);
            }
Stephen Morris's avatar
Stephen Morris committed
103
104
105
106
107
108
109
110
111
112
113
114
        }
    }
}

// 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));
115
    setConsoleAppenderLayout(console);
Stephen Morris's avatar
Stephen Morris committed
116
117
118
    logger.addAppender(console);
}

Stephen Morris's avatar
Stephen Morris committed
119
120
121
122
123
124
// 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)
{
125
126
    // Append to existing file
    std::ios::openmode mode = std::ios::app;
Stephen Morris's avatar
Stephen Morris committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

    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
143
void
144
LoggerManagerImpl::createBufferAppender(log4cplus::Logger& logger) {
145
    log4cplus::SharedAppenderPtr bufferapp(new internal::BufferAppender());
Jelte Jansen's avatar
Jelte Jansen committed
146
147
    bufferapp->setName("buffer");
    logger.addAppender(bufferapp);
148
149
150
    // 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
151
152
153
}

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

Stephen Morris's avatar
Stephen Morris committed
164

165
166
// One-time initialization of the log4cplus system
void
167
168
169
LoggerManagerImpl::init(isc::log::Severity severity, int dbglevel,
                        bool buffer)
{
170
171
172
173
174
175
176
177
178
179
    // 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();

180
    initRootLogger(severity, dbglevel, buffer);
Stephen Morris's avatar
Stephen Morris committed
181
182
183
184
185
186
}

// 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
187
LoggerManagerImpl::reset(isc::log::Severity severity, int dbglevel)
188
{
Stephen Morris's avatar
Stephen Morris committed
189
    // Initialize the root logger
190
    initRootLogger(severity, dbglevel);
191
192
193
}

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

199
200
201
    // Set the log4cplus root to not output anything - effectively we are
    // ignoring it.
    log4cplus::Logger::getRoot().setLogLevel(log4cplus::OFF_LOG_LEVEL);
202

203
204
205
206
207
208
209
    // 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)));

210
211
212
213
214
215
    if (buffer) {
        createBufferAppender(b10root);
    } else {
        OutputOption opt;
        createConsoleAppender(b10root, opt);
    }
216
217
218
}

void LoggerManagerImpl::setConsoleAppenderLayout(
219
        log4cplus::SharedAppenderPtr& appender)
220
221
{
    // Create the pattern we want for the output - local time.
222
    string pattern = "%D{%Y-%m-%d %H:%M:%S.%q} %-5p [%c/%i] %m\n";
223
224
225
226
227
228

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

229
230
231
232
// 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).

233
void LoggerManagerImpl::setSyslogAppenderLayout(
234
235
236
        log4cplus::SharedAppenderPtr& appender)
{
    // Create the pattern we want for the output - local time.
237
    string pattern = "%-5p [%c] %m\n";
238
239
240
241
242
243

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

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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());
265
        assert(app != NULL);
266
267
268
269
        app->flush();
    }
}

Stephen Morris's avatar
Stephen Morris committed
270
271
} // namespace log
} // namespace isc