Skip to content

Commit

Permalink
Log: removing 'fn' from log line format (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C authored Jan 23, 2025
1 parent 07fb33d commit d858ae8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
11 changes: 5 additions & 6 deletions include/utl/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ struct log {
? strrchr(loc_.file_name(), '/') + 1
: loc_.file_name();
#endif
fmt::print(
std::clog, "{time} [{level}] [{file}:{line} {fn}] [{ctx}] {msg}\n",
fmt::arg("time", now()), fmt::arg("level", to_str(LogLevel)),
fmt::arg("file", base_file_name), fmt::arg("line", loc_.line()),
fmt::arg("fn", loc_.function_name()), fmt::arg("ctx", ctx_),
fmt::arg("msg", msg_));
fmt::print(std::clog, "{time} [{level}] [{file}:{line}] [{ctx}] {msg}\n",
fmt::arg("time", now()), fmt::arg("level", to_str(LogLevel)),
fmt::arg("file", base_file_name),
fmt::arg("line", loc_.line()), fmt::arg("ctx", ctx_),
fmt::arg("msg", msg_));
}
}

Expand Down
28 changes: 12 additions & 16 deletions test/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TEST(log, can_send_info_msg) {
EXPECT_THAT(
testing::internal::GetCapturedStderr(),
MatchesRegex(
".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] Message\n"));
".+T.+Z \\[info\\] \\[logging.+:..\\] \\[MyCtx\\] Message\n"));
}

TEST(log, can_send_debug_msg) {
Expand All @@ -22,7 +22,7 @@ TEST(log, can_send_debug_msg) {
EXPECT_THAT(
testing::internal::GetCapturedStderr(),
MatchesRegex(
".+T.+Z \\[debug\\] \\[logging.+:.+\\] \\[MyCtx\\] Message\n"));
".+T.+Z \\[debug\\] \\[logging.+:..\\] \\[MyCtx\\] Message\n"));
}

TEST(log, can_send_error_msg) {
Expand All @@ -31,15 +31,15 @@ TEST(log, can_send_error_msg) {
EXPECT_THAT(
testing::internal::GetCapturedStderr(),
MatchesRegex(
".+T.+Z \\[error\\] \\[logging.+:.+\\] \\[MyCtx\\] Message\n"));
".+T.+Z \\[error\\] \\[logging.+:..\\] \\[MyCtx\\] Message\n"));
}

TEST(log, can_format_extra_params) {
testing::internal::CaptureStderr();
auto const value = 42;
utl::log_info("MyCtx", "String={} Int={}", "Hello", value);
EXPECT_THAT(testing::internal::GetCapturedStderr(),
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] "
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:..\\] \\[MyCtx\\] "
"String=Hello Int=42\n"));
}

Expand All @@ -48,7 +48,7 @@ TEST(log, accept_string_view_as_extra_param) {
std::string_view str{"world"};
utl::log_info("MyCtx", "Hello {}!", str);
EXPECT_THAT(testing::internal::GetCapturedStderr(),
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] "
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:..\\] \\[MyCtx\\] "
"Hello world!\n"));
}

Expand All @@ -57,7 +57,7 @@ TEST(log, accept_string_view_as_extra_param_inline) {
std::string str{"world"};
utl::log_info("MyCtx", "Hello {}!", std::string_view{str});
EXPECT_THAT(testing::internal::GetCapturedStderr(),
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] "
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:..\\] \\[MyCtx\\] "
"Hello world!\n"));
}

Expand All @@ -67,25 +67,21 @@ TEST(log, can_have_optional_attrs) {
EXPECT_THAT(
testing::internal::GetCapturedStderr(),
MatchesRegex(
".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] Message\n"));
".+T.+Z \\[info\\] \\[logging.+:..\\] \\[MyCtx\\] Message\n"));
}


struct dtor {
friend std::ostream& operator<<(std::ostream& out, dtor const& x) {
return out << x.x_;
}
~dtor() { std::clog << "DESTROY\n";}
~dtor() { std::clog << "DESTROY\n"; }
int x_;
};
TEST(log, temporary_streamed) {
testing::internal::CaptureStderr();
auto const tmp = []() {
return dtor{.x_ = 9999};
};
auto const tmp = []() { return dtor{.x_ = 9999}; };
utl::log_info("MyCtx", "{} {}", fmt::streamed(tmp()), fmt::streamed(tmp()));
EXPECT_THAT(
testing::internal::GetCapturedStderr(),
MatchesRegex(
".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] 9999 9999\nDESTROY\nDESTROY\n"));
EXPECT_THAT(testing::internal::GetCapturedStderr(),
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:..\\] \\[MyCtx\\] "
"9999 9999\nDESTROY\nDESTROY\n"));
}

0 comments on commit d858ae8

Please sign in to comment.