From b4d6a1be9635a246e6f2c90d5dcd1cc698087843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20G=C3=BCndling?= Date: Wed, 22 Jan 2025 17:28:08 +0100 Subject: [PATCH] logging: add streamed test case, make log threshold global --- include/utl/logging.h | 10 ++-------- test/logging_test.cc | 32 ++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/include/utl/logging.h b/include/utl/logging.h index 888be98..dd7bec9 100644 --- a/include/utl/logging.h +++ b/include/utl/logging.h @@ -1,9 +1,5 @@ #pragma once -#ifdef LOGGING_HEADER -#include LOGGING_HEADER -#else - #include #include #include @@ -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; @@ -55,7 +51,7 @@ struct log { msg_{fmt::format(fmt_str, std::forward(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 @@ -126,5 +122,3 @@ log_error(const char* ctx, fmt::format_string, Args&&... args) -> log_error; } // namespace utl - -#endif // LOGGING_HEADER diff --git a/test/logging_test.cc b/test/logging_test.cc index 1aee74f..c0930de 100644 --- a/test/logging_test.cc +++ b/test/logging_test.cc @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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")); +}