diff --git a/example/example.cpp b/example/example.cpp index ff610bab..bb6228ef 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -21,7 +21,8 @@ int main(int argc, char* argv[]) const unsigned int howmany = argc <= 1 ? 1000000:atoi(argv[1]); logger cout_logger ("", sinks::stdout_sink()); - cout_logger.info() << "Hello " << "man"; + cout_logger.info("Hello ") << "man" << 123 << " : " << 0.45f; + cout_logger.info("This is very nice! ") << "Yes gabi.." << ":)"; auto fsink = std::make_shared("log", "txt", 1024*1024*50 , 5, 0); auto nullsink = sinks::null_sink::get(); @@ -32,16 +33,16 @@ int main(int argc, char* argv[]) auto start = system_clock::now(); for(unsigned int i = 1; i <= howmany ; ++i) - my_logger.info() << "Hello logger: "; + my_logger.info("Hello logger: "); //auto s = howmany - as->q().size(); auto s = howmany; auto delta = system_clock::now() - start; auto delta_d = duration_cast> (delta).count(); - cout_logger.info() << "Total:" << format(s); - cout_logger.info() << "Delta:" << format(delta_d); - cout_logger.info() << "Rate:" << format(s/delta_d) << "/sec"; + cout_logger.info("Total:") << format(s); + cout_logger.info("Delta:") << format(delta_d); + cout_logger.info("Rate:") << format(s/delta_d) << "/sec"; return 0; } diff --git a/include/c11log/details/line_logger.h b/include/c11log/details/line_logger.h index 8344c4dc..99543a14 100644 --- a/include/c11log/details/line_logger.h +++ b/include/c11log/details/line_logger.h @@ -58,17 +58,25 @@ public: } } - template - line_logger& operator<<(const T& what) + template + void write(const T& what) { if (_enabled) { _oss << what; _empty = false; } + } + + template + line_logger& operator<<(const T& what) + { + write(what); return *this; } + + private: logger* _callback_logger; log_msg _log_msg; diff --git a/include/c11log/logger.h b/include/c11log/logger.h index 52e41b04..001fb9c5 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -49,15 +49,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 critical(); - details::line_logger fatal(); - + + 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; @@ -71,6 +70,8 @@ private: }; + +//Get from loggers pool if exists with such name logger& get_logger(const std::string& name); } @@ -97,38 +98,46 @@ inline c11log::logger::logger(const std::string& name, sink_ptr sink, formatter_ logger(name, {sink}, f) {} -inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level) + +template +inline c11log::details::line_logger c11log::logger::debug(const T& msg) { - return details::line_logger(this, msg_level, msg_level >= _min_level); + details::line_logger l(this, level::DEBUG, should_log(level::DEBUG)); + l.write(msg); + return l; } -inline c11log::details::line_logger c11log::logger::debug() +template +inline c11log::details::line_logger c11log::logger::info(const T& msg) { - return details::line_logger(this, level::DEBUG, should_log(level::DEBUG)); -} -inline c11log::details::line_logger c11log::logger::info() -{ - return details::line_logger(this, level::INFO, should_log(level::INFO)); -} -inline c11log::details::line_logger c11log::logger::warn() -{ - return details::line_logger(this, level::WARNING, should_log(level::WARNING)); -} -inline c11log::details::line_logger c11log::logger::error() -{ - return details::line_logger(this, level::ERROR, should_log(level::ERROR)); + details::line_logger l(this, level::INFO, should_log(level::INFO)); + l.write(msg); + return l; } -inline c11log::details::line_logger c11log::logger::critical() +template +inline c11log::details::line_logger c11log::logger::warn(const T& msg) { - return details::line_logger(this, level::CRITICAL, should_log(level::CRITICAL)); + details::line_logger l(this, level::WARNING, should_log(level::WARNING)); + l.write(msg); + return l; } -inline c11log::details::line_logger c11log::logger::fatal() +template +inline c11log::details::line_logger c11log::logger::critical(const T& msg) { - return details::line_logger(this, level::FATAL, should_log(level::FATAL)); + details::line_logger l(this, level::CRITICAL, should_log(level::CRITICAL)); + l.write(msg); + return l; } +template +inline c11log::details::line_logger c11log::logger::fatal(const T& msg) +{ + details::line_logger l(this, level::FATAL, should_log(level::FATAL)); + l.write(msg); + return l; +} inline const std::string& c11log::logger::get_name() const {