Analysis Software
Documentation for sPHENIX simulation 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 <cstddef>
16 #include <fstream>
17 #include <memory>
18 #include <stdexcept>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 namespace Acts {
24 namespace Test {
25 
26 using namespace Acts::Logging;
27 
29 namespace detail {
30 std::unique_ptr<const Logger> create_logger(const std::string& logger_name,
31  std::ostream* logfile,
32  Logging::Level lvl) {
33  auto output = std::make_unique<LevelOutputDecorator>(
34  std::make_unique<NamedOutputDecorator>(
35  std::make_unique<DefaultPrintPolicy>(logfile), logger_name, 30));
36  auto print = std::make_unique<DefaultFilterPolicy>(lvl);
37  return std::make_unique<const Logger>(std::move(output), std::move(print));
38 }
39 
40 std::string failure_msg(const std::string& expected, const std::string& found) {
41  return std::string("'") + expected + "' != '" + found + "'";
42 }
43 } // namespace detail
45 
53  // Logs will go to this file
54  std::ofstream logfile(output_file);
55 
56  // If fail-on-error is enabled, then the logger will not, and should not,
57  // tolerate being set up with a coarser debug level.
58  if (lvl > Logging::getFailureThreshold()) {
59  BOOST_CHECK_THROW(detail::create_logger("TestLogger", &logfile, lvl),
60  std::runtime_error);
61  return;
62  }
63 
64  auto test = [&](std::unique_ptr<const Logger> log, const std::string& name) {
65  // Set up local logger
67 
68  // Test logging at a certain debug level
69  auto test_logging = [](auto&& test_operation, Logging::Level test_lvl) {
70  if (test_lvl >= Logging::getFailureThreshold()) {
71  BOOST_CHECK_THROW(test_operation(), std::runtime_error);
72  } else {
73  test_operation();
74  }
75  };
76 
77  // Test logging at all debug levels
78  test_logging([&] { ACTS_FATAL("fatal level"); }, FATAL);
79  test_logging([&] { ACTS_ERROR("error level"); }, ERROR);
80  test_logging([&] { ACTS_WARNING("warning level"); }, WARNING);
81  test_logging([&] { ACTS_INFO("info level"); }, INFO);
82  test_logging([&] { ACTS_DEBUG("debug level"); }, DEBUG);
83  test_logging([&] { ACTS_VERBOSE("verbose level"); }, VERBOSE);
84  logfile.close();
85 
86  std::string padded_name = name;
87  padded_name.resize(30, ' ');
88 
89  // Compute expected output for current debug levels
90  std::vector<std::string> lines{padded_name + "FATAL fatal level",
91  padded_name + "ERROR error level",
92  padded_name + "WARNING warning level",
93  padded_name + "INFO info level",
94  padded_name + "DEBUG debug level",
95  padded_name + "VERBOSE verbose level"};
96  lines.resize(static_cast<int>(Logging::Level::MAX) - static_cast<int>(lvl));
97 
98  // Check output
99  std::ifstream infile(output_file, std::ios::in);
100  size_t i = 0;
101  for (std::string line; std::getline(infile, line); ++i) {
102  BOOST_CHECK_EQUAL(line, lines.at(i));
103  }
104  };
105 
106  auto log = detail::create_logger("TestLogger", &logfile, lvl);
107  BOOST_CHECK_EQUAL(log->name(), "TestLogger");
108  auto copy = log->clone("TestLoggerClone");
109  test(std::move(copy), "TestLoggerClone");
110  BOOST_CHECK_EQUAL(log->name(), "TestLogger");
111 
112  auto copy2 = log->clone("TestLoggerClone");
113  BOOST_CHECK_EQUAL(copy2->level(), log->level());
114 
115  auto copy3 = log->cloneWithSuffix("Suffix");
116  BOOST_CHECK_EQUAL(log->level(), copy3->level());
117 
118  logfile = std::ofstream{output_file}; // clear output
119 
120  test(std::move(log), "TestLogger");
121 }
122 
124 BOOST_AUTO_TEST_CASE(FATAL_test) {
125  debug_level_test("fatal_log.txt", FATAL);
126 }
127 
129 BOOST_AUTO_TEST_CASE(ERROR_test) {
130  debug_level_test("error_log.txt", ERROR);
131 }
132 
134 BOOST_AUTO_TEST_CASE(WARNING_test) {
135  debug_level_test("warning_log.txt", WARNING);
136 }
137 
140  debug_level_test("info_log.txt", INFO);
141 }
142 
144 BOOST_AUTO_TEST_CASE(DEBUG_test) {
145  debug_level_test("debug_log.txt", DEBUG);
146 }
147 
149 BOOST_AUTO_TEST_CASE(VERBOSE_test) {
150  debug_level_test("verbose_log.txt", VERBOSE);
151 }
152 } // namespace Test
153 } // namespace Acts