Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-16 08:04:30

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