mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
merge with v1.x
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
project(spdlog_utests CXX)
|
||||
|
||||
if(NOT TARGET spdlog)
|
||||
# Stand-alone build
|
||||
find_package(spdlog REQUIRED)
|
||||
endif()
|
||||
|
||||
include(../cmake/utils.cmake)
|
||||
|
||||
find_package(PkgConfig)
|
||||
@@ -12,6 +19,7 @@ set(SPDLOG_UTESTS_SOURCES
|
||||
test_file_logging.cpp
|
||||
test_daily_logger.cpp
|
||||
test_misc.cpp
|
||||
test_eventlog.cpp
|
||||
test_pattern_formatter.cpp
|
||||
test_async.cpp
|
||||
test_registry.cpp
|
||||
@@ -48,6 +56,7 @@ function(spdlog_prepare_test test_target spdlog_lib)
|
||||
spdlog_enable_sanitizer(${test_target})
|
||||
endif()
|
||||
add_test(NAME ${test_target} COMMAND ${test_target})
|
||||
set_tests_properties(${test_target} PROPERTIES RUN_SERIAL ON)
|
||||
endfunction()
|
||||
|
||||
# The compiled library tests
|
||||
|
@@ -169,9 +169,10 @@ TEST_CASE("to_file", "[async]")
|
||||
}
|
||||
}
|
||||
|
||||
REQUIRE(count_lines(filename) == messages);
|
||||
require_message_count(filename, messages);
|
||||
auto contents = file_contents(filename);
|
||||
REQUIRE(ends_with(contents, std::string("Hello message #1023\n")));
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(ends_with(contents, fmt::format("Hello message #1023{}", default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_file multi-workers", "[async]")
|
||||
@@ -191,5 +192,5 @@ TEST_CASE("to_file multi-workers", "[async]")
|
||||
}
|
||||
}
|
||||
|
||||
REQUIRE(count_lines(filename) == messages);
|
||||
require_message_count(filename, messages);
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
|
||||
logger->flush();
|
||||
|
||||
auto filename = fmt::to_string(w);
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
require_message_count(filename, 10);
|
||||
}
|
||||
|
||||
struct custom_daily_file_name_calculator
|
||||
@@ -55,12 +55,10 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
logger->
|
||||
|
||||
flush();
|
||||
logger->flush();
|
||||
|
||||
auto filename = fmt::to_string(w);
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
require_message_count(filename, 10);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -34,7 +34,8 @@ TEST_CASE("default_error_handler", "[errors]]")
|
||||
logger->info("Test message {}", 2);
|
||||
logger->flush();
|
||||
|
||||
REQUIRE(file_contents(filename) == std::string("Test message 2\n"));
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(file_contents(filename) == fmt::format("Test message 2{}", default_eol));
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
}
|
||||
|
||||
@@ -51,7 +52,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
|
||||
|
||||
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
||||
logger->info("Good message #2");
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
require_message_count(filename, 2);
|
||||
}
|
||||
|
||||
TEST_CASE("default_error_handler2", "[errors]]")
|
||||
@@ -93,7 +94,7 @@ TEST_CASE("async_error_handler", "[errors]]")
|
||||
spdlog::drop("logger"); // force logger to drain the queue and shutdown
|
||||
}
|
||||
spdlog::init_thread_pool(128, 1);
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
require_message_count(filename, 2);
|
||||
REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg);
|
||||
}
|
||||
|
||||
|
71
tests/test_eventlog.cpp
Normal file
71
tests/test_eventlog.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#if _WIN32
|
||||
|
||||
#include "includes.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
#include "spdlog/sinks/win_eventlog_sink.h"
|
||||
|
||||
static const LPCSTR TEST_SOURCE = "spdlog_test";
|
||||
|
||||
static void test_single_print(std::function<void(std::string const &)> do_log, std::string const &expected_contents, WORD expected_ev_type)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
do_log(expected_contents);
|
||||
const auto expected_time_generated = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
|
||||
|
||||
struct handle_t
|
||||
{
|
||||
HANDLE handle_;
|
||||
|
||||
~handle_t()
|
||||
{
|
||||
if (handle_)
|
||||
{
|
||||
REQUIRE(CloseEventLog(handle_));
|
||||
}
|
||||
}
|
||||
} event_log{::OpenEventLog(nullptr, TEST_SOURCE)};
|
||||
|
||||
REQUIRE(event_log.handle_);
|
||||
|
||||
DWORD read_bytes{}, size_needed{};
|
||||
auto ok =
|
||||
::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed);
|
||||
REQUIRE(!ok);
|
||||
REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
||||
|
||||
std::vector<char> record_buffer(size_needed);
|
||||
PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data();
|
||||
|
||||
ok = ::ReadEventLog(
|
||||
event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed);
|
||||
REQUIRE(ok);
|
||||
|
||||
REQUIRE(record->NumStrings == 1);
|
||||
REQUIRE(record->EventType == expected_ev_type);
|
||||
REQUIRE(record->TimeGenerated == expected_time_generated);
|
||||
|
||||
std::string message_in_log(((char *)record + record->StringOffset));
|
||||
REQUIRE(message_in_log == expected_contents + spdlog::details::os::default_eol);
|
||||
}
|
||||
|
||||
TEST_CASE("eventlog", "[eventlog]")
|
||||
{
|
||||
using namespace spdlog;
|
||||
|
||||
auto test_sink = std::make_shared<sinks::win_eventlog_sink_mt>(TEST_SOURCE);
|
||||
|
||||
spdlog::logger test_logger("eventlog", test_sink);
|
||||
test_logger.set_level(level::trace);
|
||||
|
||||
test_sink->set_pattern("%v");
|
||||
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.trace(msg); }, "my trace message", EVENTLOG_SUCCESS);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.debug(msg); }, "my debug message", EVENTLOG_SUCCESS);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.info(msg); }, "my info message", EVENTLOG_INFORMATION_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.warn(msg); }, "my warn message", EVENTLOG_WARNING_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.error(msg); }, "my error message", EVENTLOG_ERROR_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.critical(msg); }, "my critical message", EVENTLOG_ERROR_TYPE);
|
||||
}
|
||||
|
||||
#endif //_WIN32
|
@@ -15,8 +15,9 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
|
||||
logger->info("Test message {}", 2);
|
||||
|
||||
logger->flush();
|
||||
REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n"));
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
require_message_count(filename, 2);
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(file_contents(filename) == fmt::format("Test message 1{}Test message 2{}", default_eol, default_eol));
|
||||
}
|
||||
|
||||
TEST_CASE("flush_on", "[flush_on]]")
|
||||
@@ -34,8 +35,10 @@ TEST_CASE("flush_on", "[flush_on]]")
|
||||
logger->info("Test message {}", 1);
|
||||
logger->info("Test message {}", 2);
|
||||
|
||||
REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n"));
|
||||
REQUIRE(count_lines(filename) == 3);
|
||||
require_message_count(filename, 3);
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(file_contents(filename) ==
|
||||
fmt::format("Should not be flushed{}Test message 1{}Test message 2{}", default_eol, default_eol, default_eol));
|
||||
}
|
||||
|
||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||
@@ -52,7 +55,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||
|
||||
logger->flush();
|
||||
auto filename = basename;
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
require_message_count(filename, 10);
|
||||
}
|
||||
|
||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
@@ -81,7 +84,8 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
|
||||
logger->flush();
|
||||
auto filename = basename;
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
require_message_count(filename, 10);
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
|
||||
|
@@ -22,7 +22,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
|
||||
SPDLOG_LOGGER_DEBUG(logger, "Test message 2");
|
||||
logger->flush();
|
||||
|
||||
REQUIRE(ends_with(file_contents(filename), "Test message 2\n"));
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 2{}", default_eol)));
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
|
||||
spdlog::set_default_logger(logger);
|
||||
@@ -31,8 +32,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
|
||||
SPDLOG_DEBUG("Test message {}", 4);
|
||||
logger->flush();
|
||||
|
||||
REQUIRE(ends_with(file_contents(filename), "Test message 4\n"));
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
require_message_count(filename, 2);
|
||||
REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 4{}", default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("disable param evaluation", "[macros]")
|
||||
|
@@ -21,7 +21,7 @@ void prepare_logdir()
|
||||
|
||||
std::string file_contents(const std::string &filename)
|
||||
{
|
||||
std::ifstream ifs(filename);
|
||||
std::ifstream ifs(filename, std::ios_base::binary);
|
||||
if (!ifs)
|
||||
{
|
||||
throw std::runtime_error("Failed open file ");
|
||||
@@ -44,6 +44,18 @@ std::size_t count_lines(const std::string &filename)
|
||||
return counter;
|
||||
}
|
||||
|
||||
void require_message_count(const std::string &filename, const std::size_t messages)
|
||||
{
|
||||
if (strlen(spdlog::details::os::default_eol) == 0)
|
||||
{
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
REQUIRE(count_lines(filename) == messages);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t get_filesize(const std::string &filename)
|
||||
{
|
||||
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
|
||||
|
@@ -3,8 +3,6 @@
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
std::size_t count_lines(const std::string &filename);
|
||||
|
||||
std::size_t count_files(const std::string &folder);
|
||||
|
||||
void prepare_logdir();
|
||||
@@ -13,6 +11,8 @@ std::string file_contents(const std::string &filename);
|
||||
|
||||
std::size_t count_lines(const std::string &filename);
|
||||
|
||||
void require_message_count(const std::string &filename, const std::size_t messages);
|
||||
|
||||
std::size_t get_filesize(const std::string &filename);
|
||||
|
||||
bool ends_with(std::string const &value, std::string const &ending);
|
Reference in New Issue
Block a user