From 00c89eba809c7410ac1bd11dee6a434ce71436b4 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Thu, 5 Dec 2024 07:11:47 +0200 Subject: [PATCH] Moved refactored to os_filesystem.cpp --- CMakeLists.txt | 1 + include/spdlog/details/file_helper.h | 17 +----------- include/spdlog/details/os.h | 16 +++++++++++ include/spdlog/sinks/daily_file_sink.h | 2 +- include/spdlog/sinks/hourly_file_sink.h | 2 +- src/details/file_helper.cpp | 18 ------------- src/details/os_windows.cpp | 36 ------------------------- src/sinks/rotating_file_sink.cpp | 2 +- tests/test_file_helper.cpp | 2 +- 9 files changed, 22 insertions(+), 74 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d0305001..f0947445 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,6 +199,7 @@ set(SPDLOG_SRCS "src/spdlog.cpp" "src/cfg/helpers.cpp" "src/details/file_helper.cpp" + "src/details/os_filesystem.cpp" "src/details/log_msg.cpp" "src/details/log_msg_buffer.cpp" "src/details/periodic_worker.cpp" diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 128fe153..5b027111 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -30,22 +30,7 @@ public: void close(); void write(const memory_buf_t &buf) const; size_t size() const; - const filename_t &filename() const; - - // - // return file path and its extension: - // - // "mylog.txt" => ("mylog", ".txt") - // "mylog" => ("mylog", "") - // "mylog." => ("mylog.", "") - // "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt") - // - // the starting dot in filenames is ignored (hidden files): - // - // ".mylog" => (".mylog". "") - // "my_folder/.mylog" => ("my_folder/.mylog", "") - // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") - static std::tuple split_by_extension(const filename_t &fname); + const filename_t &filename() const; private: const int open_tries_ = 5; diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 9ae2f8cf..ccb849f7 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -4,6 +4,7 @@ #pragma once #include // std::time_t +#include #include "../common.h" namespace spdlog { @@ -104,6 +105,21 @@ SPDLOG_API bool fsync(FILE *fp); // Return true on success. SPDLOG_API bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp); +// +// Return file path and its extension: +// +// "mylog.txt" => ("mylog", ".txt") +// "mylog" => ("mylog", "") +// "mylog." => ("mylog.", "") +// "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt") +// +// the starting dot in filenames is ignored (hidden files): +// +// ".mylog" => (".mylog". "") +// "my_folder/.mylog" => ("my_folder/.mylog", "") +// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") +SPDLOG_API std::tuple split_by_extension(const filename_t &fname); + } // namespace os } // namespace details } // namespace spdlog diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 893bf96a..68477707 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -28,7 +28,7 @@ namespace sinks { struct daily_filename_calculator { static filename_t calc_filename(const filename_t &filename, const tm &now_tm) { filename_t basename, ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); + std::tie(basename, ext) = details::os::split_by_extension(filename); std::basic_ostringstream oss; oss << basename.native() << '_' << std::setfill(SPDLOG_FILENAME_T('0')) << std::setw(4) << now_tm.tm_year + 1900 << '-' << std::setw(2) << now_tm.tm_mon + 1 << '-' << std::setw(2) << now_tm.tm_mday << ext.native(); diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index 3de176b0..d1460113 100644 --- a/include/spdlog/sinks/hourly_file_sink.h +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -27,7 +27,7 @@ namespace sinks { struct hourly_filename_calculator { static filename_t calc_filename(const filename_t &filename, const tm &now_tm) { filename_t basename, ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); + std::tie(basename, ext) = details::os::split_by_extension(filename); std::basic_ostringstream oss; oss << basename.native() << '_' << std::setfill(SPDLOG_FILENAME_T('0')) << std::setw(4) << now_tm.tm_year + 1900 << '-' << std::setw(2) << now_tm.tm_mon + 1 << '-' << std::setw(2) << now_tm.tm_mday << '_' << std::setw(2) << now_tm.tm_hour diff --git a/src/details/file_helper.cpp b/src/details/file_helper.cpp index ce831189..efc3160a 100644 --- a/src/details/file_helper.cpp +++ b/src/details/file_helper.cpp @@ -108,24 +108,6 @@ size_t file_helper::size() const { const filename_t &file_helper::filename() const { return filename_; } -// -// return file path and its extension: -// -// "mylog.txt" => ("mylog", ".txt") -// "mylog" => ("mylog", "") -// "mylog." => ("mylog", ".") -// "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt") -// -// the starting dot in filenames is ignored (hidden files): -// -// ".mylog" => (".mylog". "") -// "my_folder/.mylog" => ("my_folder/.mylog", "") -// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") -std::tuple file_helper::split_by_extension(const filename_t &fname) { - const auto ext = fname.extension(); - auto without_ext = filename_t(fname).replace_extension(); - return std::make_tuple(without_ext, ext); -} } // namespace details } // namespace spdlog diff --git a/src/details/os_windows.cpp b/src/details/os_windows.cpp index 5c5c2c5b..b101fc87 100644 --- a/src/details/os_windows.cpp +++ b/src/details/os_windows.cpp @@ -76,27 +76,7 @@ bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) { return *fp == nullptr; } -bool remove(const filename_t &filename) { return std::filesystem::remove(filename); } -bool remove_if_exists(const filename_t &filename) { - if (path_exists(filename)) { - return os::remove(filename); - } - return false; -} - - -bool rename(const filename_t &filename1, const filename_t &filename2) noexcept { - if (!std::filesystem::is_regular_file(filename1)) { - return false; - } - std::error_code ec; - std::filesystem::rename(filename1, filename2, ec); - return !ec; -} - -// Return true if path exists (file or directory) -bool path_exists(const filename_t &filename) noexcept { return std::filesystem::exists(filename); } #ifdef _MSC_VER // avoid warning about unreachable statement at the end of filesize() @@ -245,22 +225,6 @@ void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) { throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError())); } -// return true on success -// static bool mkdir_(const filename_t &path) { return ::_mkdir(path.c_str()) == 0; } - -// create the given directory - and all directories leading to it -// return true on success or if the directory already exists -bool create_dir(const filename_t &path) { - std::error_code ec; - return std::filesystem::create_directories(path, ec) || !ec; -} - -// Return directory name from given path or empty string -// "abc/file" => "abc" -// "abc/" => "abc" -// "abc" => "" -// "abc///" => "abc//" -filename_t dir_name(const filename_t &path) { return path.parent_path(); } std::string getenv(const char *field) { #if defined(_MSC_VER) diff --git a/src/sinks/rotating_file_sink.cpp b/src/sinks/rotating_file_sink.cpp index 19100fb0..e55845c6 100644 --- a/src/sinks/rotating_file_sink.cpp +++ b/src/sinks/rotating_file_sink.cpp @@ -51,7 +51,7 @@ filename_t rotating_file_sink::calc_filename(const filename_t &filename, filename_t basename; filename_t ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); + std::tie(basename, ext) = details::os::split_by_extension(filename); std::basic_ostringstream oss; oss << basename.native() << '.' << index << ext.native(); return oss.str(); diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 3a069369..bd5c18fe 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -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); }