flush helper count writes instead of time

This commit is contained in:
gabime
2014-03-14 14:35:46 +02:00
parent 9d687d1634
commit 38670cef27
14 changed files with 139 additions and 94 deletions

View File

@@ -67,9 +67,12 @@ inline void c11log::sinks::async_sink::_thread_loop()
static std::chrono::seconds pop_timeout { 1 };
std::string msg;
while (_active) {
if (_q.pop(msg, pop_timeout)) {
for (auto &sink : _sinks) {
while (_active)
{
if (_q.pop(msg, pop_timeout))
{
for (auto &sink : _sinks)
{
sink->log(msg, static_cast<level::level_enum>(_level.load()));
if (!_active)
return;
@@ -92,7 +95,8 @@ inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr)
inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &timeout)
{
auto until = std::chrono::system_clock::now() + timeout;
while (_q.size() > 0 && std::chrono::system_clock::now() < until) {
while (_q.size() > 0 && std::chrono::system_clock::now() < until)
{
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
_shutdown();
@@ -100,7 +104,8 @@ inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &time
inline void c11log::sinks::async_sink::_shutdown()
{
if(_active) {
if(_active)
{
_active = false;
if (_back_thread.joinable())
_back_thread.join();

View File

@@ -14,20 +14,24 @@ class base_sink
{
public:
base_sink() = default;
base_sink(level::level_enum l):_level(l) {
base_sink(level::level_enum l):_level(l)
{
};
virtual ~base_sink() = default;
base_sink(const base_sink&) = delete;
base_sink& operator=(const base_sink&) = delete;
void log(const std::string &msg, level::level_enum level) {
if (level >= _level) {
void log(const std::string &msg, level::level_enum level)
{
if (level >= _level)
{
_sink_it(msg);
}
};
void set_level(level::level_enum level) {
void set_level(level::level_enum level)
{
_level = level;
}
@@ -39,7 +43,8 @@ protected:
class null_sink:public base_sink
{
protected:
void _sink_it(const std::string& ) override {
void _sink_it(const std::string& ) override
{
}
};
}

View File

@@ -19,7 +19,8 @@ public:
virtual ~console_sink() = default;
protected:
virtual void _sink_it(const std::string& msg) override {
virtual void _sink_it(const std::string& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_ostream << msg;
}

View File

@@ -12,20 +12,22 @@ namespace sinks
{
/*
* Trivial file sink with single file as target
* Thread safe, trivial file sink with single file as target
*/
class simple_file_sink : public base_sink
{
public:
explicit simple_file_sink(const std::string &filename,
const std::string& extension,
const std::chrono::milliseconds &flush_every=std::chrono::milliseconds::zero())
const std::size_t flush_every=0)
: _mutex(),
_ofstream(filename + "." + extension, std::ofstream::binary|std::ofstream::app),
_flush_helper(flush_every) {
_flush_helper(flush_every)
{
}
protected:
void _sink_it(const std::string& msg) override {
void _sink_it(const std::string& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_flush_helper.write(_ofstream, msg);
}
@@ -37,14 +39,14 @@ private:
/*
* Thread safe, size limited file sink
* Thread safe, rotating file sink based on size
*/
class rotating_file_sink : public base_sink
{
public:
rotating_file_sink(const std::string &base_filename, const std::string &extension,
size_t max_size, size_t max_files,
const std::chrono::milliseconds &flush_every = std::chrono::milliseconds::zero()):
const std::size_t max_size, const std::size_t max_files,
const std::size_t flush_every=0):
_base_filename(base_filename),
_extension(extension),
_max_size(max_size),
@@ -52,14 +54,17 @@ public:
_current_size(0),
_mutex(),
_ofstream(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary),
_flush_helper(flush_every) {
_flush_helper(flush_every)
{
}
protected:
void _sink_it(const std::string& msg) override {
void _sink_it(const std::string& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_current_size += msg.length();
if (_current_size > _max_size) {
if (_current_size > _max_size)
{
_rotate();
_current_size = msg.length();
}
@@ -68,7 +73,8 @@ protected:
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)
{
std::ostringstream oss;
if (index)
oss << filename << "." << index << "." << extension;
@@ -83,10 +89,12 @@ private:
// log n-2.txt -> log.n-1.txt
// ...
// log.txt -> log.1.txt
void _rotate() {
void _rotate()
{
_ofstream.close();
//Remove oldest file
for (auto i = _max_files; i > 0; --i) {
for (auto i = _max_files; i > 0; --i)
{
auto src = _calc_filename(_base_filename, i - 1, _extension);
auto target = _calc_filename(_base_filename, i, _extension);
if (i == _max_files)
@@ -106,26 +114,29 @@ private:
};
/*
* Thread safe file sink that closes the log file at midnight and opens new one
* Thread safe, rotating file sink based on date. rotates at midnight
*/
class daily_file_sink:public base_sink
{
public:
explicit daily_file_sink(const std::string& base_filename,
const std::string& extension,
const std::chrono::milliseconds &flush_every = std::chrono::milliseconds::zero()):
const std::size_t flush_every=0):
_base_filename(base_filename),
_extension(extension),
_midnight_tp (_calc_midnight_tp() ),
_mutex(),
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::binary|std::ofstream::app),
_flush_helper(flush_every) {
_flush_helper(flush_every)
{
}
protected:
void _sink_it(const std::string& msg) override {
void _sink_it(const std::string& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
if (std::chrono::system_clock::now() >= _midnight_tp) {
if (std::chrono::system_clock::now() >= _midnight_tp)
{
_ofstream.close();
_ofstream.open(_calc_filename(_base_filename, _extension));
_midnight_tp = _calc_midnight_tp();
@@ -135,7 +146,8 @@ protected:
private:
// Return next midnight's time_point
static std::chrono::system_clock::time_point _calc_midnight_tp() {
static std::chrono::system_clock::time_point _calc_midnight_tp()
{
using namespace std::chrono;
auto now = system_clock::now();
time_t tnow = std::chrono::system_clock::to_time_t(now);
@@ -146,7 +158,8 @@ 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 std::string _calc_filename(const std::string& basename, const std::string& extension)
{
std::tm tm = c11log::details::os::localtime();
std::ostringstream oss;
oss << basename << '.';