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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 | // Copyright (C) 2012-2015 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 <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log/macros.h>
#include <log/logger_support.h>
#include <log/log_messages.h>
#include <log/buffer_appender_impl.h>
#include <log4cplus/loggingmacros.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/logger.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/nullappender.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/spi/loggingevent.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::log;
using namespace isc::log::internal;
namespace isc {
namespace log {
/// \brief Specialized test class
///
/// In order to test append() somewhat directly, this
/// class implements one more method (addEvent)
class TestBufferAppender : public BufferAppender {
public:
void addEvent(const log4cplus::spi::InternalLoggingEvent& event) {
append(event);
}
};
class BufferAppenderTest : public ::testing::Test {
protected:
BufferAppenderTest() : buffer_appender1(new TestBufferAppender()),
appender1(buffer_appender1),
buffer_appender2(new TestBufferAppender()),
appender2(buffer_appender2),
logger(log4cplus::Logger::getInstance("buffer"))
{
logger.setLogLevel(log4cplus::TRACE_LOG_LEVEL);
}
~BufferAppenderTest() {
// If any log messages are left, we don't care, get rid of them,
// by flushing them to a null appender
// Given the 'messages should not get lost' approach of the logging
// system, not flushing them to a null appender would cause them
// to be dumped to stdout as the test is destroyed, making
// unnecessarily messy test output.
log4cplus::SharedAppenderPtr null_appender(
new log4cplus::NullAppender());
logger.removeAllAppenders();
logger.addAppender(null_appender);
buffer_appender1->flush();
buffer_appender2->flush();
}
TestBufferAppender* buffer_appender1;
log4cplus::SharedAppenderPtr appender1;
TestBufferAppender* buffer_appender2;
log4cplus::SharedAppenderPtr appender2;
log4cplus::Logger logger;
};
// Test that log events are indeed stored, and that they are
// flushed to the new appenders of their logger
TEST_F(BufferAppenderTest, flush) {<--- syntax error
ASSERT_EQ(0, buffer_appender1->getBufferSize());
ASSERT_EQ(0, buffer_appender2->getBufferSize());
// Create a Logger, log a few messages with the first appender
logger.addAppender(appender1);
LOG4CPLUS_INFO(logger, "Foo");
ASSERT_EQ(1, buffer_appender1->getBufferSize());
LOG4CPLUS_INFO(logger, "Foo");
ASSERT_EQ(2, buffer_appender1->getBufferSize());
LOG4CPLUS_INFO(logger, "Foo");
ASSERT_EQ(3, buffer_appender1->getBufferSize());
// Second buffer should still be empty
ASSERT_EQ(0, buffer_appender2->getBufferSize());
// Replace the appender by the second one, and call flush;
// this should cause all events to be moved to the second buffer
logger.removeAllAppenders();
logger.addAppender(appender2);
buffer_appender1->flush();
ASSERT_EQ(0, buffer_appender1->getBufferSize());
ASSERT_EQ(3, buffer_appender2->getBufferSize());
}
// Once flushed, logging new messages with the same buffer should fail
TEST_F(BufferAppenderTest, addAfterFlush) {
logger.addAppender(appender1);
buffer_appender1->flush();
EXPECT_THROW(LOG4CPLUS_INFO(logger, "Foo"), LogBufferAddAfterFlush);
// It should not have been added
ASSERT_EQ(0, buffer_appender1->getBufferSize());
// But logging should work again as long as a different buffer is used
logger.removeAllAppenders();
logger.addAppender(appender2);
LOG4CPLUS_INFO(logger, "Foo");
ASSERT_EQ(1, buffer_appender2->getBufferSize());
}
TEST_F(BufferAppenderTest, addDirectly) {
// A few direct calls
log4cplus::spi::InternalLoggingEvent event("buffer",
log4cplus::INFO_LOG_LEVEL,
"Bar", "file", 123);
buffer_appender1->addEvent(event);
ASSERT_EQ(1, buffer_appender1->getBufferSize());
// Do one from a smaller scope to make sure destruction doesn't harm
{
log4cplus::spi::InternalLoggingEvent event2("buffer",
log4cplus::INFO_LOG_LEVEL,
"Bar", "file", 123);
buffer_appender1->addEvent(event2);
}
ASSERT_EQ(2, buffer_appender1->getBufferSize());
// And flush them to the next
logger.removeAllAppenders();
logger.addAppender(appender2);
buffer_appender1->flush();
ASSERT_EQ(0, buffer_appender1->getBufferSize());
ASSERT_EQ(2, buffer_appender2->getBufferSize());
}
}
}
|