Kea: log_utils.cc Source File

| Kea 3.0.0

log_utils.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2025 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 <testutils/log_utils.h>
10#include <cstdlib>
11#include <iostream>
12
13namespace isc {
14namespace dhcp {
15 namespace test {
16
17LogContentTest::LogContentTest()
18 :verbose_(false) {
19 // Get rid of any old files
20 remFile();
21
22 // Set up the logger for use in checking the debug statements.
23 // We send the debug statements to a file which we can
24 // check after the evaluations have completed. We also
25 // set the log severity and debug levels so that the log
26 // statements are executed.
27 LoggerSpecification spec(getRootLoggerName(),
28 keaLoggerSeverity(isc::log::DEBUG),
29 keaLoggerDbglevel(isc::log::MAX_DEBUG_LEVEL));
30 OutputOption option;
31 option.destination = OutputOption::DEST_FILE;
32 option.filename = string(LogContentTest::LOG_FILE);
33 spec.addOutputOption(option);
34 LoggerManager manager;
35 manager.process(spec);
36
37 // Overwrite the verbose_ default is the KEA_LOG_CHECK_VERBOSE
38 // environment variable exists.
39 if (getenv(KEA_LOG_CHECK_VERBOSE)) {
40 verbose_ = true;
41 }
42}
43
44LogContentTest:: ~LogContentTest() {
45 remFile();
46}
47
48 void LogContentTest::reset() {
49 // Reset to default (INFO) with output to stdout.
50 // To be used to avoid spurious error messages
51 // "log4cplus:ERROR file is not open: logtest.log".
52 LoggerSpecification spec(getRootLoggerName());
53 OutputOption option;
54 // Change the default to stdout.
55 option.stream = OutputOption::STR_STDOUT;
56 spec.addOutputOption(option);
57 LoggerManager manager;
58 manager.process(spec);
59}
60
61 bool LogContentTest::checkFile() {
62 ifstream file(LOG_FILE);
63 EXPECT_TRUE(file.is_open());
64 string line, exp_line;
65 unsigned i = 0;
66 bool found = true;
67
68 using namespace std;
69
70 while (getline(file, line) && (i != exp_strings_.size())) {
71 exp_line = exp_strings_[i];
72 if (verbose_) {
73 cout << "Read line : " << line << endl;
74 cout << "Looking for: " << exp_line << endl;
75 }
76 i++;
77 if (string::npos == line.find(exp_line)) {
78 if (verbose_) {
79 cout << "Verdict : not found" << endl;
80 }
81 found = false;
82 }
83 }
84
85 file.close();
86
87 if ((i != exp_strings_.size()) || (found == false)) {
88 if (verbose_) {
89 cout << "Final verdict: false" << endl;
90 }
91 return (false);
92 }
93
94 return (true);
95}
96
97 size_t LogContentTest::countFile(const string& exp_string) {
98 ifstream file(LOG_FILE);
99 EXPECT_TRUE(file.is_open());
100 string line;
101 size_t cnt = 0;
102
103 using namespace std;
104
105 if (verbose_) {
106 cout << "Looking for:" << exp_string << endl;
107 }
108 while (getline(file, line)) {
109 if (verbose_) {
110 cout << "Read line :" << line << endl;
111 }
112 if (line.find(exp_string) != string::npos) {
113 ++cnt;
114 }
115 }
116
117 file.close();
118
119 if (verbose_) {
120 cout << "Final count: " << cnt << endl;
121 }
122
123 return (cnt);
124}
125
126 void LogContentTest::remFile() {
127 static_cast<void>(remove(LOG_FILE));
128}
129
130 void LogContentTest::addString(const string& new_string) {
131 exp_strings_.push_back(new_string);
132}
133
134 void LogContentTest::addString(const string& logger_msg, const string& new_string, const string& label) {
135 std::ostringstream stream;
136 stream << logger_msg << " " << label << ": " << new_string;
137 exp_strings_.push_back(stream.str());
138}
139
140// Set up the name of the LOG_FILE for use in checking
141// the debug statements.
142// Must not be the same file name used by test shell scripts.
143const char* LogContentTest::LOG_FILE = "logtest.log";
144
145// The environment variable to overwrite the verbose_ default value.
146const char* LogContentTest::KEA_LOG_CHECK_VERBOSE = "KEA_LOG_CHECK_VERBOSE";
147
148} // end of isc::dhcp::test namespace
149} // end of isc::dhcp namespace
150} // end of isc namespace
isc::dhcp::test::LogContentTest::LogContentTest
LogContentTest()
Initializes the logger setup for using in checking log statements.
Definition log_utils.cc:17
isc::dhcp::test::LogContentTest::reset
void reset()
reset the logger to INFO stdout.
Definition log_utils.cc:48
isc::dhcp::test::LogContentTest::verbose_
bool verbose_
controls whether the checkFile() should print more details.
Definition log_utils.h:109
isc::dhcp::test::LogContentTest::checkFile
bool checkFile()
check that the requested strings are in the test log file in the requested order.
Definition log_utils.cc:61
isc::dhcp::test::LogContentTest::KEA_LOG_CHECK_VERBOSE
static const char * KEA_LOG_CHECK_VERBOSE
Definition log_utils.h:99
isc::dhcp::test::LogContentTest::~LogContentTest
virtual ~LogContentTest()
Definition log_utils.cc:44
isc::dhcp::test::LogContentTest::remFile
void remFile()
remove the test log file
Definition log_utils.cc:126
isc::dhcp::test::LogContentTest::exp_strings_
vector< string > exp_strings_
Definition log_utils.h:97
isc::dhcp::test::LogContentTest::LOG_FILE
static const char * LOG_FILE
Definition log_utils.h:98
isc::dhcp::test::LogContentTest::countFile
size_t countFile(const string &exp_string)
check that the requested string is in the test log file.
Definition log_utils.cc:97
isc::dhcp::test::LogContentTest::addString
void addString(const string &new_string)
Add a string to the vector of expected strings.
Definition log_utils.cc:130
isc::log::LoggerManager
Logger Manager.
Definition logger_manager.h:39
isc::log::LoggerManager::process
void process(T start, T finish)
Process Specifications.
Definition logger_manager.h:56
isc::log::LoggerSpecification
Definition logger_specification.h:29
isc::log::LoggerSpecification::addOutputOption
void addOutputOption(const OutputOption &option)
Add output option.
Definition logger_specification.h:99
log_utils.h
isc::dhcp::test
Definition log_utils.cc:15
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::log::getRootLoggerName
const std::string & getRootLoggerName()
Get root logger name.
Definition logger_name.cc:33
isc::log::MAX_DEBUG_LEVEL
const int MAX_DEBUG_LEVEL
Definition logger_level.h:36
isc::log::keaLoggerDbglevel
int keaLoggerDbglevel(int defdbglevel)
Obtains logging debug level from KEA_LOGGER_DBGLEVEL.
Definition logger_unittest_support.cc:44
isc::log::DEBUG
@ DEBUG
Definition logger_level.h:25
isc::log::keaLoggerSeverity
isc::log::Severity keaLoggerSeverity(isc::log::Severity defseverity)
Obtains logging severity from KEA_LOGGER_SEVERITY.
Definition logger_unittest_support.cc:32
isc::util::file
Definition filesystem.cc:28
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::log::OutputOption
Definition output_option.h:37
isc::log::OutputOption::STR_STDOUT
@ STR_STDOUT
Definition output_option.h:56
isc::log::OutputOption::destination
Destination destination
Members.
Definition output_option.h:68
isc::log::OutputOption::DEST_FILE
@ DEST_FILE
Definition output_option.h:50
isc::log::OutputOption::stream
Stream stream
stdout/stderr if console output
Definition output_option.h:69
isc::log::OutputOption::filename
std::string filename
Filename if file output.
Definition output_option.h:72