spdlog/include/c11log/sinks/file_sinks.h

181 lines
5.4 KiB
C
Raw Normal View History

2014-01-25 17:09:04 +08:00
#pragma once
2014-01-25 21:52:10 +08:00
2014-01-25 17:09:04 +08:00
#include <fstream>
#include <iomanip>
2014-01-26 07:53:23 +08:00
#include <mutex>
2014-01-25 17:09:04 +08:00
#include "base_sink.h"
2014-02-22 17:57:53 +08:00
#include "../details/flush_helper.h"
2014-01-25 17:09:04 +08:00
2014-03-07 06:52:50 +08:00
namespace c11log
{
namespace sinks
{
2014-02-22 17:57:53 +08:00
2014-01-25 17:09:04 +08:00
/*
* Thread safe, trivial file sink with single file as target
2014-01-25 17:09:04 +08:00
*/
2014-03-07 06:52:50 +08:00
class simple_file_sink : public base_sink
{
2014-01-25 17:09:04 +08:00
public:
2014-03-01 20:06:58 +08:00
explicit simple_file_sink(const std::string &filename,
const std::string& extension,
const std::size_t flush_every=0)
2014-02-22 16:34:42 +08:00
: _mutex(),
2014-03-01 20:06:58 +08:00
_ofstream(filename + "." + extension, std::ofstream::binary|std::ofstream::app),
_flush_helper(flush_every)
{
2014-02-22 04:51:54 +08:00
}
2014-01-25 17:09:04 +08:00
protected:
void _sink_it(const std::string& msg) override
{
2014-02-22 16:34:42 +08:00
std::lock_guard<std::mutex> lock(_mutex);
2014-02-22 17:57:53 +08:00
_flush_helper.write(_ofstream, msg);
2014-02-22 04:51:54 +08:00
}
2014-01-26 07:53:23 +08:00
private:
2014-02-22 16:34:42 +08:00
std::mutex _mutex;
2014-02-22 04:51:54 +08:00
std::ofstream _ofstream;
2014-02-22 17:57:53 +08:00
details::file_flush_helper _flush_helper;
2014-01-25 17:09:04 +08:00
};
/*
* Thread safe, rotating file sink based on size
2014-01-26 07:53:23 +08:00
*/
2014-03-07 06:52:50 +08:00
class rotating_file_sink : public base_sink
{
2014-01-25 17:09:04 +08:00
public:
2014-03-01 20:06:58 +08:00
rotating_file_sink(const std::string &base_filename, const std::string &extension,
const std::size_t max_size, const std::size_t max_files,
const std::size_t flush_every=0):
2014-02-22 04:51:54 +08:00
_base_filename(base_filename),
_extension(extension),
_max_size(max_size),
_max_files(max_files),
_current_size(0),
2014-02-22 16:34:42 +08:00
_mutex(),
2014-03-01 20:06:58 +08:00
_ofstream(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary),
_flush_helper(flush_every)
{
2014-02-22 04:51:54 +08:00
}
2014-01-25 17:09:04 +08:00
protected:
void _sink_it(const std::string& msg) override
{
2014-02-22 16:34:42 +08:00
std::lock_guard<std::mutex> lock(_mutex);
2014-02-22 04:51:54 +08:00
_current_size += msg.length();
if (_current_size > _max_size)
{
2014-02-22 04:51:54 +08:00
_rotate();
_current_size = msg.length();
}
2014-02-22 17:57:53 +08:00
_flush_helper.write(_ofstream, msg);
2014-02-22 04:51:54 +08:00
}
2014-01-25 17:09:04 +08:00
2014-01-26 07:53:23 +08:00
private:
static std::string _calc_filename(const std::string& filename, std::size_t index, const std::string& extension)
{
2014-02-22 04:51:54 +08:00
std::ostringstream oss;
if (index)
oss << filename << "." << index << "." << extension;
else
oss << filename << "." << extension;
return oss.str();
}
// Rotate old files:
// log.n-1.txt -> log.n.txt
// log n-2.txt -> log.n-1.txt
// ...
// log.txt -> log.1.txt
void _rotate()
{
2014-02-22 04:51:54 +08:00
_ofstream.close();
//Remove oldest file
for (auto i = _max_files; i > 0; --i)
{
2014-02-22 04:51:54 +08:00
auto src = _calc_filename(_base_filename, i - 1, _extension);
auto target = _calc_filename(_base_filename, i, _extension);
if (i == _max_files)
std::remove(target.c_str());
std::rename(src.c_str(), target.c_str());
}
_ofstream.open(_calc_filename(_base_filename, 0, _extension));
}
std::string _base_filename;
std::string _extension;
std::size_t _max_size;
std::size_t _max_files;
std::size_t _current_size;
2014-02-22 16:34:42 +08:00
std::mutex _mutex;
2014-02-22 04:51:54 +08:00
std::ofstream _ofstream;
2014-02-22 17:57:53 +08:00
details::file_flush_helper _flush_helper;
2014-01-25 17:09:04 +08:00
};
/*
* Thread safe, rotating file sink based on date. rotates at midnight
2014-01-25 17:09:04 +08:00
*/
2014-03-07 06:52:50 +08:00
class daily_file_sink:public base_sink
{
2014-01-25 17:09:04 +08:00
public:
2014-03-01 20:06:58 +08:00
explicit daily_file_sink(const std::string& base_filename,
const std::string& extension,
const std::size_t flush_every=0):
2014-02-22 04:51:54 +08:00
_base_filename(base_filename),
_extension(extension),
_midnight_tp (_calc_midnight_tp() ),
2014-02-22 16:34:42 +08:00
_mutex(),
2014-03-01 20:06:58 +08:00
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::binary|std::ofstream::app),
_flush_helper(flush_every)
{
2014-02-22 04:51:54 +08:00
}
2014-01-25 17:09:04 +08:00
protected:
void _sink_it(const std::string& msg) override
{
2014-02-22 16:34:42 +08:00
std::lock_guard<std::mutex> lock(_mutex);
if (std::chrono::system_clock::now() >= _midnight_tp)
{
2014-02-22 04:51:54 +08:00
_ofstream.close();
_ofstream.open(_calc_filename(_base_filename, _extension));
_midnight_tp = _calc_midnight_tp();
}
2014-02-22 17:57:53 +08:00
_flush_helper.write(_ofstream, msg);
2014-02-22 04:51:54 +08:00
}
2014-01-25 17:09:04 +08:00
private:
2014-02-22 04:51:54 +08:00
// Return next midnight's time_point
static std::chrono::system_clock::time_point _calc_midnight_tp()
{
2014-02-22 04:51:54 +08:00
using namespace std::chrono;
auto now = system_clock::now();
time_t tnow = std::chrono::system_clock::to_time_t(now);
tm date = c11log::details::os::localtime(tnow);
date.tm_hour = date.tm_min = date.tm_sec = 0;
auto midnight = std::chrono::system_clock::from_time_t(std::mktime(&date));
return system_clock::time_point(midnight + hours(24));
}
2014-03-07 06:52:50 +08:00
//Create filename for the form basename.YYYY-MM-DD.extension
static std::string _calc_filename(const std::string& basename, const std::string& extension)
{
2014-03-07 06:52:50 +08:00
std::tm tm = c11log::details::os::localtime();
std::ostringstream oss;
oss << basename << '.';
oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday;
oss << '.' << extension;
return oss.str();
2014-02-22 04:51:54 +08:00
}
std::string _base_filename;
std::string _extension;
std::chrono::system_clock::time_point _midnight_tp;
2014-02-22 16:34:42 +08:00
std::mutex _mutex;
2014-02-22 04:51:54 +08:00
std::ofstream _ofstream;
2014-03-01 20:06:58 +08:00
details::file_flush_helper _flush_helper;
2014-02-09 07:56:21 +08:00
2014-01-25 17:09:04 +08:00
};
}
2014-01-27 17:44:10 +08:00
}