From 6101df4b79460de24d63df1ebcab29b9f325368f Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Fri, 17 Jan 2025 16:04:08 +0100 Subject: [PATCH] Adding a log_ prefix to the log() sub-structs --- docs/index.md | 16 ++++++++-------- include/utl/logging.h | 18 +++++++++--------- include/utl/parallel_for.h | 2 +- src/timer.cc | 12 ++++++------ test/logging_test.cc | 10 +++++----- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/docs/index.md b/docs/index.md index 6d5885b..9216e04 100644 --- a/docs/index.md +++ b/docs/index.md @@ -12,11 +12,11 @@ This is the documentation for the **utl** (utility) module. ## Logging The simplest way to produce log lines is to use the `utl:log()` function, or the wrapping functions for the various log levels, -`utl::debug()`, `utl::info()` and `utl::error()`: +`utl::log_debug()`, `utl::log_info()` and `utl::log_error()`: ```c++ #include "utl/logging.h" -utl::info("MyCtx", "Simple message"); +utl::log_info("MyCtx", "Simple message"); ``` The first parameter is the **context**, that provides an information of the origin of the log line inside MOTIS code. @@ -36,12 +36,12 @@ error You can insert variables in the message by using `{}` and passing them as extra arguments (formatting is performed by the [fmt](https://fmt.dev>) library): ```c++ -utl::info("MyCtx", "String={} Int={}", "Hello", 42); +utl::log_info("MyCtx", "String={} Int={}", "Hello", 42); ``` You can specify **metadata** using `.attrs()`: ```c++ -utl::info("MyCtx", "Message").attrs({{"key1", "value1"}, {"key2", "value2"}}); +utl::log_info("MyCtx", "Message").attrs({{"key1", "value1"}, {"key2", "value2"}}); ``` ### API details @@ -50,17 +50,17 @@ utl::info("MyCtx", "Message").attrs({{"key1", "value1"}, {"key2", "value2"}}); :members: ::: -:::{doxygenstruct} utl::debug +:::{doxygenstruct} utl::log_debug :no-link: :members: ::: -:::{doxygenstruct} utl::info +:::{doxygenstruct} utl::log_info :no-link: :members: ::: -:::{doxygenstruct} utl::error +:::{doxygenstruct} utl::log_error :no-link: :members: ::: @@ -68,5 +68,5 @@ utl::info("MyCtx", "Message").attrs({{"key1", "value1"}, {"key2", "value2"}}); :::{note} Those logging function are an exception to the rule that, in MOTIS, we use [Aggregate Initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization) wherever possible, -but here we do not want to use `utl::info{...}`. +but here we do not want to use `utl::log_info{...}`. ::: diff --git a/include/utl/logging.h b/include/utl/logging.h index 165a3d8..9daac64 100644 --- a/include/utl/logging.h +++ b/include/utl/logging.h @@ -94,19 +94,19 @@ struct log { /// Produce a new DEBUG log line template -struct debug : public log { +struct log_debug : public log { using log::log; }; /// Produce a new INFO log line template -struct info : public log { +struct log_info : public log { using log::log; }; /// Produce a new ERROR log line template -struct error : public log { +struct log_error : public log { using log::log; }; @@ -115,16 +115,16 @@ struct error : public log { // which has a default value: template -debug(const char* ctx, fmt::format_string, - Args&&... args) -> debug; +log_debug(const char* ctx, fmt::format_string, + Args&&... args) -> log_debug; template -info(const char* ctx, fmt::format_string, - Args&&... args) -> info; +log_info(const char* ctx, fmt::format_string, + Args&&... args) -> log_info; template -error(const char* ctx, fmt::format_string, - Args&&... args) -> error; +log_error(const char* ctx, fmt::format_string, + Args&&... args) -> log_error; } // namespace utl diff --git a/include/utl/parallel_for.h b/include/utl/parallel_for.h index afcc644..9b3cd84 100644 --- a/include/utl/parallel_for.h +++ b/include/utl/parallel_for.h @@ -160,7 +160,7 @@ inline errors_t parallel_for( jobs.size(), [&](auto const idx) { if (idx % mod == 0) { - utl::info("parallel_for", "{} {}/{}", desc, idx, jobs.size()); + utl::log_info("parallel_for", "{} {}/{}", desc, idx, jobs.size()); } func(jobs[idx]); }, diff --git a/src/timer.cc b/src/timer.cc index e015de5..8c81596 100644 --- a/src/timer.cc +++ b/src/timer.cc @@ -6,7 +6,7 @@ namespace utl { scoped_timer::scoped_timer(std::string name) : name_{std::move(name)}, start_{std::chrono::steady_clock::now()} { - utl::info("scoped_timer", "[{}] starting", name); + utl::log_info("scoped_timer", "[{}] starting", name); } scoped_timer::~scoped_timer() { @@ -15,7 +15,7 @@ scoped_timer::~scoped_timer() { double t = static_cast(duration_cast(stop - start_).count()) / 1000.0; - utl::info("scoped_timer", "[{}] finished ({}ms)", name_, t); + utl::log_info("scoped_timer", "[{}] finished ({}ms)", name_, t); } void scoped_timer::print(std::string_view const message) const { @@ -24,12 +24,12 @@ void scoped_timer::print(std::string_view const message) const { double const t = static_cast(duration_cast(stop - start_).count()) / 1000.0; - utl::info("scoped_timer", "[{}] {} ({}ms)", name_, message, t); + utl::log_info("scoped_timer", "[{}] {} ({}ms)", name_, message, t); } manual_timer::manual_timer(std::string name) : name_{std::move(name)}, start_{std::chrono::steady_clock::now()} { - utl::info("scoped_timer", "[{}] starting", name_); + utl::log_info("scoped_timer", "[{}] starting", name_); } void manual_timer::stop_and_print() const { @@ -38,7 +38,7 @@ void manual_timer::stop_and_print() const { double t = static_cast(duration_cast(stop - start_).count()) / 1000.0; - utl::info("scoped_timer", "[{}] finished ({}ms)", name_, t); + utl::log_info("scoped_timer", "[{}] finished ({}ms)", name_, t); } void manual_timer::print(std::string_view const message) const { @@ -47,7 +47,7 @@ void manual_timer::print(std::string_view const message) const { double const t = static_cast(duration_cast(stop - start_).count()) / 1000.0; - utl::info("scoped_timer", "[{}] {} ({}ms)", name_, message, t); + utl::log_info("scoped_timer", "[{}] {} ({}ms)", name_, message, t); } } // namespace utl \ No newline at end of file diff --git a/test/logging_test.cc b/test/logging_test.cc index 34741bf..e884951 100644 --- a/test/logging_test.cc +++ b/test/logging_test.cc @@ -7,7 +7,7 @@ using ::testing::MatchesRegex; TEST(log, can_send_info_msg) { testing::internal::CaptureStderr(); - utl::info("MyCtx", "Message"); + utl::log_info("MyCtx", "Message"); EXPECT_THAT( testing::internal::GetCapturedStderr(), MatchesRegex( @@ -16,7 +16,7 @@ TEST(log, can_send_info_msg) { TEST(log, can_send_debug_msg) { testing::internal::CaptureStderr(); - utl::debug("MyCtx", "Message"); + utl::log_debug("MyCtx", "Message"); EXPECT_THAT( testing::internal::GetCapturedStderr(), MatchesRegex( @@ -25,7 +25,7 @@ TEST(log, can_send_debug_msg) { TEST(log, can_send_error_msg) { testing::internal::CaptureStderr(); - utl::error("MyCtx", "Message"); + utl::log_error("MyCtx", "Message"); EXPECT_THAT( testing::internal::GetCapturedStderr(), MatchesRegex( @@ -35,7 +35,7 @@ TEST(log, can_send_error_msg) { TEST(log, can_format_extra_params) { testing::internal::CaptureStderr(); auto const value = 42; - utl::info("MyCtx", "String={} Int={}", "Hello", value); + utl::log_info("MyCtx", "String={} Int={}", "Hello", value); EXPECT_THAT(testing::internal::GetCapturedStderr(), MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] " "String=Hello Int=42\n")); @@ -43,7 +43,7 @@ TEST(log, can_format_extra_params) { TEST(log, can_have_optional_attrs) { testing::internal::CaptureStderr(); - utl::info("MyCtx", "Message").attrs({{"key", "value"}}); + utl::log_info("MyCtx", "Message").attrs({{"key", "value"}}); EXPECT_THAT( testing::internal::GetCapturedStderr(), MatchesRegex(