Skip to content

Commit

Permalink
Adding a log_ prefix to the log() sub-structs (motis-project#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C authored Jan 17, 2025
1 parent 69e700e commit 8593fd2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
16 changes: 8 additions & 8 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -50,23 +50,23 @@ 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:
:::
:::{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{...}`.
:::
18 changes: 9 additions & 9 deletions include/utl/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ struct log {

/// Produce a new DEBUG log line
template <typename... Args>
struct debug : public log<log_level::debug, Args...> {
struct log_debug : public log<log_level::debug, Args...> {
using log<log_level::debug, Args...>::log;
};

/// Produce a new INFO log line
template <typename... Args>
struct info : public log<log_level::info, Args...> {
struct log_info : public log<log_level::info, Args...> {
using log<log_level::info, Args...>::log;
};

/// Produce a new ERROR log line
template <typename... Args>
struct error : public log<log_level::error, Args...> {
struct log_error : public log<log_level::error, Args...> {
using log<log_level::error, Args...>::log;
};

Expand All @@ -115,16 +115,16 @@ struct error : public log<log_level::error, Args...> {
// which has a default value:

template <typename... Args>
debug(const char* ctx, fmt::format_string<Args...>,
Args&&... args) -> debug<Args...>;
log_debug(const char* ctx, fmt::format_string<Args...>,
Args&&... args) -> log_debug<Args...>;

template <typename... Args>
info(const char* ctx, fmt::format_string<Args...>,
Args&&... args) -> info<Args...>;
log_info(const char* ctx, fmt::format_string<Args...>,
Args&&... args) -> log_info<Args...>;

template <typename... Args>
error(const char* ctx, fmt::format_string<Args...>,
Args&&... args) -> error<Args...>;
log_error(const char* ctx, fmt::format_string<Args...>,
Args&&... args) -> log_error<Args...>;

} // namespace utl

Expand Down
2 changes: 1 addition & 1 deletion include/utl/parallel_for.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
Expand Down
12 changes: 6 additions & 6 deletions src/timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -15,7 +15,7 @@ scoped_timer::~scoped_timer() {
double t =
static_cast<double>(duration_cast<microseconds>(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 {
Expand All @@ -24,12 +24,12 @@ void scoped_timer::print(std::string_view const message) const {
double const t =
static_cast<double>(duration_cast<microseconds>(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 {
Expand All @@ -38,7 +38,7 @@ void manual_timer::stop_and_print() const {
double t =
static_cast<double>(duration_cast<microseconds>(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 {
Expand All @@ -47,7 +47,7 @@ void manual_timer::print(std::string_view const message) const {
double const t =
static_cast<double>(duration_cast<microseconds>(stop - start_).count()) /
1000.0;
utl::info("scoped_timer", "[{}] {} ({}ms)", name_, message, t);
utl::log_info("scoped_timer", "[{}] {} ({}ms)", name_, message, t);
}

} // namespace utl
10 changes: 5 additions & 5 deletions test/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -35,15 +35,15 @@ 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"));
};

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(
Expand Down

0 comments on commit 8593fd2

Please sign in to comment.