This commit is contained in:
gabime
2014-02-21 22:51:54 +02:00
parent 2f6a5eabe0
commit c5a8eb5cdb
18 changed files with 496 additions and 600 deletions

View File

@@ -11,8 +11,7 @@
namespace c11log {
namespace sinks {
class async_sink : public base_sink
{
class async_sink : public base_sink {
public:
using size_type = c11log::details::blocking_queue<std::string>::size_type;
@@ -45,9 +44,9 @@ private:
///////////////////////////////////////////////////////////////////////////////
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size)
:sinks_(),
active_(true),
q_(max_queue_size),
:sinks_(),
active_(true),
q_(max_queue_size),
back_thread_(&async_sink::thread_loop_, this)
{}
@@ -62,15 +61,12 @@ inline void c11log::sinks::async_sink::sink_it_(const std::string& msg)
inline void c11log::sinks::async_sink::thread_loop_()
{
constexpr auto pop_timeout = std::chrono::seconds(1);
constexpr auto pop_timeout = std::chrono::seconds(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;
@@ -93,8 +89,7 @@ inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr_t 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_();
@@ -102,8 +97,7 @@ 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

@@ -11,34 +11,32 @@ namespace sinks {
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)
{
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;
}
protected:
virtual void sink_it_(const std::string& msg) = 0;
std::atomic<int> _level{level::INFO};
std::atomic<int> _level {level::INFO};
};
class null_sink:public base_sink {
protected:
void sink_it_(const std::string& ) override
{}
void sink_it_(const std::string& ) override {
}
};
}
}

View File

@@ -6,29 +6,26 @@
#include "base_sink.h"
namespace c11log
{
namespace sinks
{
namespace c11log {
namespace sinks {
/*
* Trivial file sink with single file as target
*/
class simple_file_sink : public base_sink
{
class simple_file_sink : public base_sink {
public:
explicit simple_file_sink(const std::string &filename, const std::string& extension = "txt")
: mutex_(),
_ofstream(filename + "." + extension, std::ofstream::app)
{}
explicit simple_file_sink(const std::string &filename, const std::string& extension = "txt")
: mutex_(),
_ofstream(filename + "." + extension, std::ofstream::app) {
}
protected:
void sink_it_(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_);
_ofstream << msg;
_ofstream.flush();
}
void sink_it_(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_);
_ofstream << msg;
_ofstream.flush();
}
private:
std::mutex mutex_;
std::ofstream _ofstream;
std::mutex mutex_;
std::ofstream _ofstream;
};
@@ -36,119 +33,117 @@ private:
/*
* Thread safe, size limited file sink
*/
class rotating_file_sink : public base_sink
{
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):
_base_filename(base_filename),
_extension(extension),
_max_size(max_size),
_max_files(max_files),
_current_size(0),
mutex_(),
_ofstream(_calc_filename(_base_filename, 0, _extension))
{}
rotating_file_sink(const std::string &base_filename, const std::string &extension, size_t max_size, size_t max_files):
_base_filename(base_filename),
_extension(extension),
_max_size(max_size),
_max_files(max_files),
_current_size(0),
mutex_(),
_ofstream(_calc_filename(_base_filename, 0, _extension)) {
}
protected:
void sink_it_(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_);
_current_size += msg.length();
if (_current_size > _max_size) {
_rotate();
_current_size = msg.length();
}
_ofstream << msg;
_ofstream.flush();
}
void sink_it_(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_);
_current_size += msg.length();
if (_current_size > _max_size) {
_rotate();
_current_size = msg.length();
}
_ofstream << msg;
_ofstream.flush();
}
private:
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;
else
oss << filename << "." << extension;
return oss.str();
}
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;
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() {
_ofstream.close();
//Remove oldest file
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)
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;
std::mutex mutex_;
std::ofstream _ofstream;
// 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() {
_ofstream.close();
//Remove oldest file
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)
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;
std::mutex mutex_;
std::ofstream _ofstream;
};
/*
* Thread safe file sink that closes the log file at midnight and opens new one
*/
class daily_file_sink:public base_sink
{
class daily_file_sink:public base_sink {
public:
explicit daily_file_sink(const std::string& base_filename, const std::string& extension = "txt"):
_base_filename(base_filename),
_extension(extension),
_midnight_tp (_calc_midnight_tp() ),
mutex_(),
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::app)
{}
explicit daily_file_sink(const std::string& base_filename, const std::string& extension = "txt"):
_base_filename(base_filename),
_extension(extension),
_midnight_tp (_calc_midnight_tp() ),
mutex_(),
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::app) {
}
protected:
void sink_it_(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_);
if (std::chrono::system_clock::now() >= _midnight_tp) {
_ofstream.close();
_ofstream.open(_calc_filename(_base_filename, _extension));
_midnight_tp = _calc_midnight_tp();
}
_ofstream << msg;
_ofstream.flush();
}
void sink_it_(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_);
if (std::chrono::system_clock::now() >= _midnight_tp) {
_ofstream.close();
_ofstream.open(_calc_filename(_base_filename, _extension));
_midnight_tp = _calc_midnight_tp();
}
_ofstream << msg;
_ofstream.flush();
}
private:
// Return next midnight's time_point
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);
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));
}
// Return next midnight's time_point
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);
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));
}
static std::string _calc_filename(const std::string& basename, const std::string& extension) {
std::tm tm = c11log::details::os::localtime();
char buf[32];
sprintf(buf, ".%d-%02d-%02d.", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
return basename+buf+extension;
}
static std::string _calc_filename(const std::string& basename, const std::string& extension) {
std::tm tm = c11log::details::os::localtime();
char buf[32];
sprintf(buf, ".%d-%02d-%02d.", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
return basename+buf+extension;
}
std::string _base_filename;
std::string _extension;
std::chrono::system_clock::time_point _midnight_tp;
std::mutex mutex_;
std::ofstream _ofstream;
std::string _base_filename;
std::string _extension;
std::chrono::system_clock::time_point _midnight_tp;
std::mutex mutex_;
std::ofstream _ofstream;
};
}

View File

@@ -3,36 +3,30 @@
#include <iostream>
#include "base_sink.h"
namespace c11log
{
namespace sinks
{
class ostream_sink: public base_sink
{
namespace c11log {
namespace sinks {
class ostream_sink: public base_sink {
public:
ostream_sink(std::ostream& os):_ostream(os) {}
ostream_sink(std::ostream& os):_ostream(os) {}
virtual ~ostream_sink() = default;
protected:
virtual void sink_it_(const std::string& msg) override
{
_ostream << msg;
}
virtual void sink_it_(const std::string& msg) override {
_ostream << msg;
}
std::ostream& _ostream;
std::ostream& _ostream;
};
class stdout_sink:public ostream_sink
{
class stdout_sink:public ostream_sink {
public:
stdout_sink():ostream_sink(std::cout) {}
};
class stderr_sink:public ostream_sink
{
class stderr_sink:public ostream_sink {
public:
stderr_sink():ostream_sink(std::cerr) {}
};
}
}