Some syntactic improvements

This commit is contained in:
gabi 2014-10-10 21:17:26 +03:00
parent ae2426e0be
commit b49b086723
5 changed files with 27 additions and 49 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
// Flush to file every X writes.. // Flush to file every X writes..
// If X is zero than never flush..
namespace c11log namespace c11log
{ {
@ -13,6 +14,8 @@ public:
_flush_every(flush_every), _flush_every(flush_every),
_flush_countdown(flush_every) {}; _flush_countdown(flush_every) {};
file_flush_helper(const file_flush_helper&) = delete;
void write(const std::string& msg, std::ofstream& ofs) void write(const std::string& msg, std::ofstream& ofs)
{ {
ofs.write(msg.data(), msg.size()); ofs.write(msg.data(), msg.size());

View File

@ -11,10 +11,9 @@
namespace c11log namespace c11log
{ {
//class logger;
namespace details namespace details
{ {
class line_logger class line_logger
{ {
public: public:
@ -45,7 +44,7 @@ public:
{ {
if (_enabled) if (_enabled)
{ {
_log_msg.logger_name = _callback_logger->get_name(); _log_msg.logger_name = _callback_logger->name();
_log_msg.time = log_clock::now(); _log_msg.time = log_clock::now();
_log_msg.raw = _oss.str(); _log_msg.raw = _oss.str();
_callback_logger->_log_it(_log_msg); _callback_logger->_log_it(_log_msg);

View File

@ -1,7 +1,12 @@
#pragma once #pragma once
// Thread safe logger // Thread safe logger
// Has log level and vector sinks which do the actual logging // Has name, log level, vector of std::shared sink pointers and formatter
// Upon each log write the logger:
// 1. Checks if its log level is enough to log the message
// 2. Format the message using the formatter function
// 3. Pass the formatted message to it sinks to performa the actual logging
#include<vector> #include<vector>
#include<memory> #include<memory>
#include<atomic> #include<atomic>
@ -10,44 +15,33 @@
#include "sinks/base_sink.h" #include "sinks/base_sink.h"
#include "common.h" #include "common.h"
//Thread safe, fast logger.
//All initialization is done in ctor only, so we get away lot of locking
namespace c11log namespace c11log
{ {
namespace details namespace details
{ {
class line_logger; class line_logger;
template<std::size_t> class fast_buf;
} }
class logger class logger
{ {
public: public:
using sink_ptr = std::shared_ptr<sinks::isink>; using sink_ptr = std::shared_ptr<sinks::isink>;
using sinks_vector_t = std::vector<sink_ptr>; using sinks_vector_t = std::vector<sink_ptr>;
using sinks_init_list = std::initializer_list<sink_ptr>; using sinks_init_list = std::initializer_list<sink_ptr>;
using formatter_ptr = std::unique_ptr<c11log::formatters::formatter>; using formatter_ptr = std::unique_ptr<c11log::formatters::formatter>;
logger(const std::string& name, sinks_init_list, formatter_ptr = nullptr); logger(const std::string& name, sinks_init_list, formatter_ptr = nullptr);
logger(const std::string& name, sink_ptr, formatter_ptr = nullptr); logger(const std::string& name, sink_ptr, formatter_ptr = nullptr);
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
~logger() = default; ~logger() = default;
//Non copybale in anyway
logger(const logger&) = delete;
logger(logger&&) = delete;
logger& operator=(const logger&) = delete;
logger& operator=(logger&&) = delete;
void set_min_level(c11log::level::level_enum); void level(c11log::level::level_enum);
c11log::level::level_enum get_level() const; c11log::level::level_enum level() const;
const std::string& get_name() const; const std::string& name() const;
bool should_log(c11log::level::level_enum) const; bool should_log(c11log::level::level_enum) const;
template<typename T> details::line_logger trace(const T&); template<typename T> details::line_logger trace(const T&);
@ -73,7 +67,7 @@ private:
std::string _logger_name; std::string _logger_name;
formatter_ptr _formatter; formatter_ptr _formatter;
sinks_vector_t _sinks; sinks_vector_t _sinks;
std::atomic_int _min_level; std::atomic_int _level;
void _log_it(details::log_msg& msg); void _log_it(details::log_msg& msg);
}; };
@ -84,7 +78,6 @@ private:
} }
// //
// Logger implementation // Logger implementation
// //
@ -109,7 +102,7 @@ inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_lis
_sinks(sinks_list) _sinks(sinks_list)
{ {
//Seems that vs2013 doesn't support std::atomic member initialization yet //Seems that vs2013 doesn't support std::atomic member initialization yet
_min_level = level::INFO; _level = level::INFO;
if(!_formatter) if(!_formatter)
_formatter = std::make_unique<formatters::default_formatter>(); _formatter = std::make_unique<formatters::default_formatter>();
@ -120,7 +113,6 @@ inline c11log::logger::logger(const std::string& name, sink_ptr sink, formatter_
logger(name, {sink}, std::move(f)) {} logger(name, {sink}, std::move(f)) {}
template<typename T> template<typename T>
inline c11log::details::line_logger c11log::logger::trace(const T& msg) inline c11log::details::line_logger c11log::logger::trace(const T& msg)
{ {
@ -201,24 +193,24 @@ inline c11log::details::line_logger c11log::logger::fatal()
} }
inline const std::string& c11log::logger::get_name() const inline const std::string& c11log::logger::name() const
{ {
return _logger_name; return _logger_name;
} }
inline void c11log::logger::set_min_level(c11log::level::level_enum level) inline void c11log::logger::level(c11log::level::level_enum level)
{ {
_min_level.store(level); _level.store(level);
} }
inline c11log::level::level_enum c11log::logger::get_level() const inline c11log::level::level_enum c11log::logger::level() const
{ {
return static_cast<c11log::level::level_enum>(_min_level.load()); return static_cast<c11log::level::level_enum>(_level.load());
} }
inline bool c11log::logger::should_log(c11log::level::level_enum level) const inline bool c11log::logger::should_log(c11log::level::level_enum level) const
{ {
return level >= _min_level.load(); return level >= _level.load();
} }
inline void c11log::logger::_log_it(details::log_msg& msg) inline void c11log::logger::_log_it(details::log_msg& msg)

View File

@ -17,36 +17,22 @@ template<class Mutex>
class base_sink:public isink class base_sink:public isink
{ {
public: public:
base_sink():_mutex(), _enabled(true) {} base_sink():_mutex() {}
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 details::log_msg& msg) override void log(const details::log_msg& msg) override
{
if (_enabled)
{ {
std::lock_guard<Mutex> lock(_mutex); std::lock_guard<Mutex> lock(_mutex);
_sink_it(msg); _sink_it(msg);
}
}; };
void enable(bool enabled) override
{
_enabled = enabled;
}
bool is_enabled() override
{
return _enabled.load();
}
protected: protected:
virtual void _sink_it(const details::log_msg& msg) = 0; virtual void _sink_it(const details::log_msg& msg) = 0;
Mutex _mutex; Mutex _mutex;
std::atomic<bool> _enabled;
}; };
} }
} }

View File

@ -10,8 +10,6 @@ class isink
{ {
public: public:
virtual void log(const details::log_msg& msg) = 0; virtual void log(const details::log_msg& msg) = 0;
virtual void enable(bool enabled) = 0;
virtual bool is_enabled() = 0;
}; };
} }
} }