diff --git a/2023/cmd/cmd.cpp b/2023/cmd/cmd.cpp index 0a60b85..1377128 100644 --- a/2023/cmd/cmd.cpp +++ b/2023/cmd/cmd.cpp @@ -13,8 +13,7 @@ namespace app { std::optional solve_day( - std::map>::value_type const& - solution, + std::map>::value_type const& solution, bool verbose); solution_vector select_all_days() { @@ -80,8 +79,8 @@ bool execute_days(app&, solution_vector const& days) { } xlog::info("DONE"); - xlog::info("Total time was {} ms", - std::chrono::duration_cast(total).count()); + xlog::info( + "Total time was {} ms", std::chrono::duration_cast(total).count()); return total_success; } @@ -90,9 +89,7 @@ std::int64_t microseconds(xmas::solution::duration d) { return std::chrono::duration_cast(d).count(); } -bool time_days(app&, - solution_vector const& days, - xmas::solution::duration timeout) { +bool time_days(app&, solution_vector const& days, xmas::solution::duration timeout) { xmas::solution::duration total{}; bool total_success = true; @@ -115,6 +112,10 @@ bool time_days(app&, // Used to compute standard deviation std::int64_t M = 0, S = 0; + // Disable warning and debug messages (They'll be spammed because + // the soultion is re-run many times) + xlog::logger::global().set_severity(xlog::ERROR); + while (std::chrono::high_resolution_clock::now() - begin < timeout) { const auto t = solve_day(*d, false); if (!t.has_value()) { @@ -132,6 +133,8 @@ bool time_days(app&, S += (us - prevM) * (us - M); } + xlog::logger::global().set_severity(xlog::DEBUG); + if (iter == 0) { xlog::warning("Could not successfully complete day {}", d->second->day()); continue; @@ -139,8 +142,7 @@ bool time_days(app&, // Average const auto mean = - std::chrono::duration_cast(daily_total / iter) - .count(); + std::chrono::duration_cast(daily_total / iter).count(); const auto dev = static_cast(std::sqrt(S / iter)); // Report @@ -149,26 +151,20 @@ bool time_days(app&, total += daily_total / iter; } - xlog::info(fmt, - "TOTAL", - "-", - std::chrono::duration_cast(total).count(), - "-"); + xlog::info( + fmt, "TOTAL", "-", std::chrono::duration_cast(total).count(), "-"); return total_success; } std::optional solve_day( - std::map>::value_type const& - solution, + std::map>::value_type const& solution, bool verbose) { try { - solution.second->set_input( - std::format("./data/{:02d}/input.txt", solution.second->day())); + solution.second->set_input(std::format("./data/{:02d}/input.txt", solution.second->day())); } catch (std::runtime_error& e) { - xlog::error( - "day {} could not load: {}\n", solution.second->day(), e.what()); + xlog::error("day {} could not load: {}\n", solution.second->day(), e.what()); return {}; } diff --git a/2023/xmaslib/log/log.cpp b/2023/xmaslib/log/log.cpp index b6c767f..d6b9762 100644 --- a/2023/xmaslib/log/log.cpp +++ b/2023/xmaslib/log/log.cpp @@ -3,11 +3,19 @@ #include namespace xlog { -namespace internal { -void log(severity prefix, std::string_view message) { - const auto p = [prefix]() -> std::string_view { - switch (prefix) { +logger& logger::global() { + static logger global{}; + return global; +} + +void logger::set_severity(severity s) { + this->min_severity = s; +} + +void logger::log_impl(severity sever, std::string_view message) { + const auto p = [sever]() -> std::string_view { + switch (sever) { case ERROR: return "\x1b[31mERROR \x1b[0m"; case WARNING: @@ -24,6 +32,4 @@ void log(severity prefix, std::string_view message) { std::cerr << std::format("{} {}\n", p, message) << std::flush; } -} // namespace internal - } // namespace xlog \ No newline at end of file diff --git a/2023/xmaslib/log/log.hpp b/2023/xmaslib/log/log.hpp index e5ff7d5..6cfa5a1 100644 --- a/2023/xmaslib/log/log.hpp +++ b/2023/xmaslib/log/log.hpp @@ -1,41 +1,79 @@ #pragma once #include +#include +#include namespace xlog { enum severity { - ERROR, - WARNING, - INFO, DEBUG, + INFO, + WARNING, + ERROR, }; -namespace internal { +struct logger { + severity min_severity = DEBUG; + + static logger& global(); + void set_severity(severity s); + + template + void log(severity severity, std::format_string msg, Args&&... args) { + if (severity < min_severity) { + return; + } + return log_impl(severity, std::format(msg, std::forward(args)...)); + } -void log(severity prefix, std::string_view message); -} // namespace internal + template + constexpr void error(std::format_string msg, Args&&... args) { + log(ERROR, msg, std::forward(args)...); + } + + template + constexpr void warning(std::format_string msg, Args&&... args) { + log(WARNING, msg, std::forward(args)...); + } + + template + constexpr void info(std::format_string msg, Args&&... args) { + log(INFO, msg, std::forward(args)...); + } + + template + constexpr void debug([[maybe_unused]] std::format_string msg, + [[maybe_unused]] Args&&... args) { +#ifndef NDEBUG + log(DEBUG, msg, std::forward(args)...); +#endif + } + +private: + void log_impl(severity prefix, std::string_view message); +}; template -constexpr void error(std::format_string msg, Args &&...args) { - internal::log(ERROR, std::format(msg, std::forward(args)...)); +constexpr void error(std::format_string msg, Args&&... args) { + logger::global().error(msg, std::forward(args)...); } template -constexpr void warning(std::format_string msg, Args &&...args) { - internal::log(WARNING, std::format(msg, std::forward(args)...)); +constexpr void warning(std::format_string msg, Args&&... args) { + logger::global().warning(msg, std::forward(args)...); } template -constexpr void info(std::format_string msg, Args &&...args) { - internal::log(INFO, std::format(msg, std::forward(args)...)); +constexpr void info(std::format_string msg, Args&&... args) { + logger::global().info(msg, std::forward(args)...); } template constexpr void debug([[maybe_unused]] std::format_string msg, - [[maybe_unused]] Args &&...args) { + [[maybe_unused]] Args&&... args) { #ifndef NDEBUG - internal::log(DEBUG, std::format(msg, std::forward(args)...)); + logger::global().debug(msg, std::forward(args)...); #endif }