|
|
|
@ -28,7 +28,7 @@ template<class Mutex>
|
|
|
|
|
class simple_file_sink : public base_sink < Mutex >
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit simple_file_sink(const std::string &filename,
|
|
|
|
|
explicit simple_file_sink(const filename_str_t &filename,
|
|
|
|
|
bool force_flush = false) :
|
|
|
|
|
_file_helper(force_flush)
|
|
|
|
|
{
|
|
|
|
@ -58,7 +58,7 @@ template<class Mutex>
|
|
|
|
|
class rotating_file_sink : public base_sink < Mutex >
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
rotating_file_sink(const std::string &base_filename, const std::string &extension,
|
|
|
|
|
rotating_file_sink(const filename_str_t &base_filename, const filename_str_t &extension,
|
|
|
|
|
std::size_t max_size, std::size_t max_files,
|
|
|
|
|
bool force_flush = false) :
|
|
|
|
|
_base_filename(base_filename),
|
|
|
|
@ -90,13 +90,13 @@ protected:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static std::string calc_filename(const std::string& filename, std::size_t index, const std::string& extension)
|
|
|
|
|
static filename_str_t calc_filename(const filename_str_t& filename, std::size_t index, const filename_str_t& extension)
|
|
|
|
|
{
|
|
|
|
|
fmt::MemoryWriter w;
|
|
|
|
|
std::conditional<std::is_same<filename_char_t, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
|
|
|
|
|
if (index)
|
|
|
|
|
w.write("{}.{}.{}", filename, index, extension);
|
|
|
|
|
w.write(SPDLOG_FILENAME_T("{}.{}.{}"), filename, index, extension);
|
|
|
|
|
else
|
|
|
|
|
w.write("{}.{}", filename, extension);
|
|
|
|
|
w.write(SPDLOG_FILENAME_T("{}.{}"), filename, extension);
|
|
|
|
|
return w.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -111,25 +111,25 @@ private:
|
|
|
|
|
_file_helper.close();
|
|
|
|
|
for (auto i = _max_files; i > 0; --i)
|
|
|
|
|
{
|
|
|
|
|
std::string src = calc_filename(_base_filename, i - 1, _extension);
|
|
|
|
|
std::string target = calc_filename(_base_filename, i, _extension);
|
|
|
|
|
filename_str_t src = calc_filename(_base_filename, i - 1, _extension);
|
|
|
|
|
filename_str_t target = calc_filename(_base_filename, i, _extension);
|
|
|
|
|
|
|
|
|
|
if (details::file_helper::file_exists(target))
|
|
|
|
|
{
|
|
|
|
|
if (std::remove(target.c_str()) != 0)
|
|
|
|
|
if (details::os::remove(target.c_str()) != 0)
|
|
|
|
|
{
|
|
|
|
|
throw spdlog_ex("rotating_file_sink: failed removing " + target);
|
|
|
|
|
throw spdlog_ex("rotating_file_sink: failed removing " + filename_to_bytes(target));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str()))
|
|
|
|
|
if (details::file_helper::file_exists(src) && details::os::rename(src.c_str(), target.c_str()))
|
|
|
|
|
{
|
|
|
|
|
throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target);
|
|
|
|
|
throw spdlog_ex("rotating_file_sink: failed renaming " + filename_to_bytes(src) + " to " + filename_to_bytes(target));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_file_helper.reopen(true);
|
|
|
|
|
}
|
|
|
|
|
std::string _base_filename;
|
|
|
|
|
std::string _extension;
|
|
|
|
|
filename_str_t _base_filename;
|
|
|
|
|
filename_str_t _extension;
|
|
|
|
|
std::size_t _max_size;
|
|
|
|
|
std::size_t _max_files;
|
|
|
|
|
std::size_t _current_size;
|
|
|
|
@ -148,8 +148,8 @@ class daily_file_sink :public base_sink < Mutex >
|
|
|
|
|
public:
|
|
|
|
|
//create daily file sink which rotates on given time
|
|
|
|
|
daily_file_sink(
|
|
|
|
|
const std::string& base_filename,
|
|
|
|
|
const std::string& extension,
|
|
|
|
|
const filename_str_t& base_filename,
|
|
|
|
|
const filename_str_t& extension,
|
|
|
|
|
int rotation_hour,
|
|
|
|
|
int rotation_minute,
|
|
|
|
|
bool force_flush = false) : _base_filename(base_filename),
|
|
|
|
@ -198,16 +198,16 @@ private:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Create filename for the form basename.YYYY-MM-DD.extension
|
|
|
|
|
static std::string calc_filename(const std::string& basename, const std::string& extension)
|
|
|
|
|
static filename_str_t calc_filename(const filename_str_t& basename, const filename_str_t& extension)
|
|
|
|
|
{
|
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
|
fmt::MemoryWriter w;
|
|
|
|
|
w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, extension);
|
|
|
|
|
std::conditional<std::is_same<filename_char_t, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
|
|
|
|
|
w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, extension);
|
|
|
|
|
return w.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string _base_filename;
|
|
|
|
|
std::string _extension;
|
|
|
|
|
filename_str_t _base_filename;
|
|
|
|
|
filename_str_t _extension;
|
|
|
|
|
int _rotation_h;
|
|
|
|
|
int _rotation_m;
|
|
|
|
|
std::chrono::system_clock::time_point _rotation_tp;
|
|
|
|
|