From b49b08672331ed59b0ab2e0798c19832b4a93e08 Mon Sep 17 00:00:00 2001 From: gabi Date: Fri, 10 Oct 2014 21:17:26 +0300 Subject: [PATCH] Some syntactic improvements --- include/c11log/details/flush_helper.h | 3 ++ include/c11log/details/line_logger.h | 5 ++- include/c11log/logger.h | 46 +++++++++++---------------- include/c11log/sinks/base_sink.h | 20 ++---------- include/c11log/sinks/isink.h | 2 -- 5 files changed, 27 insertions(+), 49 deletions(-) diff --git a/include/c11log/details/flush_helper.h b/include/c11log/details/flush_helper.h index e286b8ad..7aed39f8 100644 --- a/include/c11log/details/flush_helper.h +++ b/include/c11log/details/flush_helper.h @@ -1,5 +1,6 @@ #pragma once // Flush to file every X writes.. +// If X is zero than never flush.. namespace c11log { @@ -13,6 +14,8 @@ public: _flush_every(flush_every), _flush_countdown(flush_every) {}; + file_flush_helper(const file_flush_helper&) = delete; + void write(const std::string& msg, std::ofstream& ofs) { ofs.write(msg.data(), msg.size()); diff --git a/include/c11log/details/line_logger.h b/include/c11log/details/line_logger.h index 9c866b55..f1daeda7 100644 --- a/include/c11log/details/line_logger.h +++ b/include/c11log/details/line_logger.h @@ -11,10 +11,9 @@ namespace c11log { -//class logger; + namespace details { - class line_logger { public: @@ -45,7 +44,7 @@ public: { 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.raw = _oss.str(); _callback_logger->_log_it(_log_msg); diff --git a/include/c11log/logger.h b/include/c11log/logger.h index 1b70866c..8d282536 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -1,7 +1,12 @@ #pragma once // 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 #include #include @@ -10,44 +15,33 @@ #include "sinks/base_sink.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 details { class line_logger; -template class fast_buf; } class logger { public: - using sink_ptr = std::shared_ptr; using sinks_vector_t = std::vector; using sinks_init_list = std::initializer_list; - using formatter_ptr = std::unique_ptr; logger(const std::string& name, sinks_init_list, formatter_ptr = nullptr); logger(const std::string& name, sink_ptr, formatter_ptr = nullptr); - + logger(const logger&) = delete; + logger& operator=(const logger&) = delete; ~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); - c11log::level::level_enum get_level() const; + void level(c11log::level::level_enum); + 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; template details::line_logger trace(const T&); @@ -73,7 +67,7 @@ private: std::string _logger_name; formatter_ptr _formatter; sinks_vector_t _sinks; - std::atomic_int _min_level; + std::atomic_int _level; void _log_it(details::log_msg& msg); }; @@ -84,7 +78,6 @@ private: } - // // Logger implementation // @@ -109,7 +102,7 @@ inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_lis _sinks(sinks_list) { //Seems that vs2013 doesn't support std::atomic member initialization yet - _min_level = level::INFO; + _level = level::INFO; if(!_formatter) _formatter = std::make_unique(); @@ -120,7 +113,6 @@ inline c11log::logger::logger(const std::string& name, sink_ptr sink, formatter_ logger(name, {sink}, std::move(f)) {} - template 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; } -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(_min_level.load()); + return static_cast(_level.load()); } 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) diff --git a/include/c11log/sinks/base_sink.h b/include/c11log/sinks/base_sink.h index 4b583ddb..2ba02ff7 100644 --- a/include/c11log/sinks/base_sink.h +++ b/include/c11log/sinks/base_sink.h @@ -17,7 +17,7 @@ template class base_sink:public isink { public: - base_sink():_mutex(), _enabled(true) {} + base_sink():_mutex() {} virtual ~base_sink() = default; base_sink(const base_sink&) = delete; @@ -25,28 +25,14 @@ public: void log(const details::log_msg& msg) override { - if (_enabled) - { - std::lock_guard lock(_mutex); - _sink_it(msg); - } + std::lock_guard lock(_mutex); + _sink_it(msg); }; - void enable(bool enabled) override - { - _enabled = enabled; - } - - bool is_enabled() override - { - return _enabled.load(); - } protected: virtual void _sink_it(const details::log_msg& msg) = 0; Mutex _mutex; - std::atomic _enabled; - }; } } diff --git a/include/c11log/sinks/isink.h b/include/c11log/sinks/isink.h index 7468765c..c019bfe2 100644 --- a/include/c11log/sinks/isink.h +++ b/include/c11log/sinks/isink.h @@ -10,8 +10,6 @@ class isink { public: virtual void log(const details::log_msg& msg) = 0; - virtual void enable(bool enabled) = 0; - virtual bool is_enabled() = 0; }; } }