mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
Fixed daily sink syntax error and redundant file close
This commit is contained in:
parent
53830726b4
commit
31a011e67c
@ -30,21 +30,19 @@
|
||||
#include "../details/file_helper.h"
|
||||
#include "../details/format.h"
|
||||
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
|
||||
/*
|
||||
* Trivial file sink with single file as target
|
||||
*/
|
||||
template<class Mutex>
|
||||
class simple_file_sink : public base_sink<Mutex>
|
||||
class simple_file_sink : public base_sink < Mutex >
|
||||
{
|
||||
public:
|
||||
explicit simple_file_sink(const std::string &filename,
|
||||
bool force_flush=false):
|
||||
bool force_flush = false) :
|
||||
_file_helper(force_flush)
|
||||
{
|
||||
_file_helper.open(filename);
|
||||
@ -64,14 +62,14 @@ typedef simple_file_sink<details::null_mutex> simple_file_sink_st;
|
||||
|
||||
/*
|
||||
* Rotating file sink based on size
|
||||
*/
|
||||
*/
|
||||
template<class Mutex>
|
||||
class rotating_file_sink : public base_sink<Mutex>
|
||||
class rotating_file_sink : public base_sink < Mutex >
|
||||
{
|
||||
public:
|
||||
rotating_file_sink(const std::string &base_filename, const std::string &extension,
|
||||
std::size_t max_size, std::size_t max_files,
|
||||
bool force_flush=false):
|
||||
bool force_flush = false) :
|
||||
_base_filename(base_filename),
|
||||
_extension(extension),
|
||||
_max_size(max_size),
|
||||
@ -82,12 +80,11 @@ public:
|
||||
_file_helper.open(calc_filename(_base_filename, 0, _extension));
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
{
|
||||
_current_size += msg.formatted.size();
|
||||
if (_current_size > _max_size)
|
||||
if (_current_size > _max_size)
|
||||
{
|
||||
_rotate();
|
||||
_current_size = msg.formatted.size();
|
||||
@ -95,7 +92,6 @@ protected:
|
||||
_file_helper.write(msg);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
static std::string calc_filename(const std::string& filename, std::size_t index, const std::string& extension)
|
||||
{
|
||||
@ -107,14 +103,12 @@ private:
|
||||
return w.str();
|
||||
}
|
||||
|
||||
|
||||
// Rotate files:
|
||||
// log.txt -> log.1.txt
|
||||
// log.1.txt -> log2.txt
|
||||
// log.2.txt -> log3.txt
|
||||
// log.3.txt -> delete
|
||||
|
||||
|
||||
void _rotate()
|
||||
{
|
||||
_file_helper.close();
|
||||
@ -152,7 +146,7 @@ typedef rotating_file_sink<details::null_mutex>rotating_file_sink_st;
|
||||
* Rotating file sink based on date. rotates at midnight
|
||||
*/
|
||||
template<class Mutex>
|
||||
class daily_file_sink:public base_sink<Mutex>
|
||||
class daily_file_sink :public base_sink < Mutex >
|
||||
{
|
||||
public:
|
||||
//create daily file sink which rotates on given time
|
||||
@ -161,7 +155,7 @@ public:
|
||||
const std::string& extension,
|
||||
int rotation_hour,
|
||||
int rotation_minute,
|
||||
bool force_flush=false): _base_filename(base_filename),
|
||||
bool force_flush = false) : _base_filename(base_filename),
|
||||
_extension(extension),
|
||||
_rotation_h(rotation_hour),
|
||||
_rotation_m(rotation_minute),
|
||||
@ -169,17 +163,15 @@ public:
|
||||
{
|
||||
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(),
|
||||
_rotation_tp = _next_rotation_tp();
|
||||
_file_helper.open(calc_filename(_base_filename, _extension));
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
{
|
||||
if (std::chrono::system_clock::now() >= _rotation_tp)
|
||||
{
|
||||
_file_helper.close();
|
||||
_file_helper.open(calc_filename(_base_filename, _extension));
|
||||
_rotation_tp = _next_rotation_tp();
|
||||
}
|
||||
@ -218,8 +210,6 @@ private:
|
||||
int _rotation_m;
|
||||
std::chrono::system_clock::time_point _rotation_tp;
|
||||
details::file_helper _file_helper;
|
||||
|
||||
|
||||
};
|
||||
|
||||
typedef daily_file_sink<std::mutex> daily_file_sink_mt;
|
||||
|
Loading…
Reference in New Issue
Block a user