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 "c11log/logger.h"
@ -10,84 +10,63 @@
#include "utils.h"
std::atomic<uint64_t> push_count, pop_count;
std::atomic<uint64_t> log_count;
std::atomic<bool> active;
using std::string;
using std::chrono::seconds;
using Q = c11log::details::blocking_queue<string>;
void pusher(Q* )
void logging_thread()
{
auto &logger = c11log::get_logger("async");
while(active)
{
while(active) {
logger.info()<<"Hello logger!";
++push_count;
++log_count;
}
}
void testq(int size, int pushers /*int poppers*/)
void testlog(int threads)
{
active = true;
Q q{static_cast<Q::size_type>(size)};
/*
for(int i = 0; i < poppers; i++)
testq(qsize, pushers, poppers);
*/
for(int i = 0; i < pushers; i++)
new std::thread(std::bind(pusher, &q));
for(int i = 0; i < threads; i++)
new std::thread(std::bind(logging_thread));
while(active)
{
while(active) {
using std::endl;
using std::cout;
using utils::format;
push_count = 0;
pop_count = 0;
log_count = 0;
std::this_thread::sleep_for(seconds(1));
cout << "Pushes/sec =\t" << format(push_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;
cout << "Logs/sec =\t" << format(log_count.load()) << endl;
}
}
int main(int argc, char* argv[])
{
using namespace std::chrono;
if(argc !=4)
{
std::cerr << "Usage: " << argv[0] << " qsize, pushers, poppers" << std::endl;
if(argc !=3) {
std::cerr << "Usage: " << argv[0] << " qsize, threads" << std::endl;
return 0;
}
int qsize = atoi(argv[1]);
int pushers = 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);
int threads = atoi(argv[2]);
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);
auto &logger = c11log::get_logger("async");
logger.add_sink(async);
testq(qsize, pushers);
testlog(threads);
}

View File

@ -7,8 +7,7 @@
#include <iomanip>
#include <locale>
namespace utils
{
namespace utils {
template<typename T>
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);
auto start_time = the_clock::now();
auto lastPrintTime = start_time;
while (true)
{
while (true) {
fn();
++counter;
auto now = the_clock::now();
if (now - start_time >= duration)
break;
if (now - lastPrintTime >= print_interval)
{
if (now - lastPrintTime >= print_interval) {
std::cout << fn_name << ": " << format(counter) << " per sec" << std::endl;
counter = 0;
lastPrintTime = the_clock::now();

View File

@ -10,14 +10,11 @@
#include <mutex>
#include <condition_variable>
namespace c11log
{
namespace details
{
namespace c11log {
namespace details {
template<typename T>
class blocking_queue
{
class blocking_queue {
public:
using queue_t = std::queue<T>;
using size_type = typename queue_t::size_type;
@ -26,8 +23,8 @@ public:
explicit blocking_queue(size_type max_size) :
max_size_(max_size),
q_(),
mutex_()
{}
mutex_() {
}
blocking_queue(const blocking_queue&) = delete;
blocking_queue& operator=(const blocking_queue&) = delete;
~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);
_loggers.insert(std::make_pair(name, new_logger_ptr));
return new_logger_ptr;
}
else {
} else {
return found->second;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -28,8 +28,7 @@ public:
class default_formatter: public formatter {
public:
// 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);
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];
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));
sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900,
tm.tm_mon + 1,

View File

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

View File

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

View File

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

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;
@ -65,12 +64,9 @@ inline void c11log::sinks::async_sink::thread_loop_()
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,20 +6,17 @@
#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)
{}
_ofstream(filename + "." + extension, std::ofstream::app) {
}
protected:
void sink_it_(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_);
@ -36,8 +33,7 @@ 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),
@ -46,8 +42,8 @@ public:
_max_files(max_files),
_current_size(0),
mutex_(),
_ofstream(_calc_filename(_base_filename, 0, _extension))
{}
_ofstream(_calc_filename(_base_filename, 0, _extension)) {
}
protected:
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
*/
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)
{}
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::app) {
}
protected:
void sink_it_(const std::string& msg) override {

View File

@ -3,33 +3,27 @@
#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) {}
virtual ~ostream_sink() = default;
protected:
virtual void sink_it_(const std::string& msg) override
{
virtual void sink_it_(const std::string& msg) override {
_ostream << msg;
}
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) {}

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