ECCE @ 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 #include <fstream>
13 #include <string>
14 
16 
17 namespace Acts {
18 namespace Test {
19 
20 using namespace Acts::Logging;
21 
23 namespace detail {
24 std::unique_ptr<const Logger> create_logger(const std::string& logger_name,
25  std::ostream* logfile,
26  Logging::Level lvl) {
27  auto output = std::make_unique<LevelOutputDecorator>(
28  std::make_unique<NamedOutputDecorator>(
29  std::make_unique<DefaultPrintPolicy>(logfile), logger_name));
30  auto print = std::make_unique<DefaultFilterPolicy>(lvl);
31  return std::make_unique<const Logger>(std::move(output), std::move(print));
32 }
33 
34 std::string failure_msg(const std::string& expected, const std::string& found) {
35  return std::string("'") + expected + "' != '" + found + "'";
36 }
37 } // namespace detail
39 
46 BOOST_AUTO_TEST_CASE(FATAL_test) {
47  std::ofstream logfile("fatal_log.txt");
48 
49  auto log = detail::create_logger("TestLogger", &logfile, FATAL);
50  ACTS_LOCAL_LOGGER(std::move(log));
51  ACTS_FATAL("fatal level");
52  ACTS_ERROR("error level");
53  ACTS_WARNING("warning level");
54  ACTS_INFO("info level");
55  ACTS_DEBUG("debug level");
56  ACTS_VERBOSE("verbose level");
57  logfile.close();
58 
59  std::vector<std::string> lines;
60  lines.push_back("TestLogger FATAL fatal level");
61 
62  std::ifstream infile("fatal_log.txt", std::ios::in);
63  size_t i = 0;
64  for (std::string line; std::getline(infile, line); ++i) {
65  BOOST_TEST(line == lines.at(i), detail::failure_msg(line, lines.at(i)));
66  }
67 }
68 
75 BOOST_AUTO_TEST_CASE(ERROR_test) {
76  std::ofstream logfile("error_log.txt");
77 
78  auto log = detail::create_logger("TestLogger", &logfile, ERROR);
79  ACTS_LOCAL_LOGGER(std::move(log));
80  ACTS_FATAL("fatal level");
81  ACTS_ERROR("error level");
82  ACTS_WARNING("warning level");
83  ACTS_INFO("info level");
84  ACTS_DEBUG("debug level");
85  ACTS_VERBOSE("verbose level");
86  logfile.close();
87 
88  std::vector<std::string> lines;
89  lines.push_back("TestLogger FATAL fatal level");
90  lines.push_back("TestLogger ERROR error level");
91 
92  std::ifstream infile("error_log.txt", std::ios::in);
93  size_t i = 0;
94  for (std::string line; std::getline(infile, line); ++i) {
95  BOOST_TEST(line == lines.at(i), detail::failure_msg(line, lines.at(i)));
96  }
97 }
98 
105 BOOST_AUTO_TEST_CASE(WARNING_test) {
106  std::ofstream logfile("warning_log.txt");
107 
108  auto log = detail::create_logger("TestLogger", &logfile, WARNING);
109  ACTS_LOCAL_LOGGER(std::move(log));
110  ACTS_FATAL("fatal level");
111  ACTS_ERROR("error level");
112  ACTS_WARNING("warning level");
113  ACTS_INFO("info level");
114  ACTS_DEBUG("debug level");
115  ACTS_VERBOSE("verbose level");
116  logfile.close();
117 
118  std::vector<std::string> lines;
119  lines.push_back("TestLogger FATAL fatal level");
120  lines.push_back("TestLogger ERROR error level");
121  lines.push_back("TestLogger WARNING warning level");
122 
123  std::ifstream infile("warning_log.txt", std::ios::in);
124  size_t i = 0;
125  for (std::string line; std::getline(infile, line); ++i) {
126  BOOST_TEST(line == lines.at(i), detail::failure_msg(line, lines.at(i)));
127  }
128 }
129 
137  std::ofstream logfile("info_log.txt");
138 
139  auto log = detail::create_logger("TestLogger", &logfile, INFO);
140  ACTS_LOCAL_LOGGER(std::move(log));
141  ACTS_FATAL("fatal level");
142  ACTS_ERROR("error level");
143  ACTS_WARNING("warning level");
144  ACTS_INFO("info level");
145  ACTS_DEBUG("debug level");
146  ACTS_VERBOSE("verbose level");
147  logfile.close();
148 
149  std::vector<std::string> lines;
150  lines.push_back("TestLogger FATAL fatal level");
151  lines.push_back("TestLogger ERROR error level");
152  lines.push_back("TestLogger WARNING warning level");
153  lines.push_back("TestLogger INFO info level");
154 
155  std::ifstream infile("info_log.txt", std::ios::in);
156  size_t i = 0;
157  for (std::string line; std::getline(infile, line); ++i) {
158  BOOST_TEST(line == lines.at(i), detail::failure_msg(line, lines.at(i)));
159  }
160 }
161 
168 BOOST_AUTO_TEST_CASE(DEBUG_test) {
169  std::ofstream logfile("debug_log.txt");
170 
171  auto log = detail::create_logger("TestLogger", &logfile, DEBUG);
172  ACTS_LOCAL_LOGGER(std::move(log));
173  ACTS_FATAL("fatal level");
174  ACTS_ERROR("error level");
175  ACTS_WARNING("warning level");
176  ACTS_INFO("info level");
177  ACTS_DEBUG("debug level");
178  ACTS_VERBOSE("verbose level");
179  logfile.close();
180 
181  std::vector<std::string> lines;
182  lines.push_back("TestLogger FATAL fatal level");
183  lines.push_back("TestLogger ERROR error level");
184  lines.push_back("TestLogger WARNING warning level");
185  lines.push_back("TestLogger INFO info level");
186  lines.push_back("TestLogger DEBUG debug level");
187 
188  std::ifstream infile("debug_log.txt", std::ios::in);
189  size_t i = 0;
190  for (std::string line; std::getline(infile, line); ++i) {
191  BOOST_TEST(line == lines.at(i), detail::failure_msg(line, lines.at(i)));
192  }
193 }
194 
201 BOOST_AUTO_TEST_CASE(VERBOSE_test) {
202  std::ofstream logfile("verbose_log.txt");
203 
204  auto log = detail::create_logger("TestLogger", &logfile, VERBOSE);
205  ACTS_LOCAL_LOGGER(std::move(log));
206  ACTS_FATAL("fatal level");
207  ACTS_ERROR("error level");
208  ACTS_WARNING("warning level");
209  ACTS_INFO("info level");
210  ACTS_DEBUG("debug level");
211  ACTS_VERBOSE("verbose level");
212  logfile.close();
213 
214  std::vector<std::string> lines;
215  lines.push_back("TestLogger FATAL fatal level");
216  lines.push_back("TestLogger ERROR error level");
217  lines.push_back("TestLogger WARNING warning level");
218  lines.push_back("TestLogger INFO info level");
219  lines.push_back("TestLogger DEBUG debug level");
220  lines.push_back("TestLogger VERBOSE verbose level");
221 
222  std::ifstream infile("verbose_log.txt", std::ios::in);
223  size_t i = 0;
224  for (std::string line; std::getline(infile, line); ++i) {
225  BOOST_TEST(line == lines.at(i), detail::failure_msg(line, lines.at(i)));
226  }
227 }
228 } // namespace Test
229 } // namespace Acts