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

3
astyle.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
find . -name "*\.h" -o -name "*\.cpp"|xargs astyle --style=stroustrup

View File

@ -1,6 +1,6 @@
// test.cpp : Defines the entry point for the console application. // example.cpp : Simple logger example
// //
#include "stdafx.h" #include <string>
#include <functional> #include <functional>
#include "c11log/logger.h" #include "c11log/logger.h"
@ -10,84 +10,63 @@
#include "utils.h" #include "utils.h"
std::atomic<uint64_t> log_count;
std::atomic<uint64_t> push_count, pop_count;
std::atomic<bool> active; std::atomic<bool> active;
using std::string;
using std::chrono::seconds; using std::chrono::seconds;
using Q = c11log::details::blocking_queue<string>;
void pusher(Q* ) void logging_thread()
{ {
auto &logger = c11log::get_logger("async"); auto &logger = c11log::get_logger("async");
while(active) while(active) {
{
logger.info()<<"Hello logger!"; logger.info()<<"Hello logger!";
++push_count; ++log_count;
} }
} }
void testq(int size, int pushers /*int poppers*/) void testlog(int threads)
{ {
active = true; active = true;
Q q{static_cast<Q::size_type>(size)};
/* for(int i = 0; i < threads; i++)
for(int i = 0; i < poppers; i++) new std::thread(std::bind(logging_thread));
testq(qsize, pushers, poppers);
*/
for(int i = 0; i < pushers; i++)
new std::thread(std::bind(pusher, &q));
while(active) while(active) {
{
using std::endl; using std::endl;
using std::cout; using std::cout;
using utils::format; using utils::format;
push_count = 0; log_count = 0;
pop_count = 0;
std::this_thread::sleep_for(seconds(1)); std::this_thread::sleep_for(seconds(1));
cout << "Pushes/sec =\t" << format(push_count.load()) << endl; cout << "Logs/sec =\t" << format(log_count.load()) << endl;
//cout << "Pops/sec =\t" << format(pop_count.load()) << endl << endl;
//cout << "Total/sec =\t" << format(push_count+pop_count) << endl;
cout << "Queue size =\t" << format(q.size()) << endl;
cout << "---------------------------------------------------------------------" << endl;
} }
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
using namespace std::chrono;
if(argc !=4) if(argc !=3) {
{ std::cerr << "Usage: " << argv[0] << " qsize, threads" << std::endl;
std::cerr << "Usage: " << argv[0] << " qsize, pushers, poppers" << std::endl;
return 0; return 0;
} }
int qsize = atoi(argv[1]); int qsize = atoi(argv[1]);
int pushers = atoi(argv[2]); int threads = atoi(argv[2]);
using namespace std::chrono;
auto null_sink = std::make_shared<c11log::sinks::null_sink>();
auto stdout_sink = std::make_shared<c11log::sinks::stdout_sink>();
auto async = std::make_shared<c11log::sinks::async_sink>(1000);
auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5);
using namespace c11log::sinks;
auto null_sink = std::make_shared<null_sink>();
auto stdout_sink = std::make_shared<stdout_sink>();
auto async = std::make_shared<async_sink>(qsize);
auto fsink = std::make_shared<rotating_file_sink>("example_log", "txt", 1024*1024*50 , 5);
async->add_sink(fsink); async->add_sink(fsink);
auto &logger = c11log::get_logger("async"); auto &logger = c11log::get_logger("async");
logger.add_sink(async); logger.add_sink(async);
testq(qsize, pushers); testlog(threads);
} }

View File

