Kea: buffer_appender_impl.cc Source File

| Kea 3.2.0-git

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
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 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
53BufferAppender::flush() {
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
68BufferAppender::getBufferSize() const {
69 return (stored_.size());
70}
71
72void
73BufferAppender::append(const log4cplus::spi::InternalLoggingEvent& event) {
74 if (flushed_) {
75 isc_throw(LogBufferAddAfterFlush,
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
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:68
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:73
isc::log::internal::BufferAppender::flush
void flush()
Flush the internal buffer.
Definition buffer_appender_impl.cc:53
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::log::internal
Definition buffer_appender_impl.cc:18
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::log
Definition buffer_appender_impl.cc:17
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18