From 9e6f5b6b2d3e317b6b3a0365293299c7d6950855 Mon Sep 17 00:00:00 2001 From: Ron Rechenmacher Date: Sat, 18 Apr 2020 08:58:11 -0500 Subject: [PATCH 1/2] add single logger method and log_msg constructor and tests/test_time_point.cpp --- include/spdlog/details/log_msg-inl.h | 11 ++++++--- include/spdlog/details/log_msg.h | 1 + include/spdlog/logger.h | 13 +++++++++++ tests/CMakeLists.txt | 3 ++- tests/test_time_point.cpp | 35 ++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tests/test_time_point.cpp diff --git a/include/spdlog/details/log_msg-inl.h b/include/spdlog/details/log_msg-inl.h index 0a0aed81..3d037d97 100644 --- a/include/spdlog/details/log_msg-inl.h +++ b/include/spdlog/details/log_msg-inl.h @@ -13,10 +13,10 @@ namespace spdlog { namespace details { SPDLOG_INLINE log_msg::log_msg( - spdlog::source_loc loc, string_view_t a_logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) + spdlog::log_clock::time_point log_time, spdlog::source_loc loc, string_view_t a_logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) : logger_name(a_logger_name) , level(lvl) - , time(os::now()) + , time(log_time) #ifndef SPDLOG_NO_THREAD_ID , thread_id(os::thread_id()) #endif @@ -24,8 +24,13 @@ SPDLOG_INLINE log_msg::log_msg( , payload(msg) {} +SPDLOG_INLINE log_msg::log_msg( + spdlog::source_loc loc, string_view_t a_logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) + : log_msg(os::now(), loc, a_logger_name, lvl, msg) +{} + SPDLOG_INLINE log_msg::log_msg(string_view_t a_logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) - : log_msg(source_loc{}, a_logger_name, lvl, msg) + : log_msg(os::now(), source_loc{}, a_logger_name, lvl, msg) {} } // namespace details diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index 03bdbaec..834ca4df 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -11,6 +11,7 @@ namespace details { struct SPDLOG_API log_msg { log_msg() = default; + log_msg(log_clock::time_point log_time, source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg); log_msg(source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg); log_msg(string_view_t logger_name, level::level_enum lvl, string_view_t msg); log_msg(const log_msg &other) = default; diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index aeb38564..4dba427a 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -147,6 +147,19 @@ public: log(loc, lvl, string_view_t{msg}); } + void log(log_clock::time_point &log_time, source_loc loc, level::level_enum lvl, string_view_t msg) + { + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) + { + return; + } + + details::log_msg log_msg(log_time, loc, name_, lvl, msg); + log_it_(log_msg, log_enabled, traceback_enabled); + } + void log(source_loc loc, level::level_enum lvl, string_view_t msg) { bool log_enabled = should_log(lvl); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7f20d6a9..df6604f5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -32,7 +32,8 @@ set(SPDLOG_UTESTS_SOURCES test_stdout_api.cpp test_backtrace.cpp test_create_dir.cpp - test_cfg.cpp) + test_cfg.cpp + test_time_point.cpp) if(NOT SPDLOG_NO_EXCEPTIONS) list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp) diff --git a/tests/test_time_point.cpp b/tests/test_time_point.cpp new file mode 100644 index 00000000..02e0116c --- /dev/null +++ b/tests/test_time_point.cpp @@ -0,0 +1,35 @@ +#include "includes.h" +#include "test_sink.h" +#include "spdlog/async.h" + +TEST_CASE("time_point1", "[time_point log_msg]") +{ + std::shared_ptr test_sink(new spdlog::sinks::test_sink_st); + spdlog::logger logger("test-time_point", test_sink); + + spdlog::source_loc source{}; + std::chrono::system_clock::time_point tp{std::chrono::system_clock::now()}; + test_sink->set_pattern("%T.%F"); // interested in the time_point + + // all the following should have the same time + test_sink->set_delay(std::chrono::milliseconds(10)); + for (int i = 0; i < 5; i++) { + spdlog::details::log_msg msg{tp,source,"test_logger", spdlog::level::info, "message"}; + test_sink->log(msg); + } + + logger.log(tp,source,spdlog::level::info,"formatted message"); + logger.log(tp,source,spdlog::level::info,"formatted message"); + logger.log(tp,source,spdlog::level::info,"formatted message"); + logger.log(tp,source,spdlog::level::info,"formatted message"); + logger.log(source,spdlog::level::info,"formatted message"); // last line has different time_point + + // now the real test... that the times are the same. + std::vector lines = test_sink->lines(); + REQUIRE(lines[0] == lines[1]); + REQUIRE(lines[2] == lines[3]); + REQUIRE(lines[4] == lines[5]); + REQUIRE(lines[6] == lines[7]); + REQUIRE(lines[8] != lines[9]); + spdlog::drop_all(); +} From faaef7686d38c651510a4ef8307e94af33a80b04 Mon Sep 17 00:00:00 2001 From: Ron Rechenmacher Date: Sat, 18 Apr 2020 22:44:13 -0500 Subject: [PATCH 2/2] pass log_time param by value --- include/spdlog/logger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 4dba427a..67f808ba 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -147,7 +147,7 @@ public: log(loc, lvl, string_view_t{msg}); } - void log(log_clock::time_point &log_time, source_loc loc, level::level_enum lvl, string_view_t msg) + void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, string_view_t msg) { bool log_enabled = should_log(lvl); bool traceback_enabled = tracer_.enabled();