mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-26 10:31:34 +08:00
65e388e82b
Some checks are pending
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[asan:ON build_type:Debug … (push) Waiting to run
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Debug compiler… (push) Waiting to run
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Release compil… (push) Waiting to run
linux / OS X Clang (C++11, Release) (push) Waiting to run
macos / macOS Clang (C++11, Release) (push) Waiting to run
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022 USE_STD_FORMAT:ON WCHAR:OFF WCHAR_FILES:OFF]) (push) Waiting to run
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022 USE_STD_FORMAT:ON WCHAR:ON WCHAR_FILES:ON]) (push) Waiting to run
windows / build (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022 USE_STD_FORMAT:OFF WCHAR:OFF WCHAR_FILES:OFF]) (push) Waiting to run
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:11 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019 USE_STD_FORMAT:OFF WCHAR:OFF WCHAR_FILES:OFF]) (push) Waiting to run
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:14 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019 USE_STD_FORMAT:OFF WCHAR:OFF WCHAR_FILES:OFF]) (push) Waiting to run
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019 USE_STD_FORMAT:OFF WCHAR:OFF WCHAR_FILES:OFF]) (push) Waiting to run
* Adding support to truncate on demand for basic file sink * Remove unnecessary file close * Adding lock in basic_file_sink truncate()
144 lines
4.7 KiB
C++
144 lines
4.7 KiB
C++
/*
|
|
* This content is released under the MIT License as specified in
|
|
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
|
*/
|
|
#include "includes.h"
|
|
|
|
#define SIMPLE_LOG "test_logs/simple_log"
|
|
#define ROTATING_LOG "test_logs/rotating_log"
|
|
|
|
TEST_CASE("simple_file_logger", "[simple_logger]") {
|
|
prepare_logdir();
|
|
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
|
|
|
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
|
|
logger->set_pattern("%v");
|
|
|
|
logger->info("Test message {}", 1);
|
|
logger->info("Test message {}", 2);
|
|
|
|
logger->flush();
|
|
require_message_count(SIMPLE_LOG, 2);
|
|
using spdlog::details::os::default_eol;
|
|
REQUIRE(file_contents(SIMPLE_LOG) ==
|
|
spdlog::fmt_lib::format("Test message 1{}Test message 2{}", default_eol, default_eol));
|
|
}
|
|
|
|
TEST_CASE("flush_on", "[flush_on]") {
|
|
prepare_logdir();
|
|
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
|
|
|
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
|
|
logger->set_pattern("%v");
|
|
logger->set_level(spdlog::level::trace);
|
|
logger->flush_on(spdlog::level::info);
|
|
logger->trace("Should not be flushed");
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 0);
|
|
|
|
logger->info("Test message {}", 1);
|
|
logger->info("Test message {}", 2);
|
|
|
|
require_message_count(SIMPLE_LOG, 3);
|
|
using spdlog::details::os::default_eol;
|
|
REQUIRE(file_contents(SIMPLE_LOG) ==
|
|
spdlog::fmt_lib::format("Should not be flushed{}Test message 1{}Test message 2{}",
|
|
default_eol, default_eol, default_eol));
|
|
}
|
|
|
|
TEST_CASE("simple_file_logger", "[truncate]") {
|
|
prepare_logdir();
|
|
const spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
|
const bool truncate = true;
|
|
const auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, truncate);
|
|
const auto logger = std::make_shared<spdlog::logger>("simple_file_logger", sink);
|
|
|
|
logger->info("Test message {}", 3.14);
|
|
logger->info("Test message {}", 2.71);
|
|
logger->flush();
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 2);
|
|
|
|
sink->truncate();
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 0);
|
|
|
|
logger->info("Test message {}", 6.28);
|
|
logger->flush();
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 1);
|
|
}
|
|
|
|
TEST_CASE("rotating_file_logger1", "[rotating_logger]") {
|
|
prepare_logdir();
|
|
size_t max_size = 1024 * 10;
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0);
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
logger->info("Test message {}", i);
|
|
}
|
|
|
|
logger->flush();
|
|
require_message_count(ROTATING_LOG, 10);
|
|
}
|
|
|
|
TEST_CASE("rotating_file_logger2", "[rotating_logger]") {
|
|
prepare_logdir();
|
|
size_t max_size = 1024 * 10;
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
|
|
|
{
|
|
// make an initial logger to create the first output file
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
|
for (int i = 0; i < 10; ++i) {
|
|
logger->info("Test message {}", i);
|
|
}
|
|
// drop causes the logger destructor to be called, which is required so the
|
|
// next logger can rename the first output file.
|
|
spdlog::drop(logger->name());
|
|
}
|
|
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
|
for (int i = 0; i < 10; ++i) {
|
|
logger->info("Test message {}", i);
|
|
}
|
|
|
|
logger->flush();
|
|
|
|
require_message_count(ROTATING_LOG, 10);
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
logger->info("Test message {}", i);
|
|
}
|
|
|
|
logger->flush();
|
|
REQUIRE(get_filesize(ROTATING_LOG) <= max_size);
|
|
REQUIRE(get_filesize(ROTATING_LOG ".1") <= max_size);
|
|
}
|
|
|
|
// test that passing max_size=0 throws
|
|
TEST_CASE("rotating_file_logger3", "[rotating_logger]") {
|
|
prepare_logdir();
|
|
size_t max_size = 0;
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
|
REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0),
|
|
spdlog::spdlog_ex);
|
|
}
|
|
|
|
// test on-demand rotation of logs
|
|
TEST_CASE("rotating_file_logger4", "[rotating_logger]") {
|
|
prepare_logdir();
|
|
size_t max_size = 1024 * 10;
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
|
auto sink = std::make_shared<spdlog::sinks::rotating_file_sink_st>(basename, max_size, 2);
|
|
auto logger = std::make_shared<spdlog::logger>("rotating_sink_logger", sink);
|
|
|
|
logger->info("Test message - pre-rotation");
|
|
logger->flush();
|
|
|
|
sink->rotate_now();
|
|
|
|
logger->info("Test message - post-rotation");
|
|
logger->flush();
|
|
|
|
REQUIRE(get_filesize(ROTATING_LOG) > 0);
|
|
REQUIRE(get_filesize(ROTATING_LOG ".1") > 0);
|
|
}
|