Kea 2.7.5
buffer_appender_impl.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2024 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
16namespace isc {
17namespace log {
18namespace 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
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 for (auto const& it : stored_) {
43 const std::string level(it.first);
44 LogEventPtr event(it.second);
45 std::printf("%s [%s]: %s\n", level.c_str(),
46 event->getLoggerName().c_str(),
47 event->getMessage().c_str());
48 }
49 stored_.clear();
50}
51
52void
54 LogEventList stored_copy;
55 stored_.swap(stored_copy);
56
57 for (auto const& it : stored_copy) {
58 LogEventPtr event(it.second);
59 log4cplus::Logger logger =
60 log4cplus::Logger::getInstance(event->getLoggerName());
61
62 logger.log(event->getLogLevel(), event->getMessage());
63 }
64 flushed_ = true;
65}
66
67size_t
69 return (stored_.size());
70}
71
72void
73BufferAppender::append(const log4cplus::spi::InternalLoggingEvent& event) {
74 if (flushed_) {
76 "Internal log buffer has been flushed already");
77 }
78 // get a clone, and put the pointer in a shared_ptr in the list
79#if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2, 0, 0)
80 std::auto_ptr<log4cplus::spi::InternalLoggingEvent>
81#else
82 std::unique_ptr<log4cplus::spi::InternalLoggingEvent>
83#endif
84 event_aptr = event.clone();
85 // Also store the string representation of the log level, to be
86 // used in flushStdout if necessary
87 stored_.push_back(LevelAndEvent(
88 log4cplus::LogLevelManager().toString(event.getLogLevel()),
89 LogEventPtr(event_aptr.release())));
90}
91
92} // end namespace internal
93} // end namespace log
94} // end namespace isc
size_t getBufferSize() const
Returns the number of stored logging events.
virtual void append(const log4cplus::spi::InternalLoggingEvent &event)
void flush()
Flush the internal buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::pair< std::string, LogEventPtr > LevelAndEvent
Convenience typedef for a pair string/logeventptr, the string representing the logger level,...
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.
Defines the logger used by the top-level component of kea-lfc.