Moved refactored to os_filesystem.cpp

This commit is contained in:
Gabi Melman 2024-12-05 07:11:47 +02:00
parent b03c45ebaa
commit 00c89eba80
9 changed files with 22 additions and 74 deletions

View File

@ -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"

View File

@ -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<filename_t, filename_t> split_by_extension(const filename_t &fname);
const filename_t &filename() const;
private:
const int open_tries_ = 5;

View File

@ -4,6 +4,7 @@
#pragma once
#include <ctime> // std::time_t
#include <tuple>
#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<filename_t, filename_t> split_by_extension(const filename_t &fname);
} // namespace os
} // namespace details
} // namespace spdlog

View File

@ -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<filename_t::value_type> 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();

View File

@ -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<filename_t::value_type> 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

View File

@ -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<filename_t, filename_t> 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

View File

@ -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)

View File

@ -51,7 +51,7 @@ filename_t rotating_file_sink<Mutex>::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<filename_t::value_type> oss;
oss << basename.native() << '.' << index << ext.native();
return oss.str();

View File

@ -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);
}