2023-09-24 21:56:05 +08:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
2024-11-30 17:36:43 +08:00
|
|
|
#include <utility>
|
2024-12-04 06:33:42 +08:00
|
|
|
#include <filesystem>
|
2024-01-14 19:23:09 +08:00
|
|
|
|
2024-12-04 06:33:42 +08:00
|
|
|
#include "spdlog/details/file_helper.h"
|
2023-09-29 05:20:26 +08:00
|
|
|
#include "spdlog/common.h"
|
|
|
|
#include "spdlog/details/os.h"
|
|
|
|
|
2023-09-24 21:56:05 +08:00
|
|
|
namespace spdlog {
|
|
|
|
namespace details {
|
|
|
|
|
2024-11-30 17:36:43 +08:00
|
|
|
file_helper::file_helper(file_event_handlers event_handlers)
|
|
|
|
: event_handlers_(std::move(event_handlers)) {}
|
2023-09-24 21:56:05 +08:00
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
file_helper::~file_helper() { close(); }
|
2023-09-24 21:56:05 +08:00
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
void file_helper::open(const filename_t &fname, bool truncate) {
|
2023-09-24 21:56:05 +08:00
|
|
|
close();
|
|
|
|
filename_ = fname;
|
|
|
|
|
2024-01-14 01:21:41 +08:00
|
|
|
const auto *mode = SPDLOG_FILENAME_T("ab");
|
|
|
|
const auto *trunc_mode = SPDLOG_FILENAME_T("wb");
|
2023-09-24 21:56:05 +08:00
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
if (event_handlers_.before_open) {
|
2023-09-24 21:56:05 +08:00
|
|
|
event_handlers_.before_open(filename_);
|
|
|
|
}
|
2023-09-25 07:35:55 +08:00
|
|
|
for (int tries = 0; tries < open_tries_; ++tries) {
|
2023-09-24 21:56:05 +08:00
|
|
|
// create containing folder if not exists already.
|
|
|
|
os::create_dir(os::dir_name(fname));
|
2023-09-25 07:35:55 +08:00
|
|
|
if (truncate) {
|
2023-09-24 21:56:05 +08:00
|
|
|
// Truncate by opening-and-closing a tmp file in "wb" mode, always
|
|
|
|
// opening the actual log-we-write-to in "ab" mode, since that
|
|
|
|
// interacts more politely with eternal processes that might
|
|
|
|
// rotate/truncate the file underneath us.
|
2024-01-14 01:21:41 +08:00
|
|
|
std::FILE *tmp = nullptr;
|
2023-09-25 07:35:55 +08:00
|
|
|
if (os::fopen_s(&tmp, fname, trunc_mode)) {
|
2023-09-24 21:56:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::fclose(tmp);
|
|
|
|
}
|
2023-09-25 07:35:55 +08:00
|
|
|
if (!os::fopen_s(&fd_, fname, mode)) {
|
|
|
|
if (event_handlers_.after_open) {
|
2023-09-24 21:56:05 +08:00
|
|
|
event_handlers_.after_open(filename_, fd_);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
details::os::sleep_for_millis(open_interval_);
|
|
|
|
}
|
|
|
|
|
2024-01-13 15:37:32 +08:00
|
|
|
throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno);
|
2023-09-24 21:56:05 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
void file_helper::reopen(bool truncate) {
|
|
|
|
if (filename_.empty()) {
|
2023-09-24 21:56:05 +08:00
|
|
|
throw_spdlog_ex("Failed re opening file - was not opened before");
|
|
|
|
}
|
|
|
|
this->open(filename_, truncate);
|
|
|
|
}
|
|
|
|
|
2024-11-30 18:53:19 +08:00
|
|
|
void file_helper::flush() const {
|
2023-09-25 07:35:55 +08:00
|
|
|
if (std::fflush(fd_) != 0) {
|
2023-09-24 21:56:05 +08:00
|
|
|
throw_spdlog_ex("Failed flush to file " + os::filename_to_str(filename_), errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-30 18:53:19 +08:00
|
|
|
void file_helper::sync() const {
|
2023-09-25 07:35:55 +08:00
|
|
|
if (!os::fsync(fd_)) {
|
2023-09-24 21:56:05 +08:00
|
|
|
throw_spdlog_ex("Failed to fsync file " + os::filename_to_str(filename_), errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
void file_helper::close() {
|
|
|
|
if (fd_ != nullptr) {
|
|
|
|
if (event_handlers_.before_close) {
|
2023-09-24 21:56:05 +08:00
|
|
|
event_handlers_.before_close(filename_, fd_);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fclose(fd_);
|
|
|
|
fd_ = nullptr;
|
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
if (event_handlers_.after_close) {
|
2023-09-24 21:56:05 +08:00
|
|
|
event_handlers_.after_close(filename_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-30 18:53:19 +08:00
|
|
|
void file_helper::write(const memory_buf_t &buf) const {
|
2024-01-13 15:37:32 +08:00
|
|
|
if (fd_ == nullptr) return;
|
2024-11-30 18:53:19 +08:00
|
|
|
const size_t msg_size = buf.size();
|
2024-01-14 01:21:41 +08:00
|
|
|
const auto *data = buf.data();
|
2024-11-30 21:46:06 +08:00
|
|
|
if (!os::fwrite_bytes(data, msg_size, fd_)) {
|
2023-09-24 21:56:05 +08:00
|
|
|
throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
size_t file_helper::size() const {
|
|
|
|
if (fd_ == nullptr) {
|
2023-09-24 21:56:05 +08:00
|
|
|
throw_spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_));
|
|
|
|
}
|
|
|
|
return os::filesize(fd_);
|
|
|
|
}
|
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
const filename_t &file_helper::filename() const { return filename_; }
|
2023-09-24 21:56:05 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// return file path and its extension:
|
|
|
|
//
|
|
|
|
// "mylog.txt" => ("mylog", ".txt")
|
|
|
|
// "mylog" => ("mylog", "")
|
2024-12-04 06:33:42 +08:00
|
|
|
// "mylog." => ("mylog", ".")
|
2023-09-24 21:56:05 +08:00
|
|
|
// "/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")
|
2024-12-04 06:33:42 +08:00
|
|
|
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);
|
2023-09-24 21:56:05 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:40:05 +08:00
|
|
|
} // namespace details
|
|
|
|
} // namespace spdlog
|