mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
Merge pull request #1521 from ron003/single_log_method_with_time_point
add single logger method and log_msg constructor and tests/test_time_…
This commit is contained in:
commit
5716ab70ec
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
35
tests/test_time_point.cpp
Normal file
35
tests/test_time_point.cpp
Normal file
@ -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<spdlog::sinks::test_sink_st> 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<std::string> 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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user