Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:59

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Utilities/Logger.hpp"
0012 
0013 #include <cstddef>
0014 #include <fstream>
0015 #include <memory>
0016 #include <stdexcept>
0017 #include <string>
0018 #include <utility>
0019 #include <vector>
0020 
0021 using namespace Acts::Logging;
0022 
0023 namespace Acts::Test {
0024 
0025 /// @cond
0026 namespace detail {
0027 std::unique_ptr<const Logger> create_logger(const std::string& logger_name,
0028                                             std::ostream* logfile,
0029                                             Logging::Level lvl) {
0030   auto output = std::make_unique<LevelOutputDecorator>(
0031       std::make_unique<NamedOutputDecorator>(
0032           std::make_unique<DefaultPrintPolicy>(logfile), logger_name, 30));
0033   auto print = std::make_unique<DefaultFilterPolicy>(lvl);
0034   return std::make_unique<const Logger>(std::move(output), std::move(print));
0035 }
0036 
0037 }  // namespace detail
0038 /// @endcond
0039 
0040 /// @brief unit test for a certain debug level
0041 ///
0042 /// This test checks for the expected output when using the
0043 /// specified debug level as threshold. It also tests
0044 /// - #ACTS_LOCAL_LOGGER
0045 /// - Acts::getDefaultLogger
0046 void debug_level_test(const char* output_file, Logging::Level lvl) {
0047   // Logs will go to this file
0048   std::ofstream logfile(output_file);
0049 
0050   // If fail-on-error is enabled, then the logger will not, and should not,
0051   // tolerate being set up with a coarser debug level.
0052   if (lvl > Logging::getFailureThreshold()) {
0053     BOOST_CHECK_THROW(detail::create_logger("TestLogger", &logfile, lvl),
0054                       std::runtime_error);
0055     return;
0056   }
0057 
0058   auto test = [&](std::unique_ptr<const Logger> log, const std::string& name) {
0059     // Set up local logger
0060     ACTS_LOCAL_LOGGER(std::move(log));
0061 
0062     // Test logging at a certain debug level
0063     auto test_logging = [](auto&& test_operation, Logging::Level test_lvl) {
0064       if (test_lvl >= Logging::getFailureThreshold()) {
0065         BOOST_CHECK_THROW(test_operation(), std::runtime_error);
0066       } else {
0067         test_operation();
0068       }
0069     };
0070 
0071     // Test logging at all debug levels
0072     test_logging([&] { ACTS_FATAL("fatal level"); }, FATAL);
0073     test_logging([&] { ACTS_ERROR("error level"); }, ERROR);
0074     test_logging([&] { ACTS_WARNING("warning level"); }, WARNING);
0075     test_logging([&] { ACTS_INFO("info level"); }, INFO);
0076     test_logging([&] { ACTS_DEBUG("debug level"); }, DEBUG);
0077     test_logging([&] { ACTS_VERBOSE("verbose level"); }, VERBOSE);
0078     logfile.close();
0079 
0080     std::string padded_name = name;
0081     padded_name.resize(30, ' ');
0082 
0083     // Compute expected output for current debug levels
0084     std::vector<std::string> lines{padded_name + "FATAL     fatal level",
0085                                    padded_name + "ERROR     error level",
0086                                    padded_name + "WARNING   warning level",
0087                                    padded_name + "INFO      info level",
0088                                    padded_name + "DEBUG     debug level",
0089                                    padded_name + "VERBOSE   verbose level"};
0090     lines.resize(static_cast<int>(Logging::Level::MAX) - static_cast<int>(lvl));
0091 
0092     // Check output
0093     std::ifstream infile(output_file, std::ios::in);
0094     std::size_t i = 0;
0095     for (std::string line; std::getline(infile, line); ++i) {
0096       BOOST_CHECK_EQUAL(line, lines.at(i));
0097     }
0098   };
0099 
0100   auto log = detail::create_logger("TestLogger", &logfile, lvl);
0101   BOOST_CHECK_EQUAL(log->name(), "TestLogger");
0102   auto copy = log->clone("TestLoggerClone");
0103   test(std::move(copy), "TestLoggerClone");
0104   BOOST_CHECK_EQUAL(log->name(), "TestLogger");
0105 
0106   auto copy2 = log->clone("TestLoggerClone");
0107   BOOST_CHECK_EQUAL(copy2->level(), log->level());
0108 
0109   auto copy3 = log->cloneWithSuffix("Suffix");
0110   BOOST_CHECK_EQUAL(log->level(), copy3->level());
0111 
0112   logfile = std::ofstream{output_file};  // clear output
0113 
0114   test(std::move(log), "TestLogger");
0115 }
0116 
0117 /// @brief unit test for FATAL debug level
0118 BOOST_AUTO_TEST_CASE(FATAL_test) {
0119   debug_level_test("fatal_log.txt", FATAL);
0120 }
0121 
0122 /// @brief unit test for ERROR debug level
0123 BOOST_AUTO_TEST_CASE(ERROR_test) {
0124   debug_level_test("error_log.txt", ERROR);
0125 }
0126 
0127 /// @brief unit test for WARNING debug level
0128 BOOST_AUTO_TEST_CASE(WARNING_test) {
0129   debug_level_test("warning_log.txt", WARNING);
0130 }
0131 
0132 /// @brief unit test for INFO debug level
0133 BOOST_AUTO_TEST_CASE(INFO_test) {
0134   debug_level_test("info_log.txt", INFO);
0135 }
0136 
0137 /// @brief unit test for DEBUG debug level
0138 BOOST_AUTO_TEST_CASE(DEBUG_test) {
0139   debug_level_test("debug_log.txt", DEBUG);
0140 }
0141 
0142 /// @brief unit test for VERBOSE debug level
0143 BOOST_AUTO_TEST_CASE(VERBOSE_test) {
0144   debug_level_test("verbose_log.txt", VERBOSE);
0145 }
0146 }  // namespace Acts::Test