Kea  2.3.4-git
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 
10 
11 #include <log4cplus/loglevel.h>
12 #include <log4cplus/version.h>
13 #include <boost/scoped_ptr.hpp>
14 #include <cstdio>
15 
16 namespace isc {
17 namespace log {
18 namespace internal {
19 
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 
34 void
35 BufferAppender::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 
53 void
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 
69 size_t
71  return (stored_.size());
72 }
73 
74 void
75 BufferAppender::append(const log4cplus::spi::InternalLoggingEvent& event) {
76  if (flushed_) {
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
std::vector< LevelAndEvent > LogEventList
Convenience typedef for a vector of LevelAndEvent instances.
boost::shared_ptr< log4cplus::spi::InternalLoggingEvent > LogEventPtr
Convenience typedef for a pointer to a log event.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
virtual void append(const log4cplus::spi::InternalLoggingEvent &event)
std::pair< std::string, LogEventPtr > LevelAndEvent
Convenience typedef for a pair string/logeventptr, the string representing the logger level...
size_t getBufferSize() const
Returns the number of stored logging events.
Defines the logger used by the top-level component of kea-lfc.
isc::log::Logger logger("asiodns")
Use the ASIO logger.
void flush()
Flush the internal buffer.