@ -7,8 +7,7 @@
#include <iomanip> #include <iomanip>
#include <locale> #include <locale>
namespace utils namespace utils {
{
template<typename T> template<typename T>
std::string format(const T& value) std::string format(const T& value)
@ -28,15 +27,13 @@ inline void bench(const std::string& fn_name, const std::chrono::milliseconds &d
seconds print_interval(1); seconds print_interval(1);
auto start_time = the_clock::now(); auto start_time = the_clock::now();
auto lastPrintTime = start_time; auto lastPrintTime = start_time;
while (true) while (true) {
{
fn(); fn();
++counter; ++counter;
auto now = the_clock::now(); auto now = the_clock::now();
if (now - start_time >= duration) if (now - start_time >= duration)
break; break;
if (now - lastPrintTime >= print_interval) if (now - lastPrintTime >= print_interval) {
{
std::cout << fn_name << ": " << format(counter) << " per sec" << std::endl; std::cout << fn_name << ": " << format(counter) << " per sec" << std::endl;
counter = 0; counter = 0;
lastPrintTime = the_clock::now(); lastPrintTime = the_clock::now();

View File

@ -10,14 +10,11 @@
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
namespace c11log namespace c11log {
{ namespace details {
namespace details
{
template<typename T> template<typename T>
class blocking_queue class blocking_queue {
{
public: public:
using queue_t = std::queue<T>; using queue_t = std::queue<T>;
using size_type = typename queue_t::size_type; using size_type = typename queue_t::size_type;
@ -26,8 +23,8 @@ public:
explicit blocking_queue(size_type max_size) : explicit blocking_queue(size_type max_size) :
max_size_(max_size), max_size_(max_size),
q_(), q_(),
mutex_() mutex_() {
{} }
blocking_queue(const blocking_queue&) = delete; blocking_queue(const blocking_queue&) = delete;
blocking_queue& operator=(const blocking_queue&) = delete; blocking_queue& operator=(const blocking_queue&) = delete;
~blocking_queue() = default; ~blocking_queue() = default;

View File

@ -30,8 +30,7 @@ inline c11log::details::factory::logger_ptr c11log::details::factory::get_logger
auto new_logger_ptr = std::make_shared<c11log::logger>(name); auto new_logger_ptr = std::make_shared<c11log::logger>(name);
_loggers.insert(std::make_pair(name, new_logger_ptr)); _loggers.insert(std::make_pair(name, new_logger_ptr));
return new_logger_ptr; return new_logger_ptr;
} } else {
else {
return found->second; return found->second;
} }
} }

View File

@ -2,19 +2,15 @@
#include<streambuf> #include<streambuf>
#include<string> #include<string>
namespace c11log namespace c11log {
{ namespace details {
namespace details
{
class str_devicebuf:public std::streambuf class str_devicebuf:public std::streambuf {
{
public: public:
str_devicebuf() = default; str_devicebuf() = default;
~str_devicebuf() = default; ~str_devicebuf() = default;
str_devicebuf(const str_devicebuf& other):std::streambuf(),_str(other._str) {} str_devicebuf(const str_devicebuf& other):std::streambuf(),_str(other._str) {}
str_devicebuf& operator=(const str_devicebuf other) str_devicebuf& operator=(const str_devicebuf other) {
{
if(this != &other) if(this != &other)
_str = other._str; _str = other._str;
return *this; return *this;
@ -47,31 +43,26 @@ private:
std::string _str; std::string _str;
}; };
class fast_oss:public std::ostream class fast_oss:public std::ostream {
{
public: public:
fast_oss():std::ostream(&_dev) {} fast_oss():std::ostream(&_dev) {}
~fast_oss() = default; ~fast_oss() = default;
fast_oss(const fast_oss& other):std::basic_ios<char>(), std::ostream(),_dev(other._dev) {} fast_oss(const fast_oss& other):std::basic_ios<char>(), std::ostream(),_dev(other._dev) {}
fast_oss& operator=(const fast_oss& other) fast_oss& operator=(const fast_oss& other) {
{
if(&other != this) if(&other != this)
_dev = other._dev; _dev = other._dev;
return *this; return *this;
} }
const std::string& str_ref() const const std::string& str_ref() const {
{
return _dev.str_ref(); return _dev.str_ref();
} }
const std::string str() const const std::string str() const {
{
return _dev.str_ref(); return _dev.str_ref();
} }
void clear() void clear() {
{
_dev.clear(); _dev.clear();
} }
private: private:

View File

@ -4,14 +4,11 @@
#include "../logger.h" #include "../logger.h"
#include "fast_oss.h" #include "fast_oss.h"
namespace c11log namespace c11log {
{
class logger; class logger;
namespace details namespace details {
{
class line_logger class line_logger {
{
public: public:
line_logger(logger* callback_logger, level::level_enum msg_level): line_logger(logger* callback_logger, level::level_enum msg_level):
_callback_logger(callback_logger), _callback_logger(callback_logger),

View File

@ -1,17 +1,15 @@
#pragma once #pragma once
namespace c11log{ namespace c11log {
namespace details{ namespace details {
struct null_mutex struct null_mutex {
{ void lock() {
void lock() }
{} void unlock() {
void unlock() }
{} bool try_lock() {
bool try_lock()
{
return true; return true;
} }
}; };
} }
} }

View File

@ -3,17 +3,14 @@
#include<cstdio> #include<cstdio>
#include<ctime> #include<ctime>
namespace c11log namespace c11log {
{ namespace details {
namespace details namespace os {
{ std::tm localtime(const std::time_t &time_tt);
namespace os std::tm localtime();
{
std::tm localtime(const std::time_t &time_tt);
std::tm localtime();
} }
} }
} }

View File

@ -28,8 +28,7 @@ public:
class default_formatter: public formatter { class default_formatter: public formatter {
public: public:
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body // Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body
void format_header(const std::string& logger_name, level::level_enum level, const time_point& tp, std::ostream& dest) override void format_header(const std::string& logger_name, level::level_enum level, const time_point& tp, std::ostream& dest) override {
{
_format_time(tp, dest); _format_time(tp, dest);
dest << " [" << logger_name << ":" << c11log::level::to_str(level) << "] "; dest << " [" << logger_name << ":" << c11log::level::to_str(level) << "] ";
} }
@ -50,8 +49,7 @@ inline void c11log::formatters::default_formatter::_format_time(const time_point
static thread_local char timestamp_cache[64]; static thread_local char timestamp_cache[64];
if(duration_cast<milliseconds>(tp-last_tp).count() > 950) if(duration_cast<milliseconds>(tp-last_tp).count() > 950) {
{
auto tm = details::os::localtime(clock::to_time_t(tp)); auto tm = details::os::localtime(clock::to_time_t(tp));
sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900, sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900,
tm.tm_mon + 1, tm.tm_mon + 1,

View File

@ -1,20 +1,17 @@
#pragma once #pragma once
namespace c11log namespace c11log {
{ namespace level {
namespace level typedef enum {
{
typedef enum
{
DEBUG, DEBUG,
INFO, INFO,
WARNING, WARNING,
ERROR, ERROR,
FATAL, FATAL,
NONE = 99 NONE = 99
} level_enum; } level_enum;
const char* to_str(level_enum l); const char* to_str(level_enum l);
} }
} }
static const char* level_names[] { "Debug", "Info", "Warning", "Error", "Fatal" }; static const char* level_names[] { "Debug", "Info", "Warning", "Error", "Fatal" };

View File

@ -2,45 +2,40 @@
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
namespace c11log namespace c11log {
{ class log_exception :public std::exception {
class log_exception :public std::exception public:
{ log_exception() : _oss(), _msg() {
public: }
log_exception() : _oss(), _msg()
{}
virtual ~log_exception() virtual ~log_exception() {
{} }
explicit log_exception(const std::string& msg) :_oss(msg, std::ostringstream::ate), _msg(msg) explicit log_exception(const std::string& msg) :_oss(msg, std::ostringstream::ate), _msg(msg) {
{} }
log_exception(const log_exception &other) :_oss(other._oss.str()), _msg(other._msg) log_exception(const log_exception &other) :_oss(other._oss.str()), _msg(other._msg) {
{} }
log_exception& operator=(const log_exception& other) log_exception& operator=(const log_exception& other) {
{
_oss.str(other._oss.str()); _oss.str(other._oss.str());
_msg = other._msg; _msg = other._msg;
return *this; return *this;
} }
virtual const char* what() const throw () override virtual const char* what() const throw () override {
{
return _msg.c_str(); return _msg.c_str();
} }
template<typename T> template<typename T>
log_exception& operator<<(const T& what) log_exception& operator<<(const T& what) {
{
_oss << what; _oss << what;
_msg = _oss.str(); _msg = _oss.str();
return *this; return *this;
} }
private: private:
std::ostringstream _oss; std::ostringstream _oss;
std::string _msg; std::string _msg;
}; };
} }

View File

@ -12,15 +12,12 @@
#include "sinks/base_sink.h" #include "sinks/base_sink.h"
#include "details/factory.h" #include "details/factory.h"
namespace c11log namespace c11log {
{ namespace details {
namespace details
{
class line_logger; class line_logger;
} }
class logger class logger {
{
public: public:
typedef std::shared_ptr<sinks::base_sink> sink_ptr_t; typedef std::shared_ptr<sinks::base_sink> sink_ptr_t;
@ -28,11 +25,11 @@ public:
explicit logger(const std::string& name) : explicit logger(const std::string& name) :
logger_name_(name), logger_name_(name),
formatter_(std::make_unique<formatters::default_formatter>()), formatter_(new formatters::default_formatter()),
sinks_(), sinks_(),
mutex_(), mutex_(),
atomic_level_(level::INFO) atomic_level_(level::INFO) {
{} }
~logger() = default; ~logger() = default;
@ -71,19 +68,17 @@ private:
logger& get_logger(const std::string& name); logger& get_logger(const std::string& name);
} }
#include "details/line_logger.h"
// //
// Logger inline impl // Logger inline impl
// //
#include "details/line_logger.h"
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level) inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{ {
if (msg_level >= atomic_level_) { if (msg_level >= atomic_level_)
return details::line_logger(this, msg_level); return details::line_logger(this, msg_level);
} else { else
return details::line_logger(nullptr); return details::line_logger(nullptr);
}
} }
inline c11log::details::line_logger c11log::logger::debug() inline c11log::details::line_logger c11log::logger::debug()

View File

@ -11,8 +11,7 @@
namespace c11log { namespace c11log {
namespace sinks { namespace sinks {
class async_sink : public base_sink class async_sink : public base_sink {
{
public: public:
using size_type = c11log::details::blocking_queue<std::string>::size_type; using size_type = c11log::details::blocking_queue<std::string>::size_type;
@ -65,12 +64,9 @@ 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; std::string msg;
while (active_) while (active_) {
{ if (q_.pop(msg, pop_timeout)) {
if (q_.pop(msg, pop_timeout)) for (auto &sink : sinks_) {
{
for (auto &sink : sinks_)
{
sink->log(msg, static_cast<level::level_enum>(_level.load())); sink->log(msg, static_cast<level::level_enum>(_level.load()));
if (!active_) if (!active_)
return; 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) inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &timeout)
{ {
auto until = std::chrono::system_clock::now() + 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)); std::this_thread::sleep_for(std::chrono::milliseconds(200));
} }
shutdown_(); shutdown_();
@ -102,8 +97,7 @@ inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &time
inline void c11log::sinks::async_sink::shutdown_() inline void c11log::sinks::async_sink::shutdown_()
{ {
if(active_) if(active_) {
{
active_ = false; active_ = false;
if (back_thread_.joinable()) if (back_thread_.joinable())
back_thread_.join(); back_thread_.join();

View File

@ -11,34 +11,32 @@ namespace sinks {
class base_sink { class base_sink {
public: public:
base_sink() = default; base_sink() = default;
base_sink(level::level_enum l):_level(l) base_sink(level::level_enum l):_level(l) {
{}; };
virtual ~base_sink() = default; virtual ~base_sink() = default;
base_sink(const base_sink&) = delete; base_sink(const base_sink&) = delete;
base_sink& operator=(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) { if (level >= _level) {
sink_it_(msg); sink_it_(msg);
} }
}; };
void set_level(level::level_enum level) void set_level(level::level_enum level) {
{
_level = level; _level = level;
} }
protected: protected:
virtual void sink_it_(const std::string& msg) = 0; 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 { class null_sink:public base_sink {
protected: protected:
void sink_it_(const std::string& ) override void sink_it_(const std::string& ) override {
{} }
}; };
} }
} }

View File

@ -6,20 +6,17 @@
#include "base_sink.h" #include "base_sink.h"
namespace c11log namespace c11log {
{ namespace sinks {
namespace sinks
{
/* /*
* Trivial file sink with single file as target * Trivial file sink with single file as target
*/ */
class simple_file_sink : public base_sink class simple_file_sink : public base_sink {
{
public: public:
explicit simple_file_sink(const std::string &filename, const std::string& extension = "txt") explicit simple_file_sink(const std::string &filename, const std::string& extension = "txt")
: mutex_(), : mutex_(),
_ofstream(filename + "." + extension, std::ofstream::app) _ofstream(filename + "." + extension, std::ofstream::app) {
{} }
protected: protected:
void sink_it_(const std::string& msg) override { void sink_it_(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@ -36,8 +33,7 @@ private:
/* /*
* Thread safe, size limited file sink * Thread safe, size limited file sink
*/ */
class rotating_file_sink : public base_sink class rotating_file_sink : public base_sink {
{
public: public:
rotating_file_sink(const std::string &base_filename, const std::string &extension, size_t max_size, size_t max_files): rotating_file_sink(const std::string &base_filename, const std::string &extension, size_t max_size, size_t max_files):
_base_filename(base_filename), _base_filename(base_filename),
@ -46,8 +42,8 @@ public:
_max_files(max_files), _max_files(max_files),
_current_size(0), _current_size(0),
mutex_(), mutex_(),
_ofstream(_calc_filename(_base_filename, 0, _extension)) _ofstream(_calc_filename(_base_filename, 0, _extension)) {
{} }
protected: protected:
void sink_it_(const std::string& msg) override { void sink_it_(const std::string& msg) override {
@ -102,16 +98,15 @@ private:
/* /*
* Thread safe file sink that closes the log file at midnight and opens new one * 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: public:
explicit daily_file_sink(const std::string& base_filename, const std::string& extension = "txt"): explicit daily_file_sink(const std::string& base_filename, const std::string& extension = "txt"):
_base_filename(base_filename), _base_filename(base_filename),
_extension(extension), _extension(extension),
_midnight_tp (_calc_midnight_tp() ), _midnight_tp (_calc_midnight_tp() ),
mutex_(), mutex_(),
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::app) _ofstream(_calc_filename(_base_filename, _extension), std::ofstream::app) {
{} }
protected: protected:
void sink_it_(const std::string& msg) override { void sink_it_(const std::string& msg) override {

View File

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

View File

@ -1,28 +0,0 @@
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#ifdef _MSC_VER
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif
#include <string>
#include <chrono>
#include <ctime>
#include <memory>
#include <iostream>
#ifndef _MSC_VER
namespace std
{
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}
}
#endif