mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-30 02:19:35 +08:00
Add new logger: step_logger
This commit is contained in:
@@ -179,6 +179,71 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
}
|
||||
|
||||
TEST_CASE("step_logger", "[step_logger]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
// calculate filename (time based)
|
||||
std::string basename = "logs/step_log";
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
|
||||
auto logger = spdlog::step_logger_mt("logger", basename, 60);
|
||||
logger->flush_on(spdlog::level::info);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
#if !defined(SPDLOG_FMT_PRINTF)
|
||||
logger->info("Test message {}", i);
|
||||
#else
|
||||
logger->info("Test message %d", i);
|
||||
#endif
|
||||
}
|
||||
|
||||
auto filename = w.str();
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
}
|
||||
|
||||
struct custom_step_file_name_calculator
|
||||
{
|
||||
static std::tuple<spdlog::filename_t, spdlog::filename_t> calc_filename(const spdlog::filename_t &filename)
|
||||
{
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
spdlog::filename_t basename, ext;
|
||||
std::tie(basename, ext) = spdlog::details::file_helper::split_by_extenstion(filename);
|
||||
std::conditional<std::is_same<spdlog::filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
|
||||
w.write(SPDLOG_FILENAME_T("{}.{:04d}:{:02d}:{:02d}.{:02d}:{:02d}:{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
return std::make_tuple(w.str(), ext);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("step_logger with custom calculator", "[step_logger_custom]]")
|
||||
{
|
||||
using sink_type = spdlog::sinks::step_file_sink<std::mutex, custom_step_file_name_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
|
||||
std::string basename = "logs/step_log_custom";
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
w.write("{}.{:04d}:{:02d}:{:02d}.{:02d}:{:02d}:{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, 60, std::numeric_limits<int>::max());
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
#if !defined(SPDLOG_FMT_PRINTF)
|
||||
logger->info("Test message {}", i);
|
||||
#else
|
||||
logger->info("Test message %d", i);
|
||||
#endif
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
auto filename = w.str();
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
}
|
||||
|
||||
/*
|
||||
* File name calculations
|
||||
*/
|
||||
|
Reference in New Issue
Block a user