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:
@@ -31,3 +31,8 @@
|
||||
#include "spdlog/sinks/null_sink.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
// to_string_view
|
||||
[[nodiscard]] constexpr spdlog::string_view_t to_string_view(const spdlog::memory_buf_t &buf) noexcept {
|
||||
return spdlog::string_view_t{buf.data(), buf.size()};
|
||||
}
|
||||
|
@@ -43,34 +43,38 @@ TEST_CASE("create_invalid_dir", "[create_dir]") {
|
||||
}
|
||||
|
||||
TEST_CASE("dir_name", "[create_dir]") {
|
||||
using spdlog::details::os::dir_name;
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("")).empty());
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir")).empty());
|
||||
|
||||
#ifdef WIN32
|
||||
using spdlog::details::os::dir_name;
|
||||
#ifdef WIN32
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\)")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\\\)")) == SPDLOG_FILENAME_T(R"(dir\\)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\\\)")) == SPDLOG_FILENAME_T(R"(dir)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file)")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir/file)")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt)")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir/file)")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt\)")) == SPDLOG_FILENAME_T(R"(dir\file.txt)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\dir)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\\dir)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\\dir\)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(..\file.txt)")) == SPDLOG_FILENAME_T(".."));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(.\file.txt)")) == SPDLOG_FILENAME_T("."));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c:\\a\b\c\d\file.txt)")) == SPDLOG_FILENAME_T(R"(c:\\a\b\c\d)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c://a/b/c/d/file.txt)")) == SPDLOG_FILENAME_T(R"(c://a/b/c/d)"));
|
||||
#endif
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("")).empty());
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir")).empty());
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir///")) == SPDLOG_FILENAME_T("dir//"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir///")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file.txt")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file.txt/")) == SPDLOG_FILENAME_T("dir/file.txt"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("/dir/file.txt")) == SPDLOG_FILENAME_T("/dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("//dir/file.txt")) == SPDLOG_FILENAME_T("//dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("/dir/file.txt")) == SPDLOG_FILENAME_T("/dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("../file.txt")) == SPDLOG_FILENAME_T(".."));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("./file.txt")) == SPDLOG_FILENAME_T("."));
|
||||
|
||||
#ifdef _WIN32
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("//dir/file.txt")) == SPDLOG_FILENAME_T("//dir/"));
|
||||
#else
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("//dir/file.txt")) == SPDLOG_FILENAME_T("//dir"));
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -122,15 +126,4 @@ TEST_CASE("create_abs_path2", "[create_dir]") {
|
||||
REQUIRE(create_dir(abs_path) == true);
|
||||
}
|
||||
|
||||
TEST_CASE("non_existing_drive", "[create_dir]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t path;
|
||||
|
||||
auto non_existing_drive = find_non_existing_drive();
|
||||
path += non_existing_drive;
|
||||
path += SPDLOG_FILENAME_T(":\\");
|
||||
REQUIRE(create_dir(path) == false);
|
||||
path += SPDLOG_FILENAME_T("subdir");
|
||||
REQUIRE(create_dir(path) == false);
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -70,7 +70,7 @@ static void test_split_ext(const spdlog::filename_t::value_type *fname,
|
||||
|
||||
spdlog::filename_t basename;
|
||||
spdlog::filename_t ext;
|
||||
std::tie(basename, ext) = file_helper::split_by_extension(filename);
|
||||
std::tie(basename, ext) = spdlog::details::os::split_by_extension(filename);
|
||||
REQUIRE(basename == expected_base);
|
||||
REQUIRE(ext == expected_ext);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ TEST_CASE("file_helper_split_by_extension", "[file_helper::split_by_extension()]
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog.txt"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog.txt"), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T("."));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog.txt"), SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog"),
|
||||
SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog.txt"), SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog"),
|
||||
@@ -92,7 +92,7 @@ TEST_CASE("file_helper_split_by_extension", "[file_helper::split_by_extension()]
|
||||
test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt"), SPDLOG_FILENAME_T(".././mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt/xxx"), SPDLOG_FILENAME_T(".././mylog.txt/xxx"), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/mylog.txt"), SPDLOG_FILENAME_T("/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("//mylog.txt"), SPDLOG_FILENAME_T("//mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("///mylog.txt"), SPDLOG_FILENAME_T("///mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(""), SPDLOG_FILENAME_T(""), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("..txt"), SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T(".txt"));
|
||||
|
@@ -2,7 +2,6 @@
|
||||
#include "includes.h"
|
||||
|
||||
using spdlog::memory_buf_t;
|
||||
using spdlog::details::to_string_view;
|
||||
|
||||
void test_pad2(int n, const char *expected) {
|
||||
memory_buf_t buf;
|
||||
|
@@ -3,7 +3,6 @@
|
||||
#include "test_sink.h"
|
||||
|
||||
using spdlog::memory_buf_t;
|
||||
using spdlog::details::to_string_view;
|
||||
|
||||
// log to str and return it
|
||||
template <typename... Args>
|
||||
|
@@ -27,7 +27,7 @@ std::string file_contents(const std::string &filename) {
|
||||
return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
||||
}
|
||||
|
||||
std::size_t count_lines(const std::string &filename) {
|
||||
std::size_t count_lines(const spdlog::filename_t &filename) {
|
||||
std::ifstream ifs(filename);
|
||||
if (!ifs) {
|
||||
throw std::runtime_error("Failed open file ");
|
||||
@@ -39,7 +39,7 @@ std::size_t count_lines(const std::string &filename) {
|
||||
return counter;
|
||||
}
|
||||
|
||||
void require_message_count(const std::string &filename, const std::size_t messages) {
|
||||
void require_message_count(const std::filesystem::path &filename, const std::size_t messages) {
|
||||
if (strlen(spdlog::details::os::default_eol) == 0) {
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
} else {
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
std::size_t count_files(const std::string &folder);
|
||||
|
||||
@@ -9,9 +10,10 @@ void prepare_logdir();
|
||||
|
||||
std::string file_contents(const std::string &filename);
|
||||
|
||||
std::size_t count_lines(const std::string &filename);
|
||||
//std::size_t count_lines(const std::string &filename);
|
||||
std::size_t count_lines(const std::filesystem::path &filename);
|
||||
|
||||
void require_message_count(const std::string &filename, const std::size_t messages);
|
||||
void require_message_count(const std::filesystem::path &filename, const std::size_t messages);
|
||||
|
||||
std::size_t get_filesize(const std::string &filename);
|
||||
|
||||
|
Reference in New Issue
Block a user