From cc71df8100b33904d2c12a92fef237bebacbe1cd Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 12 May 2017 20:44:42 +0200 Subject: Refactor log code Inverse log levels order. Allow to change logging level with int. Signed-off-by: Szczepan Zalega --- log.cc | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'log.cc') diff --git a/log.cc b/log.cc index 84f34d7..7d61a0d 100644 --- a/log.cc +++ b/log.cc @@ -30,31 +30,32 @@ namespace nitrokey { void Log::operator()(const std::string &logstr, Loglevel lvl) { if (mp_loghandler != nullptr) - if ((int) lvl >= (int) m_loglevel) mp_loghandler->print(logstr, lvl); + if ((int) lvl <= (int) m_loglevel) mp_loghandler->print(logstr, lvl); } void StdlogHandler::print(const std::string &str, Loglevel lvl) { - std::time_t t = std::time(nullptr); - std::tm tm = *std::localtime(&t); - - std::clog << "[" << loglevel_to_str(lvl) << "] [" - << std::put_time(&tm, "%c %z") - << "]\t" << str << std::endl; + std::string s = format_message_to_string(str, lvl); + std::clog << s; } void FunctionalLogHandler::print(const std::string &str, Loglevel lvl) { - std::time_t t = std::time(nullptr); - std::tm tm = *std::localtime(&t); + std::string s = format_message_to_string(str, lvl); + log_function(s); + } - std::stringstream s; - s << "[" << loglevel_to_str(lvl) << "] [" - << std::put_time(&tm, "%c %z") - << "]\t" << str << std::endl; + std::string LogHandler::format_message_to_string(const std::string &str, const Loglevel &lvl) { + time_t t = time(nullptr); + tm tm = *localtime(&t); - log_function(s.str()); + std::stringstream s; + s + << "[" << std::put_time(&tm, "%c") << "]" + << "[" << loglevel_to_str(lvl) << "]\t" + << str << std::endl; + return s.str(); } - FunctionalLogHandler::FunctionalLogHandler(std::function _log_function) { + FunctionalLogHandler::FunctionalLogHandler(log_function_type _log_function) { log_function = _log_function; } } -- cgit v1.2.1