Kea  2.1.7-git
log_utils.cc
Go to the documentation of this file.
1 // Copyright (C) 2016-2021 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 
13 namespace isc {
14 namespace dhcp {
15 namespace 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;
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 
45  remFile();
46 }
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 
84 size_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 
117 void LogContentTest::addString(const string& new_string) {
118  exp_strings_.push_back(new_string);
119 }
120 
121 // Set up the name of the LOG_FILE for use in checking
122 // the debug statements.
123 // Must not be the same file name used by test shell scripts.
124 const char* LogContentTest::LOG_FILE = "logtest.log";
125 
126 // The environment variable to overwrite the verbose_ default value.
127 const char* LogContentTest::KEA_LOG_CHECK_VERBOSE = "KEA_LOG_CHECK_VERBOSE";
128 
129 } // end of isc::dhcp::test namespace
130 } // end of isc::dhcp namespace
131 } // end of isc namespace
const std::string & getRootLoggerName()
Get root logger name.
Definition: logger_name.cc:33
void addOutputOption(const OutputOption &option)
Add output option.
isc::log::Severity keaLoggerSeverity(isc::log::Severity defseverity)
Obtains logging severity from KEA_LOGGER_SEVERITY.
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.
int keaLoggerDbglevel(int defdbglevel)
Obtains logging debug level from KEA_LOGGER_DBGLEVEL.
Logger Manager.
STL namespace.
static const char * LOG_FILE
Definition: log_utils.h:89
bool verbose_
controls whether the checkFile() should print more details.
Definition: log_utils.h:100
LogContentTest()
Initializes the logger setup for using in checking log statements.
Definition: log_utils.cc:17
static const char * KEA_LOG_CHECK_VERBOSE
Definition: log_utils.h:90
Destination destination
Members.
Definition: output_option.h:68
vector< string > exp_strings_
Definition: log_utils.h:88
Defines the logger used by the top-level component of kea-lfc.
void remFile()
remove the test log file
Definition: log_utils.cc:113
bool checkFile()
check that the requested strings are in the test log file in the requested order. ...
Definition: log_utils.cc:48
std::string filename
Filename if file output.
Definition: output_option.h:72
size_t countFile(const string &exp_string)
check that the requested string is in the test log file.
Definition: log_utils.cc:84
const int MAX_DEBUG_LEVEL
Definition: logger_level.h:36