1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2012-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <log/buffer_appender_impl.h>

#include <log4cplus/loglevel.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/version.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/scoped_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdio><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace log {
namespace internal {

BufferAppender::~BufferAppender() {
    // If there is anything left in the buffer,
    // it means no reconfig has been done, and
    // we can assume the logging system was either
    // never setup, or broke while doing so.
    // So dump all that is left to stdout
    try {
        flushStdout();
        destructorImpl();
    } catch (...) {
        // Ok if we can't even seem to dump to stdout, never mind.
    }
}

void
BufferAppender::flushStdout() {
    // This does not show a bit of information normal log messages
    // do, so perhaps we should try and setup a new logger here
    // However, as this is called from a destructor, it may not
    // be a good idea; as we can't reliably know whether in what
    // state the logger instance is now (or what the specific logger's
    // settings were).
    for (auto const& it : stored_) {
        const std::string level(it.first);
        LogEventPtr event(it.second);
        std::printf("%s [%s]: %s\n", level.c_str(),
                    event->getLoggerName().c_str(),
                    event->getMessage().c_str());
    }
    stored_.clear();
}

void
BufferAppender::flush() {
    LogEventList stored_copy;
    stored_.swap(stored_copy);

    for (auto const& it : stored_copy) {
        LogEventPtr event(it.second);
        log4cplus::Logger logger =
            log4cplus::Logger::getInstance(event->getLoggerName());

        logger.log(event->getLogLevel(), event->getMessage());
    }
    flushed_ = true;
}

size_t
BufferAppender::getBufferSize() const {
    return (stored_.size());
}

void
BufferAppender::append(const log4cplus::spi::InternalLoggingEvent& event) {
    if (flushed_) {
        isc_throw(LogBufferAddAfterFlush,
                  "Internal log buffer has been flushed already");
    }
    // get a clone, and put the pointer in a shared_ptr in the list
#if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2, 0, 0)
    std::auto_ptr<log4cplus::spi::InternalLoggingEvent>
#else
    std::unique_ptr<log4cplus::spi::InternalLoggingEvent>
#endif
        event_aptr = event.clone();
    // Also store the string representation of the log level, to be
    // used in flushStdout if necessary
    stored_.push_back(LevelAndEvent(
                log4cplus::LogLevelManager().toString(event.getLogLevel()),
                LogEventPtr(event_aptr.release())));
}

} // end namespace internal
} // end namespace log
} // end namespace isc