diff --git a/example/example.cpp b/example/example.cpp index a52585a2..54cd2e47 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -1,12 +1,10 @@ // example.cpp : Simple logger example // -#include -#include + #include "c11log/logger.h" #include "c11log/sinks/async_sink.h" #include "c11log/sinks/file_sinks.h" #include "c11log/sinks/console_sinks.h" -#include "c11log/details/log_msg.h" #include "utils.h" @@ -16,24 +14,25 @@ using namespace std::chrono; using namespace c11log; using namespace utils; + int main(int argc, char* argv[]) { if(argc || argv) {}; + const unsigned int howmany = 1000000; auto fsink = std::make_shared("log", "txt", 1024*1024*50 , 5, 0); //auto fsink = std::make_shared("simplelog", "txt"); auto null_sink = std::make_shared(); - logger cout_logger ("cout", {null_sink, sinks::stdout_sink()}); - cout_logger.info() << "Hello cout logger!"; + cout_logger.info() << "Hello cout logger!"; logger my_logger ("my_logger", {null_sink}); std::string s(100, '0'); - const unsigned int howmany = 5000000; + auto start = system_clock::now(); for(unsigned int i = 0; i < howmany ; i++) my_logger.info() << s; diff --git a/include/c11log/details/fast_buf.h b/include/c11log/details/fast_buf.h index 6f956e1c..8ff28530 100644 --- a/include/c11log/details/fast_buf.h +++ b/include/c11log/details/fast_buf.h @@ -5,14 +5,11 @@ #include #include -// Fast buffer +// Fast memory storage // stores its contents on the stack when possible, in vector otherwise // NOTE: User should be remember that returned buffer might be on the stack!! -namespace c11log -{ -namespace details -{ +namespace c11log { namespace details { template class fast_buf diff --git a/include/c11log/details/fast_oss.h b/include/c11log/details/fast_oss.h index 8804b6bb..bd52ef38 100644 --- a/include/c11log/details/fast_oss.h +++ b/include/c11log/details/fast_oss.h @@ -1,7 +1,7 @@ #pragma once // Faster than ostringstream--returns its string by ref - +#include #include "c11log/details/fast_buf.h" namespace c11log @@ -19,21 +19,14 @@ public: str_devicebuf(str_devicebuf&& other) = delete; str_devicebuf& operator=(const str_devicebuf&) = delete; str_devicebuf& operator=(str_devicebuf&&) = delete; - - /* - const std::string& str_ref() const - { - return _str; - } - */ + bufpair_t buf() { return _fastbuf.get(); } void reset_str() - { - //_str.clear(); + { _fastbuf.clear(); } @@ -46,21 +39,13 @@ protected: // copy the give buffer into the accumulated string. // reserve initially 128 bytes which should be enough for common log lines std::streamsize xsputn(const char_type* s, std::streamsize count) override - { - /* - if(_str.capacity() < k_initial_reserve) - { - _str.reserve(k_initial_reserve); - } - _str.append(s, static_cast(count)); - */ + { _fastbuf.append(s, static_cast(count)); return count; } int_type overflow(int_type ch) override { - bool not_eofile = traits_type::not_eof(ch); if (not_eofile) { @@ -70,7 +55,6 @@ protected: return not_eofile; } private: - //std::string _str; fast_buf<192> _fastbuf; }; @@ -83,12 +67,7 @@ public: fast_oss(const fast_oss& other) = delete; fast_oss(fast_oss&& other) = delete; fast_oss& operator=(const fast_oss& other) = delete; - /* - const std::string& str_ref() const - { - return _dev.str_ref(); - } - */ + bufpair_t buf() { return _dev.buf(); diff --git a/include/c11log/details/line_logger.h b/include/c11log/details/line_logger.h index c6aec833..494f6b4e 100644 --- a/include/c11log/details/line_logger.h +++ b/include/c11log/details/line_logger.h @@ -38,12 +38,13 @@ public: line_logger& operator=(const line_logger&) = delete; line_logger& operator=(line_logger&&) = delete; + // The move ctor should only be called on start of logging line, + // where no logging happened yet for this line so no need to copy the string from the other line_logger(line_logger&& other) : - _callback_logger(other._callback_logger), - // The move ctor should only be called on start of logging line, - // where no logging happened yet for this line so no need to copy the string from the other + _callback_logger(other._callback_logger), _oss(), - _level(other._level) {}; + _level(other._level), + _enabled(other._enabled) {} ~line_logger() diff --git a/include/c11log/logger.h b/include/c11log/logger.h index c68e1c96..56426512 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -67,7 +67,7 @@ private: std::string _logger_name = ""; formatter_ptr _formatter; sinks_vector_t _sinks; - std::atomic_int _atomic_level; + std::atomic_int _logger_level; void _log_it(const bufpair_t& buf, const level::level_enum level); @@ -91,7 +91,7 @@ inline c11log::logger::logger(const std::string& name, formatter_ptr f, sinks_in _sinks(sinks_list) { //Seems that vs2013 doesnt support atomic member initialization in ctor, so its done here - _atomic_level = level::INFO; + _logger_level = level::INFO; } inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list) : @@ -103,7 +103,7 @@ inline c11log::logger::logger(sinks_init_list sinks_list) : inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level) { - return details::line_logger(this, msg_level, msg_level >= _atomic_level); + return details::line_logger(this, msg_level, msg_level >= _logger_level); } inline c11log::details::line_logger c11log::logger::debug() @@ -136,17 +136,17 @@ inline const std::string& c11log::logger::get_name() const inline void c11log::logger::set_level(c11log::level::level_enum level) { - _atomic_level.store(level); + _logger_level.store(level); } inline c11log::level::level_enum c11log::logger::get_level() const { - return static_cast(_atomic_level.load()); + return static_cast(_logger_level.load()); } inline bool c11log::logger::should_log(c11log::level::level_enum level) const { - return level >= _atomic_level.load(); + return level >= _logger_level.load(); } inline void c11log::logger::_log_it(const bufpair_t& buf, const level::level_enum level)