Skip to content

Commit

Permalink
logging: add streamed test case, make log threshold global
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Jan 22, 2025
1 parent 2d8b992 commit b4d6a1b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
10 changes: 2 additions & 8 deletions include/utl/logging.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#pragma once

#ifdef LOGGING_HEADER
#include LOGGING_HEADER
#else

#include <chrono>
#include <iomanip>
#include <iostream>
Expand All @@ -28,7 +24,7 @@ constexpr char const* to_str(log_level const level) {
return "";
}

static log_level s_verbosity;
extern log_level log_verbosity;

inline std::string now() {
using clock = std::chrono::system_clock;
Expand All @@ -55,7 +51,7 @@ struct log {
msg_{fmt::format(fmt_str, std::forward<Args>(args)...)} {}

~log() {
if (LogLevel >= utl::s_verbosity) {
if (LogLevel >= log_verbosity) {
#if defined(_WIN32)
auto const base_file_name = strrchr(loc_.file_name(), '\\')
? strrchr(loc_.file_name(), '\\') + 1
Expand Down Expand Up @@ -126,5 +122,3 @@ log_error(const char* ctx, fmt::format_string<Args...>,
Args&&... args) -> log_error<Args...>;

} // namespace utl

#endif // LOGGING_HEADER
32 changes: 26 additions & 6 deletions test/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TEST(log, can_send_info_msg) {
testing::internal::GetCapturedStderr(),
MatchesRegex(
".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] Message\n"));
};
}

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

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

TEST(log, can_format_extra_params) {
testing::internal::CaptureStderr();
Expand All @@ -41,7 +41,7 @@ TEST(log, can_format_extra_params) {
EXPECT_THAT(testing::internal::GetCapturedStderr(),
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] "
"String=Hello Int=42\n"));
};
}

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

TEST(log, accept_string_view_as_extra_param_inline) {
testing::internal::CaptureStderr();
Expand All @@ -59,7 +59,7 @@ TEST(log, accept_string_view_as_extra_param_inline) {
EXPECT_THAT(testing::internal::GetCapturedStderr(),
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] "
"Hello world!\n"));
};
}

TEST(log, can_have_optional_attrs) {
testing::internal::CaptureStderr();
Expand All @@ -68,4 +68,24 @@ TEST(log, can_have_optional_attrs) {
testing::internal::GetCapturedStderr(),
MatchesRegex(
".+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";}
int x_;
};
TEST(log, temporary_streamed) {
testing::internal::CaptureStderr();
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"));
}

0 comments on commit b4d6a1b

Please sign in to comment.