mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
daily_file_sink with custom file name calculator
This commit is contained in:
@@ -77,15 +77,56 @@ TEST_CASE("daily_logger", "[daily_logger]]")
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
|
||||
{
|
||||
using sink_type = spdlog::sinks::daily_file_sink<
|
||||
std::mutex,
|
||||
spdlog::sinks::dateonly_daily_file_name_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
//calculate filename (time based)
|
||||
std::string basename = "logs/daily_dateonly";
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
w.write("{}_{:04d}-{:02d}-{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, "txt", 0, 0, true);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
logger->info("Test message {}", i);
|
||||
|
||||
auto filename = w.str();
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
}
|
||||
|
||||
struct custom_daily_file_name_calculator
|
||||
{
|
||||
static spdlog::filename_t calc_filename(const spdlog::filename_t& basename, const spdlog::filename_t& extension)
|
||||
{
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
w.write("{}{:04d}{:02d}{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, extension);
|
||||
return w.str();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
|
||||
{
|
||||
using sink_type = spdlog::sinks::daily_file_sink<
|
||||
std::mutex,
|
||||
custom_daily_file_name_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
//calculate filename (time based)
|
||||
std::string basename = "logs/daily_dateonly";
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
w.write("{}{:04d}{:02d}{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, "txt", 0, 0, true);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
logger->info("Test message {}", i);
|
||||
|
||||
|
||||
|
||||
|
||||
auto filename = w.str();
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user