logger templates

This commit is contained in:
gabime 2014-03-29 00:27:13 +03:00
parent c09df09b9c
commit 1c4be7fb88
5 changed files with 74 additions and 35 deletions

View File

@ -19,28 +19,26 @@ int main(int argc, char* argv[])
{ {
if(argc || 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<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0); auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
auto null_sink = std::make_shared<sinks::null_sink>(); auto null_sink = std::make_shared<sinks::null_sink>();
logger cout_logger ("cout", {sinks::stdout_sink()}); logger cout_logger ("cout", {sinks::stdout_sink()});
cout_logger.info() << "Hello cout logger!";
logger my_logger ("my_logger", {null_sink}); logger my_logger ("my_logger", {null_sink});
std::string s(100, '0');
auto start = system_clock::now(); auto start = system_clock::now();
for(unsigned int i = 0; i < howmany ; i++) 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 = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count(); auto delta_d = duration_cast<duration<double>> (delta).count();
cout_logger.info() << "Total " << format(howmany); cout_logger.info("Total ") << format(howmany);
cout_logger.info() << "Delta " << format(delta_d); cout_logger.info("Delta ") << format(delta_d);
cout_logger.info() << "Rate: " << format(howmany/delta_d) << "/sec"; cout_logger.info("Rate: ") << format(howmany/delta_d) << "/sec";
return 0; return 0;

View File

@ -16,11 +16,12 @@ typedef enum
INFO, INFO,
WARNING, WARNING,
ERROR, ERROR,
CRITICAL,
FATAL, FATAL,
NONE = 99 NONE = 99
} level_enum; } 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) inline const char* to_str(c11log::level::level_enum l)
{ {
return level_names[l]; return level_names[l];

View File

@ -8,7 +8,6 @@
// line logger class. should be used by the logger as an rvalue only. // 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 // aggregates logging string until the end of the line and then calls the logger upon destruction
namespace c11log namespace c11log
{ {
//class logger; //class logger;
@ -49,7 +48,6 @@ public:
_oss(), _oss(),
_enabled(other._enabled) {} _enabled(other._enabled) {}
~line_logger() ~line_logger()
{ {
if (_enabled) if (_enabled)
@ -59,15 +57,13 @@ public:
_callback_logger->_log_it(_log_msg); _callback_logger->_log_it(_log_msg);
} }
} }
template<typename T> template<typename T>
line_logger&& operator<<(const T& what) line_logger& operator<<(const T& what)
{ {
if (_enabled) if (_enabled)
_oss << what; _oss << what;
return *this;
return std::move(*this);
} }
private: private:
@ -75,8 +71,6 @@ private:
log_msg _log_msg; log_msg _log_msg;
details::stack_oss _oss; details::stack_oss _oss;
bool _enabled; bool _enabled;
}; };
} //Namespace details } //Namespace details
} // Namespace c11log } // Namespace c11log

View File

@ -53,12 +53,14 @@ public:
const std::string& get_name() const; const std::string& get_name() const;
bool should_log(c11log::level::level_enum) const; bool should_log(c11log::level::level_enum) const;
details::line_logger log(level::level_enum); details::line_logger log(c11log::level::level_enum);
details::line_logger debug(); template<typename T> details::line_logger debug(const T&);
details::line_logger info(); template<typename T> details::line_logger info(const T&);
details::line_logger warn(); template<typename T> details::line_logger warn(const T&);
details::line_logger error(); template<typename T> details::line_logger error(const T&);
details::line_logger fatal(); template<typename T> details::line_logger critical(const T&);
template<typename T> details::line_logger fatal(const T&);
private: private:
friend details::line_logger; 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); return details::line_logger(this, msg_level, msg_level >= _logger_level);
} }
inline c11log::details::line_logger c11log::logger::debug()
template<typename T>
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<typename T>
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<typename T>
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<typename T>
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<typename T>
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<typename T>
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 inline const std::string& c11log::logger::get_name() const
{ {
return _logger_name; return _logger_name;
} }
inline void c11log::logger::set_level(c11log::level::level_enum level) inline void c11log::logger::set_level(c11log::level::level_enum level)
{ {
_logger_level.store(level); _logger_level.store(level);

View File

@ -74,6 +74,7 @@ inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
auto new_shared = std::shared_ptr<details::log_msg>(new_msg, [](details::log_msg* msg_to_delete) auto new_shared = std::shared_ptr<details::log_msg>(new_msg, [](details::log_msg* msg_to_delete)
{ {
delete []msg_to_delete->msg_buf.first; delete []msg_to_delete->msg_buf.first;
delete msg_to_delete;
}); });
_q.push(new_shared); _q.push(new_shared);
} }