mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
Use std filesystem (#3284)
* Use std::filesystem for path names and impl
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "includes.h"
|
||||
#include "spdlog/sinks/daily_file_sink.h"
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
#include "spdlog/sinks/hourly_file_sink.h"
|
||||
|
||||
using filename_memory_buf_t = spdlog::memory_buf_t;
|
||||
|
||||
@@ -16,9 +17,8 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
||||
// calculate filename (time based)
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
filename_memory_buf_t w;
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"), basename, tm.tm_year + 1900,
|
||||
tm.tm_mon + 1, tm.tm_mday);
|
||||
auto w = spdlog::fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"), basename.native(), tm.tm_year + 1900,
|
||||
tm.tm_mon + 1, tm.tm_mday);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
@@ -26,16 +26,15 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
||||
}
|
||||
logger->flush();
|
||||
|
||||
require_message_count(SPDLOG_BUF_TO_STRING(w), 10);
|
||||
require_message_count(w, 10);
|
||||
}
|
||||
|
||||
struct custom_daily_file_name_calculator {
|
||||
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm) {
|
||||
filename_memory_buf_t w;
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename,
|
||||
now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday);
|
||||
auto w = spdlog::fmt_lib::format(SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename.native(), now_tm.tm_year + 1900,
|
||||
now_tm.tm_mon + 1, now_tm.tm_mday);
|
||||
|
||||
return SPDLOG_BUF_TO_STRING(w);
|
||||
return w;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,9 +46,10 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") {
|
||||
// calculate filename (time based)
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
filename_memory_buf_t w;
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, tm.tm_year + 1900,
|
||||
tm.tm_mon + 1, tm.tm_mday);
|
||||
|
||||
auto w = spdlog::fmt_lib::format(SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename.native(), tm.tm_year + 1900,
|
||||
tm.tm_mon + 1,
|
||||
tm.tm_mday);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
@@ -57,7 +57,7 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") {
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
require_message_count(SPDLOG_BUF_TO_STRING(w), 10);
|
||||
require_message_count(w, 10);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -82,17 +82,32 @@ TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]") {
|
||||
// regex supported only from gcc 4.9 and above
|
||||
#if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
|
||||
|
||||
#include <regex>
|
||||
#include <regex>
|
||||
|
||||
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]") {
|
||||
// daily_YYYY-MM-DD_hh-mm.txt
|
||||
auto filename =
|
||||
spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.txt"),
|
||||
spdlog::details::os::localtime());
|
||||
// date regex based on https://www.regular-expressions.info/dates.html
|
||||
std::basic_regex<spdlog::filename_t::value_type> re(
|
||||
SPDLOG_FILENAME_T(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"));
|
||||
|
||||
std::match_results<spdlog::filename_t::string_type::const_iterator> match;
|
||||
REQUIRE(std::regex_match(filename.native(), match, re));
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("hourly_file_sink::hourly_filename_calculator", "[hrouly_file_sink]") {
|
||||
// daily_YYYY-MM-DD_hh-mm.txt
|
||||
auto filename =
|
||||
spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.txt"), spdlog::details::os::localtime());
|
||||
spdlog::sinks::hourly_filename_calculator::calc_filename(SPDLOG_FILENAME_T("hourly.txt"), spdlog::details::os::localtime());
|
||||
// date regex based on https://www.regular-expressions.info/dates.html
|
||||
std::basic_regex<spdlog::filename_t::value_type> re(
|
||||
SPDLOG_FILENAME_T(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"));
|
||||
std::match_results<spdlog::filename_t::const_iterator> match;
|
||||
REQUIRE(std::regex_match(filename, match, re));
|
||||
SPDLOG_FILENAME_T(R"(^hourly_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d\.txt$)"));
|
||||
|
||||
std::match_results<spdlog::filename_t::string_type::const_iterator> match;
|
||||
REQUIRE(std::regex_match(filename.native(), match, re));
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -148,4 +163,4 @@ TEST_CASE("daily_logger rotate", "[daily_file_sink]") {
|
||||
test_rotate(days_to_run, 10, 10);
|
||||
test_rotate(days_to_run, 11, 10);
|
||||
test_rotate(days_to_run, 20, 10);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user