Kea 2.7.6
log_utils.cc
Go to the documentation of this file.
1// Copyright (C) 2016-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#include <cstdlib>
11#include <iostream>
12
13namespace isc {
14namespace dhcp {
15namespace test {
16
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.
30 OutputOption option;
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
47
49 ifstream file(LOG_FILE);
50 EXPECT_TRUE(file.is_open());
51 string line, exp_line;
52 int i = 0;
53 bool found = true;
54
55 using namespace std;
56
57 while (getline(file, line) && (i != exp_strings_.size())) {
58 exp_line = exp_strings_[i];
59 if (verbose_) {
60 cout << "Read line : " << line << endl;
61 cout << "Looking for: " << exp_line << endl;
62 }
63 i++;
64 if (string::npos == line.find(exp_line)) {
65 if (verbose_) {
66 cout << "Verdict : not found" << endl;
67 }
68 found = false;
69 }
70 }
71
72 file.close();
73
74 if ((i != exp_strings_.size()) || (found == false)) {
75 if (verbose_) {
76 cout << "Final verdict: false" << endl;
77 }
78 return (false);
79 }
80
81 return (true);
82}
83
84size_t LogContentTest::countFile(const string& exp_string) {
85 ifstream file(LOG_FILE);
86 EXPECT_TRUE(file.is_open());
87 string line;
88 size_t cnt = 0;
89
90 using namespace std;
91
92 if (verbose_) {
93 cout << "Looking for:" << exp_string << endl;
94 }
95 while (getline(file, line)) {
96 if (verbose_) {
97 cout << "Read line :" << line << endl;
98 }
99 if (line.find(exp_string) != string::npos) {
100 ++cnt;
101 }
102 }
103
104 file.close();
105
106 if (verbose_) {
107 cout << "Final count: " << cnt << endl;
108 }
109
110 return (cnt);
111}
112
114 static_cast<void>(remove(LOG_FILE));
115}
116
117void LogContentTest::addString(const string& new_string) {
118 exp_strings_.push_back(new_string);
119}
120
121void LogContentTest::addString(const string& logger_msg, const string& new_string, const string& label) {
122 std::ostringstream stream;
123 stream << logger_msg << " " << label << ": " << new_string;
124 exp_strings_.push_back(stream.str());
125}
126
127// Set up the name of the LOG_FILE for use in checking
128// the debug statements.
129// Must not be the same file name used by test shell scripts.
130const char* LogContentTest::LOG_FILE = "logtest.log";
131
132// The environment variable to overwrite the verbose_ default value.
133const char* LogContentTest::KEA_LOG_CHECK_VERBOSE = "KEA_LOG_CHECK_VERBOSE";
134
135} // end of isc::dhcp::test namespace
136} // end of isc::dhcp namespace
137} // end of isc namespace
LogContentTest()
Initializes the logger setup for using in checking log statements.
Definition log_utils.cc:17
bool verbose_
controls whether the checkFile() should print more details.
Definition log_utils.h:106
bool checkFile()
check that the requested strings are in the test log file in the requested order.
Definition log_utils.cc:48
static const char * KEA_LOG_CHECK_VERBOSE
Definition log_utils.h:96
void remFile()
remove the test log file
Definition log_utils.cc:113
vector< string > exp_strings_
Definition log_utils.h:94
static const char * LOG_FILE
Definition log_utils.h:95
size_t countFile(const string &exp_string)
check that the requested string is in the test log file.
Definition log_utils.cc:84
void addString(const string &new_string)
Add a string to the vector of expected strings.
Definition log_utils.cc:117
void process(T start, T finish)
Process Specifications.
void addOutputOption(const OutputOption &option)
Add output option.
const std::string & getRootLoggerName()
Get root logger name.
const int MAX_DEBUG_LEVEL
int keaLoggerDbglevel(int defdbglevel)
Obtains logging debug level from KEA_LOGGER_DBGLEVEL.
isc::log::Severity keaLoggerSeverity(isc::log::Severity defseverity)
Obtains logging severity from KEA_LOGGER_SEVERITY.
Defines the logger used by the top-level component of kea-lfc.
Destination destination
Members.
std::string filename
Filename if file output.