astyle+dos2unix

This commit is contained in:
gabime
2014-03-22 14:11:17 +02:00
parent 6a83c34426
commit 8494590fb3
15 changed files with 1130 additions and 1129 deletions

View File

@@ -1,116 +1,116 @@
#pragma once
#include <thread>
#include <chrono>
#include <atomic>
#include "base_sink.h"
#include "../logger.h"
#include "../details/blocking_queue.h"
namespace c11log
{
namespace sinks
{
class async_sink : public base_sink
{
public:
using size_type = c11log::details::blocking_queue<std::string>::size_type;
explicit async_sink(const size_type max_queue_size);
~async_sink();
void add_sink(logger::sink_ptr sink);
void remove_sink(logger::sink_ptr sink_ptr);
//Wait to remaining items (if any) in the queue to be written and shutdown
void shutdown(const std::chrono::seconds& timeout);
protected:
void _sink_it(const bufpair_t& msg) override;
void _thread_loop();
private:
c11log::logger::sinks_vector_t _sinks;
std::atomic<bool> _active;
c11log::details::blocking_queue<std::string> _q;
std::thread _back_thread;
//Clear all remaining messages(if any), stop the _back_thread and join it
void _shutdown();
};
}
}
///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size)
:_sinks(),
_active(true),
_q(max_queue_size),
_back_thread(&async_sink::_thread_loop, this)
{}
inline c11log::sinks::async_sink::~async_sink()
{
_shutdown();
}
inline void c11log::sinks::async_sink::_sink_it(const bufpair_t& msg)
{
std::string s {msg.first, msg.first+msg.second};
_q.push(s);
}
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))
{
bufpair_t buf(msg.data(), msg.size());
for (auto &sink : _sinks)
{
sink->log(buf, static_cast<level::level_enum>(_level.load()));
if (!_active)
return;
}
}
}
}
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr sink)
{
_sinks.push_back(sink);
}
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr)
{
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end());
}
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)
{
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
_shutdown();
}
inline void c11log::sinks::async_sink::_shutdown()
{
if(_active)
{
_active = false;
if (_back_thread.joinable())
_back_thread.join();
}
}
#pragma once
#include <thread>
#include <chrono>
#include <atomic>
#include "base_sink.h"
#include "../logger.h"
#include "../details/blocking_queue.h"
namespace c11log
{
namespace sinks
{
class async_sink : public base_sink
{
public:
using size_type = c11log::details::blocking_queue<std::string>::size_type;
explicit async_sink(const size_type max_queue_size);
~async_sink();
void add_sink(logger::sink_ptr sink);
void remove_sink(logger::sink_ptr sink_ptr);
//Wait to remaining items (if any) in the queue to be written and shutdown
void shutdown(const std::chrono::seconds& timeout);
protected:
void _sink_it(const bufpair_t& msg) override;
void _thread_loop();
private:
c11log::logger::sinks_vector_t _sinks;
std::atomic<bool> _active;
c11log::details::blocking_queue<std::string> _q;
std::thread _back_thread;
//Clear all remaining messages(if any), stop the _back_thread and join it
void _shutdown();
};
}
}
///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size)
:_sinks(),
_active(true),
_q(max_queue_size),
_back_thread(&async_sink::_thread_loop, this)
{}
inline c11log::sinks::async_sink::~async_sink()
{
_shutdown();
}
inline void c11log::sinks::async_sink::_sink_it(const bufpair_t& msg)
{
std::string s {msg.first, msg.first+msg.second};
_q.push(s);
}
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))
{
bufpair_t buf(msg.data(), msg.size());
for (auto &sink : _sinks)
{
sink->log(buf, static_cast<level::level_enum>(_level.load()));
if (!_active)
return;
}
}
}
}
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr sink)
{
_sinks.push_back(sink);
}
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr)
{
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end());
}
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)
{
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
_shutdown();
}
inline void c11log::sinks::async_sink::_shutdown()
{
if(_active)
{
_active = false;
if (_back_thread.joinable())
_back_thread.join();
}
}

View File

@@ -1,51 +1,51 @@
#pragma once
#include<string>
#include<atomic>
#include "../formatter.h"
#include "../common_types.h"
namespace c11log
{
namespace sinks
{
class base_sink
{
public:
base_sink() = default;
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 bufpair_t &msg, level::level_enum level)
{
if (level >= _level)
{
_sink_it(msg);
}
};
void set_level(level::level_enum level)
{
_level = level;
}
protected:
virtual void _sink_it(const bufpair_t& msg) = 0;
std::atomic<int> _level {level::INFO};
};
class null_sink:public base_sink
{
protected:
void _sink_it(const bufpair_t& ) override
{
}
};
}
}
#pragma once
#include<string>
#include<atomic>
#include "../formatter.h"
#include "../common_types.h"
namespace c11log
{
namespace sinks
{
class base_sink
{
public:
base_sink() = default;
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 bufpair_t &msg, level::level_enum level)
{
if (level >= _level)
{
_sink_it(msg);
}
};
void set_level(level::level_enum level)
{
_level = level;
}
protected:
virtual void _sink_it(const bufpair_t& msg) = 0;
std::atomic<int> _level {level::INFO};
};
class null_sink:public base_sink
{
protected:
void _sink_it(const bufpair_t& ) override
{
}
};
}
}

