1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-04-01 02:42:41 +08:00
spdlog/include/spdlog/sinks/file_sinks.h

252 lines
7.6 KiB
C
Raw Normal View History

2016-04-20 16:57:49 +08:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include "../details/file_helper.h"
2018-03-09 21:26:33 +08:00
#include "../details/null_mutex.h"
#include "../fmt/fmt.h"
2018-03-09 21:26:33 +08:00
#include "base_sink.h"
2016-04-20 16:57:49 +08:00
#include <algorithm>
2018-03-09 21:26:33 +08:00
#include <cerrno>
2016-04-20 16:57:49 +08:00
#include <chrono>
#include <cstdio>
#include <ctime>
#include <mutex>
#include <string>
2018-03-17 18:47:46 +08:00
namespace spdlog {
namespace sinks {
2016-04-20 16:57:49 +08:00
/*
* Trivial file sink with single file as target
*/
2018-03-16 23:35:56 +08:00
template<class Mutex>
class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex>
2016-04-20 16:57:49 +08:00
{
public:
2018-03-09 21:26:33 +08:00
explicit simple_file_sink(const filename_t &filename, bool truncate = false)
: _force_flush(false)
2016-04-20 16:57:49 +08:00
{
_file_helper.open(filename, truncate);
2016-04-20 16:57:49 +08:00
}
2016-10-13 04:08:44 +08:00
void set_force_flush(bool force_flush)
{
_force_flush = force_flush;
}
2016-04-20 16:57:49 +08:00
protected:
2018-03-09 21:26:33 +08:00
void _sink_it(const details::log_msg &msg) override
2016-04-20 16:57:49 +08:00
{
2016-10-13 04:08:44 +08:00
_file_helper.write(msg);
2018-03-09 21:26:33 +08:00
if (_force_flush)
2016-10-13 04:08:44 +08:00
_file_helper.flush();
2016-04-20 16:57:49 +08:00
}
2018-02-25 06:56:56 +08:00
void _flush() override
{
_file_helper.flush();
}
2018-02-25 06:56:56 +08:00
2016-04-20 16:57:49 +08:00
private:
details::file_helper _file_helper;
2016-10-13 04:08:44 +08:00
bool _force_flush;
2016-04-20 16:57:49 +08:00
};
2018-02-25 05:35:09 +08:00
using simple_file_sink_mt = simple_file_sink<std::mutex>;
using simple_file_sink_st = simple_file_sink<details::null_mutex>;
2016-04-20 16:57:49 +08:00
/*
* Rotating file sink based on size
*/
2018-03-16 23:35:56 +08:00
template<class Mutex>
class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex>
2016-04-20 16:57:49 +08:00
{
public:
2018-03-09 21:26:33 +08:00
rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files)
: _base_filename(std::move(base_filename))
, _max_size(max_size)
, _max_files(max_files)
2016-04-20 16:57:49 +08:00
{
_file_helper.open(calc_filename(_base_filename, 0));
2018-03-09 21:26:33 +08:00
_current_size = _file_helper.size(); // expensive. called only once
2016-04-20 16:57:49 +08:00
}
2017-12-01 09:46:19 +08:00
// calc filename according to index and file extension if exists.
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
2018-03-09 21:26:33 +08:00
static filename_t calc_filename(const filename_t &filename, std::size_t index)
2017-12-01 09:46:19 +08:00
{
typename std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
2018-02-25 07:16:18 +08:00
if (index != 0u)
2017-12-01 09:46:19 +08:00
{
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
w.write(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
}
else
{
w.write(SPDLOG_FILENAME_T("{}"), filename);
}
return w.str();
}
2016-04-20 16:57:49 +08:00
protected:
2018-03-09 21:26:33 +08:00
void _sink_it(const details::log_msg &msg) override
2016-04-20 16:57:49 +08:00
{
_current_size += msg.formatted.size();
if (_current_size > _max_size)
{
_rotate();
_current_size = msg.formatted.size();
}
_file_helper.write(msg);
}
void _flush() override
{
_file_helper.flush();
}
2017-05-21 08:43:41 +08:00
2017-12-01 09:46:19 +08:00
private:
2016-04-20 16:57:49 +08:00
// Rotate files:
// log.txt -> log.1.txt
// log.1.txt -> log.2.txt
// log.2.txt -> log.3.txt
// log.3.txt -> delete
2016-04-20 16:57:49 +08:00
void _rotate()
{
2016-05-15 06:49:15 +08:00
using details::os::filename_to_str;
2016-04-20 16:57:49 +08:00
_file_helper.close();
for (auto i = _max_files; i > 0; --i)
{
filename_t src = calc_filename(_base_filename, i - 1);
filename_t target = calc_filename(_base_filename, i);
2016-04-20 16:57:49 +08:00
if (details::file_helper::file_exists(target))
{
if (details::os::remove(target) != 0)
{
throw spdlog_ex("rotating_file_sink: failed removing " + filename_to_str(target), errno);
2016-04-20 16:57:49 +08:00
}
}
2018-02-25 07:16:18 +08:00
if (details::file_helper::file_exists(src) && details::os::rename(src, target) != 0)
2016-04-20 16:57:49 +08:00
{
throw spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
2016-04-20 16:57:49 +08:00
}
}
_file_helper.reopen(true);
}
2018-02-25 06:56:56 +08:00
2016-04-20 16:57:49 +08:00
filename_t _base_filename;
std::size_t _max_size;
std::size_t _max_files;
std::size_t _current_size;
details::file_helper _file_helper;
};
2018-02-25 05:35:09 +08:00
using rotating_file_sink_mt = rotating_file_sink<std::mutex>;
using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
2016-04-20 16:57:49 +08:00
/*
* Default generator of daily log file names.
*/
struct default_daily_file_name_calculator
{
// Create filename for the form filename.YYYY-MM-DD_hh-mm.ext
2018-03-09 21:26:33 +08:00
static filename_t calc_filename(const filename_t &filename)
{
std::tm tm = spdlog::details::os::localtime();
2017-12-01 09:46:19 +08:00
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
2018-03-09 21:26:33 +08:00
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, ext);
return w.str();
}
};
/*
* Generator of daily log file names in format basename.YYYY-MM-DD.ext
*/
struct dateonly_daily_file_name_calculator
{
// Create filename for the form basename.YYYY-MM-DD
2018-03-09 21:26:33 +08:00
static filename_t calc_filename(const filename_t &filename)
{
std::tm tm = spdlog::details::os::localtime();
2017-12-01 09:46:19 +08:00
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, ext);
return w.str();
}
};
2016-04-20 16:57:49 +08:00
/*
* Rotating file sink based on date. rotates at midnight
*/
2018-03-16 23:35:56 +08:00
template<class Mutex, class FileNameCalc = default_daily_file_name_calculator>
class daily_file_sink SPDLOG_FINAL : public base_sink<Mutex>
2016-04-20 16:57:49 +08:00
{
public:
2018-03-09 21:26:33 +08:00
// create daily file sink which rotates on given time
daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute)
: _base_filename(std::move(base_filename))
, _rotation_h(rotation_hour)
, _rotation_m(rotation_minute)
2016-04-20 16:57:49 +08:00
{
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
_rotation_tp = _next_rotation_tp();
_file_helper.open(FileNameCalc::calc_filename(_base_filename));
2016-04-20 16:57:49 +08:00
}
protected:
2018-03-09 21:26:33 +08:00
void _sink_it(const details::log_msg &msg) override
2016-04-20 16:57:49 +08:00
{
if (std::chrono::system_clock::now() >= _rotation_tp)
{
_file_helper.open(FileNameCalc::calc_filename(_base_filename));
2016-04-20 16:57:49 +08:00
_rotation_tp = _next_rotation_tp();
}
_file_helper.write(msg);
}
void _flush() override
{
_file_helper.flush();
}
2017-05-21 08:43:41 +08:00
2016-04-20 16:57:49 +08:00
private:
std::chrono::system_clock::time_point _next_rotation_tp()
2016-07-23 01:19:26 +08:00
{
auto now = std::chrono::system_clock::now();
2016-04-20 16:57:49 +08:00
time_t tnow = std::chrono::system_clock::to_time_t(now);
tm date = spdlog::details::os::localtime(tnow);
date.tm_hour = _rotation_h;
date.tm_min = _rotation_m;
date.tm_sec = 0;
auto rotation_time = std::chrono::system_clock::from_time_t(std::mktime(&date));
2018-02-25 19:41:18 +08:00
if (rotation_time > now)
{
2016-04-20 16:57:49 +08:00
return rotation_time;
2018-02-25 07:24:47 +08:00
}
2018-03-09 21:26:33 +08:00
return {rotation_time + std::chrono::hours(24)};
2016-04-20 16:57:49 +08:00
}
filename_t _base_filename;
int _rotation_h;
int _rotation_m;
std::chrono::system_clock::time_point _rotation_tp;
details::file_helper _file_helper;
};
2018-02-25 05:35:09 +08:00
using daily_file_sink_mt = daily_file_sink<std::mutex>;
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
2018-03-17 18:47:46 +08:00
} // namespace sinks
} // namespace spdlog