spdlog/include/spdlog/details/file_helper.h

153 lines
3.8 KiB
C
Raw Normal View History

2016-04-20 04:57:49 -04:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
2018-08-24 06:27:10 -04:00
// Helper class for file sinks.
2018-08-24 06:27:49 -04:00
// When failing to open a file, retry several times(5) with a delay interval(10 ms).
2018-08-24 06:26:45 -04:00
// Throw spdlog_ex exception on errors.
2016-04-20 04:57:49 -04:00
#include "spdlog/details/log_msg.h"
#include "spdlog/details/os.h"
2016-04-20 04:57:49 -04:00
2018-03-09 08:26:33 -05:00
#include <cerrno>
2016-04-20 04:57:49 -04:00
#include <chrono>
#include <cstdio>
#include <string>
#include <thread>
#include <tuple>
2016-04-20 04:57:49 -04:00
2018-03-17 06:47:46 -04:00
namespace spdlog {
namespace details {
2016-04-20 04:57:49 -04:00
class file_helper
{
2016-05-14 18:49:15 -04:00
2016-04-20 04:57:49 -04:00
public:
const int open_tries = 5;
const int open_interval = 10;
2018-02-24 19:25:15 -05:00
explicit file_helper() = default;
2016-04-20 04:57:49 -04:00
2018-03-09 08:26:33 -05:00
file_helper(const file_helper &) = delete;
file_helper &operator=(const file_helper &) = delete;
2016-04-20 04:57:49 -04:00
~file_helper()
{
close();
}
2018-03-09 08:26:33 -05:00
void open(const filename_t &fname, bool truncate = false)
2016-04-20 04:57:49 -04:00
{
close();
auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
_filename = fname;
for (int tries = 0; tries < open_tries; ++tries)
{
if (!os::fopen_s(&fd_, fname, mode))
{
2016-04-20 04:57:49 -04:00
return;
}
2016-04-20 04:57:49 -04:00
2018-01-11 15:58:02 -05:00
details::os::sleep_for_millis(open_interval);
2016-04-20 04:57:49 -04:00
}
throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno);
2016-04-20 04:57:49 -04:00
}
void reopen(bool truncate)
{
if (_filename.empty())
{
2016-04-20 04:57:49 -04:00
throw spdlog_ex("Failed re opening file - was not opened before");
}
2016-04-20 04:57:49 -04:00
open(_filename, truncate);
}
void flush()
{
std::fflush(fd_);
2016-04-20 04:57:49 -04:00
}
void close()
{
if (fd_ != nullptr)
2016-04-20 04:57:49 -04:00
{
std::fclose(fd_);
fd_ = nullptr;
2016-04-20 04:57:49 -04:00
}
}
2018-06-23 18:32:39 -04:00
void write(const fmt::memory_buffer &buf)
2016-04-20 04:57:49 -04:00
{
2018-06-23 18:32:39 -04:00
size_t msg_size = buf.size();
auto data = buf.data();
if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
{
2016-10-12 16:08:44 -04:00
throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno);
}
2016-04-20 04:57:49 -04:00
}
size_t size() const
2016-04-20 04:57:49 -04:00
{
if (fd_ == nullptr)
2018-02-25 06:41:18 -05:00
{
throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
2018-02-24 18:16:18 -05:00
}
return os::filesize(fd_);
2016-04-20 04:57:49 -04:00
}
2018-03-09 08:26:33 -05:00
const filename_t &filename() const
2016-04-20 04:57:49 -04:00
{
return _filename;
}
2018-03-09 08:26:33 -05:00
static bool file_exists(const filename_t &fname)
2016-04-20 04:57:49 -04:00
{
return os::file_exists(fname);
2016-04-20 04:57:49 -04:00
}
2017-11-30 20:46:19 -05:00
//
// return file path and its extension:
2017-11-30 20:46:19 -05:00
//
// "mylog.txt" => ("mylog", ".txt")
// "mylog" => ("mylog", "")
2017-12-22 11:55:19 -05:00
// "mylog." => ("mylog.", "")
// "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt")
2017-11-30 20:46:19 -05:00
//
// the starting dot in filenames is ignored (hidden files):
//
2017-12-22 11:55:19 -05:00
// ".mylog" => (".mylog". "")
// "my_folder/.mylog" => ("my_folder/.mylog", "")
2017-11-30 20:46:19 -05:00
// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
static std::tuple<filename_t, filename_t> split_by_extension(const spdlog::filename_t &fname)
2017-12-22 11:55:19 -05:00
{
auto ext_index = fname.rfind('.');
2018-07-21 16:48:07 -04:00
// no valid extension found - return whole path and empty string as
// extension
2017-12-22 11:55:19 -05:00
if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1)
{
2017-12-22 11:55:19 -05:00
return std::make_tuple(fname, spdlog::filename_t());
}
2017-12-22 11:55:19 -05:00
2018-01-11 15:58:02 -05:00
// treat casese like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
2017-12-22 11:55:19 -05:00
auto folder_index = fname.rfind(details::os::folder_sep);
2018-08-13 04:43:00 -04:00
if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
{
2017-12-22 11:55:19 -05:00
return std::make_tuple(fname, spdlog::filename_t());
}
2017-12-22 11:55:19 -05:00
2017-12-23 04:43:41 -05:00
// finally - return a valid base and extension tuple
2017-12-22 11:55:19 -05:00
return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
}
2018-02-24 19:25:15 -05:00
2016-04-20 04:57:49 -04:00
private:
2018-08-17 07:07:49 -04:00
std::FILE *fd_{nullptr};
2016-10-12 16:08:44 -04:00
filename_t _filename;
2016-04-20 04:57:49 -04:00
};
2018-03-17 06:47:46 -04:00
} // namespace details
} // namespace spdlog