View File

@@ -1,47 +1,47 @@
#pragma once
#include <iostream>
#include <mutex>
#include <memory>
#include "base_sink.h"
namespace c11log
{
namespace sinks
{
class console_sink: public base_sink
{
public:
explicit console_sink(std::ostream& os):_ostream(os) {}
console_sink(const console_sink&) = delete;
console_sink& operator=(const console_sink&) = delete;
virtual ~console_sink() = default;
protected:
virtual void _sink_it(const bufpair_t& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_ostream.write(msg.first, msg.second);
}
std::ostream& _ostream;
std::mutex _mutex;
};
inline std::shared_ptr<console_sink>& stdout_sink ()
{
static auto inst = std::make_shared<console_sink>(std::cout);
return inst;
}
inline std::shared_ptr<console_sink>& stderr_sink ()
{
static auto inst = std::make_shared<console_sink>(std::cerr);
return inst;
}
}
}
#pragma once
#include <iostream>
#include <mutex>
#include <memory>
#include "base_sink.h"
namespace c11log
{
namespace sinks
{
class console_sink: public base_sink
{
public:
explicit console_sink(std::ostream& os):_ostream(os) {}
console_sink(const console_sink&) = delete;
console_sink& operator=(const console_sink&) = delete;
virtual ~console_sink() = default;
protected:
virtual void _sink_it(const bufpair_t& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_ostream.write(msg.first, msg.second);
}
std::ostream& _ostream;
std::mutex _mutex;
};
inline std::shared_ptr<console_sink>& stdout_sink ()
{
static auto inst = std::make_shared<console_sink>(std::cout);
return inst;
}
inline std::shared_ptr<console_sink>& stderr_sink ()
{
static auto inst = std::make_shared<console_sink>(std::cerr);
return inst;
}
}
}

View File

@@ -1,180 +1,180 @@
#pragma once
#include <fstream>
#include <iomanip>
#include <mutex>
#include "base_sink.h"
#include "../details/flush_helper.h"
namespace c11log
{
namespace sinks
{
/*
* 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::size_t flush_every=0)
: _mutex(),
_ofstream(filename + "." + extension, std::ofstream::binary|std::ofstream::app),
_flush_helper(flush_every)
{
}
protected:
void _sink_it(const bufpair_t& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_flush_helper.write(_ofstream, msg);
}
private:
std::mutex _mutex;
std::ofstream _ofstream;
details::file_flush_helper _flush_helper;
};
/*
* 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,
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),
_max_files(max_files),
_current_size(0),
_mutex(),
_ofstream(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary),
_flush_helper(flush_every)
{
}
protected:
void _sink_it(const bufpair_t& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_current_size += msg.second;
if (_current_size > _max_size)
{
_rotate();
_current_size = msg.second;
}
_flush_helper.write(_ofstream, msg);
}
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();
}
// 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;
details::file_flush_helper _flush_helper;
};
/*
* 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::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)
{
}
protected:
void _sink_it(const bufpair_t& 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();
}
_flush_helper.write(_ofstream, msg);
}
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));
}
//Create filename for the form basename.YYYY-MM-DD.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 << '.';
oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday;
oss << '.' << extension;
return oss.str();
}
std::string _base_filename;
std::string _extension;
std::chrono::system_clock::time_point _midnight_tp;
std::mutex _mutex;
std::ofstream _ofstream;
details::file_flush_helper _flush_helper;
};
}
}
#pragma once
#include <fstream>
#include <iomanip>
#include <mutex>
#include "base_sink.h"
#include "../details/flush_helper.h"
namespace c11log
{
namespace sinks
{
/*
* 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::size_t flush_every=0)
: _mutex(),
_ofstream(filename + "." + extension, std::ofstream::binary|std::ofstream::app),
_flush_helper(flush_every)
{
}
protected:
void _sink_it(const bufpair_t& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_flush_helper.write(_ofstream, msg);
}
private:
std::mutex _mutex;
std::ofstream _ofstream;
details::file_flush_helper _flush_helper;
};
/*
* 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,
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),
_max_files(max_files),
_current_size(0),
_mutex(),
_ofstream(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary),
_flush_helper(flush_every)
{
}
protected:
void _sink_it(const bufpair_t& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_current_size += msg.second;
if (_current_size > _max_size)
{
_rotate();
_current_size = msg.second;
}
_flush_helper.write(_ofstream, msg);
}
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();
}
// 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;
details::file_flush_helper _flush_helper;
};
/*
* 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::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)
{
}
protected:
void _sink_it(const bufpair_t& 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();
}
_flush_helper.write(_ofstream, msg);
}
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));
}
//Create filename for the form basename.YYYY-MM-DD.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 << '.';
oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday;
oss << '.' << extension;
return oss.str();
}
std::string _base_filename;
std::string _extension;
std::chrono::system_clock::time_point _midnight_tp;
std::mutex _mutex;
std::ofstream _ofstream;
details::file_flush_helper _flush_helper;
};
}
}