EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LoggerTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file LoggerTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
11 #include <boost/test/unit_test.hpp>
12 
14 
15 #include <fstream>
16 #include <string>
17 
18 namespace Acts {
19 namespace Test {
20 
21 using namespace Acts::Logging;
22 
24 namespace detail {
25 std::unique_ptr<const Logger> create_logger(const std::string& logger_name,
26  std::ostream* logfile,
27  Logging::Level lvl) {
28  auto output = std::make_unique<LevelOutputDecorator>(
29  std::make_unique<NamedOutputDecorator>(
30  std::make_unique<DefaultPrintPolicy>(logfile), logger_name));
31  auto print = std::make_unique<DefaultFilterPolicy>(lvl);
32  return std::make_unique<const Logger>(std::move(output), std::move(print));
33 }
34 
35 std::string failure_msg(const std::string& expected, const std::string& found) {
36  return std::string("'") + expected + "' != '" + found + "'";
37 }
38 } // namespace detail
40 
47 BOOST_AUTO_TEST_CASE(FATAL_test) {
48  std::ofstream logfile("fatal_log.txt");
49 
50  auto log = detail::create_logger("TestLogger", &logfile, FATAL);
51  ACTS_LOCAL_LOGGER(std::move(log));
52  ACTS_FATAL("fatal level");
53  ACTS_ERROR("error level");
54  ACTS_WARNING("warning level");
55  ACTS_INFO("info level");
56  ACTS_DEBUG("debug level");
57  ACTS_VERBOSE("verbose level");
58  logfile.close();
59 
60  std::vector<std::string> lines;
61  lines.push_back("TestLogger FATAL fatal level");
62 
63  std::ifstream infile("fatal_log.txt", std::ios::in);
64  size_t i = 0;
65  for (std::string line; std::getline(infile, line); ++i) {
66  BOOST_CHECK_EQUAL(line, lines.at(i));
67  }
68 }
69 
76 BOOST_AUTO_TEST_CASE(ERROR_test) {
77  std::ofstream logfile("error_log.txt");
78 
79  auto log = detail::create_logger("TestLogger", &logfile, ERROR);
80  ACTS_LOCAL_LOGGER(std::move(log));
81  ACTS_FATAL("fatal level");
82  ACTS_ERROR("error level");
83  ACTS_WARNING("warning level");
84  ACTS_INFO("info level");
85  ACTS_DEBUG("debug level");
86  ACTS_VERBOSE("verbose level");
87  logfile.close();
88 
89  std::vector<std::string> lines;
90  lines.push_back("TestLogger FATAL fatal level");
91  lines.push_back("TestLogger ERROR error level");
92 
93  std::ifstream infile("error_log.txt", std::ios::in);
94  size_t i = 0;
95  for (std::string line; std::getline(infile, line); ++i) {
96  BOOST_CHECK_EQUAL(line, lines.at(i));
97  }
98 }
99 
106 BOOST_AUTO_TEST_CASE(WARNING_test) {
107  std::ofstream logfile("warning_log.txt");
108 
109  auto log = detail::create_logger("TestLogger", &logfile, WARNING);
110  ACTS_LOCAL_LOGGER(std::move(log));
111  ACTS_FATAL("fatal level");
112  ACTS_ERROR("error level");
113  ACTS_WARNING("warning level");
114  ACTS_INFO("info level");
115  ACTS_DEBUG("debug level");
116  ACTS_VERBOSE("verbose level");
117  logfile.close();
118 
119  std::vector<std::string> lines;
120  lines.push_back("TestLogger FATAL fatal level");
121  lines.push_back("TestLogger ERROR error level");
122  lines.push_back("TestLogger WARNING warning level");
123 
124  std::ifstream infile("warning_log.txt", std::ios::in);
125  size_t i = 0;
126  for (std::string line; std::getline(infile, line); ++i) {
127  BOOST_CHECK_EQUAL(line, lines.at(i));
128  }
129 }
130 
138  std::ofstream logfile("info_log.txt");
139 
140  auto log = detail::create_logger("TestLogger", &logfile, INFO);
141  ACTS_LOCAL_LOGGER(std::move(log));
142  ACTS_FATAL("fatal level");
143  ACTS_ERROR("error level");
144  ACTS_WARNING("warning level");
145  ACTS_INFO("info level");
146  ACTS_DEBUG("debug level");
147  ACTS_VERBOSE("verbose level");
148  logfile.close();
149 
150  std::vector<std::string> lines;
151  lines.push_back("TestLogger FATAL fatal level");
152  lines.push_back("TestLogger ERROR error level");
153  lines.push_back("TestLogger WARNING warning level");
154  lines.push_back("TestLogger INFO info level");
155 
156  std::ifstream infile("info_log.txt", std::ios::in);
157  size_t i = 0;
158  for (std::string line; std::getline(infile, line); ++i) {
159  BOOST_CHECK_EQUAL(line, lines.at(i));
160  }
161 }
162 
169 BOOST_AUTO_TEST_CASE(DEBUG_test) {
170  std::ofstream logfile("debug_log.txt");
171 
172  auto log = detail::create_logger("TestLogger", &logfile, DEBUG);
173  ACTS_LOCAL_LOGGER(std::move(log));
174  ACTS_FATAL("fatal level");
175  ACTS_ERROR("error level");
176  ACTS_WARNING("warning level");
177  ACTS_INFO("info level");
178  ACTS_DEBUG("debug level");
179  ACTS_VERBOSE("verbose level");
180  logfile.close();
181 
182  std::vector<std::string> lines;
183  lines.push_back("TestLogger FATAL fatal level");
184  lines.push_back("TestLogger ERROR error level");
185  lines.push_back("TestLogger WARNING warning level");
186  lines.push_back("TestLogger INFO info level");
187  lines.push_back("TestLogger DEBUG debug level");
188 
189  std::ifstream infile("debug_log.txt", std::ios::in);
190  size_t i = 0;
191  for (std::string line; std::getline(infile, line); ++i) {
192  BOOST_CHECK_EQUAL(line, lines.at(i));
193  }
194 }
195 
202 BOOST_AUTO_TEST_CASE(VERBOSE_test) {
203  std::ofstream logfile("verbose_log.txt");
204 
205  auto log = detail::create_logger("TestLogger", &logfile, VERBOSE);
206  ACTS_LOCAL_LOGGER(std::move(log));
207  ACTS_FATAL("fatal level");
208  ACTS_ERROR("error level");
209  ACTS_WARNING("warning level");
210  ACTS_INFO("info level");
211  ACTS_DEBUG("debug level");
212  ACTS_VERBOSE("verbose level");
213  logfile.close();
214 
215  std::vector<std::string> lines;
216  lines.push_back("TestLogger FATAL fatal level");
217  lines.push_back("TestLogger ERROR error level");
218  lines.push_back("TestLogger WARNING warning level");
219  lines.push_back("TestLogger INFO info level");
220  lines.push_back("TestLogger DEBUG debug level");
221  lines.push_back("TestLogger VERBOSE verbose level");
222 
223  std::ifstream infile("verbose_log.txt", std::ios::in);
224  size_t i = 0;
225  for (std::string line; std::getline(infile, line); ++i) {
226  BOOST_CHECK_EQUAL(line, lines.at(i));
227  }
228 }
229 } // namespace Test
230 } // namespace Acts