diff --git a/example/example.cpp b/example/example.cpp index 462613a1..1ef6bef4 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -19,28 +19,26 @@ int main(int argc, char* argv[]) { if(argc || argv) {}; - const unsigned int howmany = argc <= 1 ? 1500000 :atoi(argv[1]); + const unsigned int howmany = argc <= 1 ? 1000 :atoi(argv[1]); auto fsink = std::make_shared("log", "txt", 1024*1024*50 , 5, 0); auto null_sink = std::make_shared(); + logger cout_logger ("cout", {sinks::stdout_sink()}); - cout_logger.info() << "Hello cout logger!"; logger my_logger ("my_logger", {null_sink}); - std::string s(100, '0'); - auto start = system_clock::now(); for(unsigned int i = 0; i < howmany ; i++) - my_logger.info() << s; + my_logger.info("Hello logger"); auto delta = system_clock::now() - start; auto delta_d = duration_cast> (delta).count(); - cout_logger.info() << "Total " << format(howmany); - cout_logger.info() << "Delta " << format(delta_d); - cout_logger.info() << "Rate: " << format(howmany/delta_d) << "/sec"; + cout_logger.info("Total ") << format(howmany); + cout_logger.info("Delta ") << format(delta_d); + cout_logger.info("Rate: ") << format(howmany/delta_d) << "/sec"; return 0; diff --git a/include/c11log/common_types.h b/include/c11log/common_types.h index d4b1efea..bbe724df 100644 --- a/include/c11log/common_types.h +++ b/include/c11log/common_types.h @@ -16,11 +16,12 @@ typedef enum INFO, WARNING, ERROR, + CRITICAL, FATAL, NONE = 99 } level_enum; -static const char* level_names[] { "debug", "info", "warning", "error", "fatal" }; +static const char* level_names[] { "debug", "info", "warning", "error", "critical", "fatal" }; inline const char* to_str(c11log::level::level_enum l) { return level_names[l]; diff --git a/include/c11log/details/line_logger.h b/include/c11log/details/line_logger.h index fce5a933..70a2d6c9 100644 --- a/include/c11log/details/line_logger.h +++ b/include/c11log/details/line_logger.h @@ -8,7 +8,6 @@ // line logger class. should be used by the logger as an rvalue only. // aggregates logging string until the end of the line and then calls the logger upon destruction - namespace c11log { //class logger; @@ -49,7 +48,6 @@ public: _oss(), _enabled(other._enabled) {} - ~line_logger() { if (_enabled) @@ -59,15 +57,13 @@ public: _callback_logger->_log_it(_log_msg); } } - - + template - line_logger&& operator<<(const T& what) + line_logger& operator<<(const T& what) { if (_enabled) _oss << what; - - return std::move(*this); + return *this; } private: @@ -75,8 +71,6 @@ private: log_msg _log_msg; details::stack_oss _oss; bool _enabled; - - }; } //Namespace details } // Namespace c11log diff --git a/include/c11log/logger.h b/include/c11log/logger.h index 97b124b0..166bdf47 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -53,12 +53,14 @@ public: const std::string& get_name() const; bool should_log(c11log::level::level_enum) const; - details::line_logger log(level::level_enum); - details::line_logger debug(); - details::line_logger info(); - details::line_logger warn(); - details::line_logger error(); - details::line_logger fatal(); + details::line_logger log(c11log::level::level_enum); + template details::line_logger debug(const T&); + template details::line_logger info(const T&); + template details::line_logger warn(const T&); + template details::line_logger error(const T&); + template details::line_logger critical(const T&); + template details::line_logger fatal(const T&); + private: friend details::line_logger; @@ -103,34 +105,77 @@ inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enu return details::line_logger(this, msg_level, msg_level >= _logger_level); } -inline c11log::details::line_logger c11log::logger::debug() + +template +inline c11log::details::line_logger c11log::logger::debug(const T& what) { - return log(c11log::level::DEBUG); + bool really_log = should_log(level::DEBUG); + details::line_logger l(this, level::DEBUG, really_log); + if(really_log) + l << what; + return l; } -inline c11log::details::line_logger c11log::logger::info() + +template +inline c11log::details::line_logger c11log::logger::info(const T& what) { - return log(c11log::level::INFO); + bool really_log = should_log(level::INFO); + details::line_logger l(this, level::INFO, really_log); + if(really_log) + l << what; + return l; } -inline c11log::details::line_logger c11log::logger::warn() + + +template +inline c11log::details::line_logger c11log::logger::warn(const T& what) { - return log(c11log::level::WARNING); + bool really_log = should_log(level::WARNING); + details::line_logger l(this, level::WARNING, really_log); + if(really_log) + l << what; + return l; } -inline c11log::details::line_logger c11log::logger::error() + + +template +inline c11log::details::line_logger c11log::logger::error(const T& what) { - return log(level::ERROR); + bool really_log = should_log(level::ERROR); + details::line_logger l(this, level::ERROR, really_log); + if(really_log) + l << what; + return l; } -inline c11log::details::line_logger c11log::logger::fatal() + + +template +inline c11log::details::line_logger c11log::logger::critical(const T& what) { - return log(c11log::level::FATAL); + bool really_log = should_log(level::CRITICAL); + details::line_logger l(this, level::CRITICAL, really_log); + if(really_log) + l << what; + return l; } +template +inline c11log::details::line_logger c11log::logger::fatal(const T& what) +{ + bool really_log = should_log(level::FATAL); + details::line_logger l(this, level::FATAL, really_log); + if(really_log) + l << what; + return l; +} + + inline const std::string& c11log::logger::get_name() const { return _logger_name; } - inline void c11log::logger::set_level(c11log::level::level_enum level) { _logger_level.store(level); diff --git a/include/c11log/sinks/async_sink.h b/include/c11log/sinks/async_sink.h index fb92733b..6a408e34 100644 --- a/include/c11log/sinks/async_sink.h +++ b/include/c11log/sinks/async_sink.h @@ -74,6 +74,7 @@ inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg) auto new_shared = std::shared_ptr(new_msg, [](details::log_msg* msg_to_delete) { delete []msg_to_delete->msg_buf.first; + delete msg_to_delete; }); _q.push(new_shared); }