Adding on demand truncation for basic file sinks (#3280)

* Adding support to truncate on demand for basic file sink

* Remove unnecessary file close

* Adding lock in basic_file_sink truncate()
This commit is contained in:
Matteo Del Seppia
2024-12-03 00:38:51 +01:00
committed by GitHub
parent 1e6250e183
commit 65e388e82b
3 changed files with 27 additions and 0 deletions

View File

@@ -45,6 +45,26 @@ TEST_CASE("flush_on", "[flush_on]") {
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;