Commit 31a011e6 authored by gabime's avatar gabime

Fixed daily sink syntax error and redundant file close

parent 53830726
...@@ -30,21 +30,19 @@ ...@@ -30,21 +30,19 @@
#include "../details/file_helper.h" #include "../details/file_helper.h"
#include "../details/format.h" #include "../details/format.h"
namespace spdlog namespace spdlog
{ {
namespace sinks namespace sinks
{ {
/* /*
* Trivial file sink with single file as target * Trivial file sink with single file as target
*/ */
template<class Mutex> template<class Mutex>
class simple_file_sink : public base_sink<Mutex> class simple_file_sink : public base_sink < Mutex >
{ {
public: public:
explicit simple_file_sink(const std::string &filename, explicit simple_file_sink(const std::string &filename,
bool force_flush=false): bool force_flush = false) :
_file_helper(force_flush) _file_helper(force_flush)
{ {
_file_helper.open(filename); _file_helper.open(filename);
...@@ -64,14 +62,14 @@ typedef simple_file_sink<details::null_mutex> simple_file_sink_st; ...@@ -64,14 +62,14 @@ typedef simple_file_sink<details::null_mutex> simple_file_sink_st;
/* /*
* Rotating file sink based on size * Rotating file sink based on size
*/ */
template<class Mutex> template<class Mutex>
class rotating_file_sink : public base_sink<Mutex> class rotating_file_sink : public base_sink < Mutex >
{ {
public: public:
rotating_file_sink(const std::string &base_filename, const std::string &extension, rotating_file_sink(const std::string &base_filename, const std::string &extension,
std::size_t max_size, std::size_t max_files, std::size_t max_size, std::size_t max_files,
bool force_flush=false): bool force_flush = false) :
_base_filename(base_filename), _base_filename(base_filename),
_extension(extension), _extension(extension),
_max_size(max_size), _max_size(max_size),
...@@ -82,12 +80,11 @@ public: ...@@ -82,12 +80,11 @@ public:
_file_helper.open(calc_filename(_base_filename, 0, _extension)); _file_helper.open(calc_filename(_base_filename, 0, _extension));
} }
protected: protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
{ {
_current_size += msg.formatted.size(); _current_size += msg.formatted.size();
if (_current_size > _max_size) if (_current_size > _max_size)
{ {
_rotate(); _rotate();
_current_size = msg.formatted.size(); _current_size = msg.formatted.size();
...@@ -95,7 +92,6 @@ protected: ...@@ -95,7 +92,6 @@ protected:
_file_helper.write(msg); _file_helper.write(msg);
} }
private: private:
static std::string calc_filename(const std::string& filename, std::size_t index, const std::string& extension) static std::string calc_filename(const std::string& filename, std::size_t index, const std::string& extension)
{ {
...@@ -107,14 +103,12 @@ private: ...@@ -107,14 +103,12 @@ private:
return w.str(); return w.str();
} }
// Rotate files: // Rotate files:
// log.txt -> log.1.txt // log.txt -> log.1.txt
// log.1.txt -> log2.txt // log.1.txt -> log2.txt
// log.2.txt -> log3.txt // log.2.txt -> log3.txt
// log.3.txt -> delete // log.3.txt -> delete
void _rotate() void _rotate()
{ {
_file_helper.close(); _file_helper.close();
...@@ -152,7 +146,7 @@ typedef rotating_file_sink<details::null_mutex>rotating_file_sink_st; ...@@ -152,7 +146,7 @@ typedef rotating_file_sink<details::null_mutex>rotating_file_sink_st;
* Rotating file sink based on date. rotates at midnight * Rotating file sink based on date. rotates at midnight
*/ */
template<class Mutex> template<class Mutex>
class daily_file_sink:public base_sink<Mutex> class daily_file_sink :public base_sink < Mutex >
{ {
public: public:
//create daily file sink which rotates on given time //create daily file sink which rotates on given time
...@@ -161,7 +155,7 @@ public: ...@@ -161,7 +155,7 @@ public:
const std::string& extension, const std::string& extension,
int rotation_hour, int rotation_hour,
int rotation_minute, int rotation_minute,
bool force_flush=false): _base_filename(base_filename), bool force_flush = false) : _base_filename(base_filename),
_extension(extension), _extension(extension),
_rotation_h(rotation_hour), _rotation_h(rotation_hour),
_rotation_m(rotation_minute), _rotation_m(rotation_minute),
...@@ -169,17 +163,15 @@ public: ...@@ -169,17 +163,15 @@ public:
{ {
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); 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)); _file_helper.open(calc_filename(_base_filename, _extension));
} }
protected: protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
{ {
if (std::chrono::system_clock::now() >= _rotation_tp) if (std::chrono::system_clock::now() >= _rotation_tp)
{ {
_file_helper.close();
_file_helper.open(calc_filename(_base_filename, _extension)); _file_helper.open(calc_filename(_base_filename, _extension));
_rotation_tp = _next_rotation_tp(); _rotation_tp = _next_rotation_tp();
} }
...@@ -218,8 +210,6 @@ private: ...@@ -218,8 +210,6 @@ private:
int _rotation_m; int _rotation_m;
std::chrono::system_clock::time_point _rotation_tp; std::chrono::system_clock::time_point _rotation_tp;
details::file_helper _file_helper; details::file_helper _file_helper;
}; };
typedef daily_file_sink<std::mutex> daily_file_sink_mt; typedef daily_file_sink<std::mutex> daily_file_sink_mt;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment