File indexing completed on 2025-01-18 09:12:59
0001
0002
0003
0004
0005
0006
0007
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
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 }
0038
0039
0040
0041
0042
0043
0044
0045
0046 void debug_level_test(const char* output_file, Logging::Level lvl) {
0047
0048 std::ofstream logfile(output_file);
0049
0050
0051
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
0060 ACTS_LOCAL_LOGGER(std::move(log));
0061
0062
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
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
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
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};
0113
0114 test(std::move(log), "TestLogger");
0115 }
0116
0117
0118 BOOST_AUTO_TEST_CASE(FATAL_test) {
0119 debug_level_test("fatal_log.txt", FATAL);
0120 }
0121
0122
0123 BOOST_AUTO_TEST_CASE(ERROR_test) {
0124 debug_level_test("error_log.txt", ERROR);
0125 }
0126
0127
0128 BOOST_AUTO_TEST_CASE(WARNING_test) {
0129 debug_level_test("warning_log.txt", WARNING);
0130 }
0131
0132
0133 BOOST_AUTO_TEST_CASE(INFO_test) {
0134 debug_level_test("info_log.txt", INFO);
0135 }
0136
0137
0138 BOOST_AUTO_TEST_CASE(DEBUG_test) {
0139 debug_level_test("debug_log.txt", DEBUG);
0140 }
0141
0142
0143 BOOST_AUTO_TEST_CASE(VERBOSE_test) {
0144 debug_level_test("verbose_log.txt", VERBOSE);
0145 }
0146 }