Kea: buffer_appender_impl.cc Source File

| Kea 2.2.0

buffer_appender_impl.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <log/buffer_appender_impl.h>
10
11#include <log4cplus/loglevel.h>
12#include <log4cplus/version.h>
13#include <boost/scoped_ptr.hpp>
14#include <cstdio>
15
16namespace isc {
17 namespace log {
18 namespace internal {
19
20BufferAppender::~BufferAppender() {
21 // If there is anything left in the buffer,
22 // it means no reconfig has been done, and
23 // we can assume the logging system was either
24 // never setup, or broke while doing so.
25 // So dump all that is left to stdout
26 try {
27 flushStdout();
28 destructorImpl();
29 } catch (...) {
30 // Ok if we can't even seem to dump to stdout, never mind.
31 }
32}
33
34void
35BufferAppender::flushStdout() {
36 // This does not show a bit of information normal log messages
37 // do, so perhaps we should try and setup a new logger here
38 // However, as this is called from a destructor, it may not
39 // be a good idea; as we can't reliably know whether in what
40 // state the logger instance is now (or what the specific logger's
41 // settings were).
42 LogEventList::const_iterator it;
43 for (it = stored_.begin(); it != stored_.end(); ++it) {
44 const std::string level(it->first);
45 LogEventPtr event(it->second);
46 std::printf("%s [%s]: %s\n", level.c_str(),
47 event->getLoggerName().c_str(),
48 event->getMessage().c_str());
49 }
50 stored_.clear();
51}
52
53void
54BufferAppender::flush() {
55 LogEventList stored_copy;
56 stored_.swap(stored_copy);
57
58 LogEventList::const_iterator it;
59 for (it = stored_copy.begin(); it != stored_copy.end(); ++it) {
60 LogEventPtr event(it->second);
61 log4cplus::Logger logger =
62 log4cplus::Logger::getInstance(event->getLoggerName());
63
64 logger.log(event->getLogLevel(), event->getMessage());
65 }
66 flushed_ = true;
67}
68
69size_t
70BufferAppender::getBufferSize() const {
71 return (stored_.size());
72}
73
74void
75BufferAppender::append(const log4cplus::spi::InternalLoggingEvent& event) {
76 if (flushed_) {
77 isc_throw(LogBufferAddAfterFlush,
78 "Internal log buffer has been flushed already");
79 }
80 // get a clone, and put the pointer in a shared_ptr in the list
81#if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2, 0, 0)
82 std::auto_ptr<log4cplus::spi::InternalLoggingEvent>
83#else
84 std::unique_ptr<log4cplus::spi::InternalLoggingEvent>
85#endif
86 event_aptr = event.clone();
87 // Also store the string representation of the log level, to be
88 // used in flushStdout if necessary
89 stored_.push_back(LevelAndEvent(
90 log4cplus::LogLevelManager().toString(event.getLogLevel()),
91 LogEventPtr(event_aptr.release())));
92}
93
94} // end namespace internal
95} // end namespace log
96} // end namespace isc
buffer_appender_impl.h
isc::log::internal::BufferAppender::getBufferSize
size_t getBufferSize() const
Returns the number of stored logging events.
Definition: buffer_appender_impl.cc:70
isc::log::internal::BufferAppender::~BufferAppender
virtual ~BufferAppender()
Destructor.
Definition: buffer_appender_impl.cc:20
isc::log::internal::BufferAppender::append
virtual void append(const log4cplus::spi::InternalLoggingEvent &event)
Definition: buffer_appender_impl.cc:75
isc::log::internal::BufferAppender::flush
void flush()
Flush the internal buffer.
Definition: buffer_appender_impl.cc:54
isc::log::internal::LogBufferAddAfterFlush
Buffer add after flush.
Definition: buffer_appender_impl.h:26
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:210
isc::asiodns::logger
isc::log::Logger logger("asiodns")
Use the ASIO logger.
Definition: asiodns/logger.h:15
isc::log::internal::LevelAndEvent
std::pair< std::string, LogEventPtr > LevelAndEvent
Convenience typedef for a pair string/logeventptr, the string representing the logger level,...
Definition: buffer_appender_impl.h:39
isc::log::internal::LogEventList
std::vector< LevelAndEvent > LogEventList
Convenience typedef for a vector of LevelAndEvent instances.
Definition: buffer_appender_impl.h:42
isc::log::internal::LogEventPtr
boost::shared_ptr< log4cplus::spi::InternalLoggingEvent > LogEventPtr
Convenience typedef for a pointer to a log event.
Definition: buffer_appender_impl.h:34
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143