From b2dec03e9b3df59550e598906a76e51bf721afa6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 7 Apr 2017 07:06:13 +0200 Subject: Handle print function in logger by functor Signed-off-by: Szczepan Zalega --- log.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'log.cc') diff --git a/log.cc b/log.cc index 3370959..84f34d7 100644 --- a/log.cc +++ b/log.cc @@ -4,6 +4,8 @@ #include #include "log.h" +#include + namespace nitrokey { namespace log { @@ -39,5 +41,21 @@ namespace nitrokey { << std::put_time(&tm, "%c %z") << "]\t" << str << std::endl; } + + void FunctionalLogHandler::print(const std::string &str, Loglevel lvl) { + std::time_t t = std::time(nullptr); + std::tm tm = *std::localtime(&t); + + std::stringstream s; + s << "[" << loglevel_to_str(lvl) << "] [" + << std::put_time(&tm, "%c %z") + << "]\t" << str << std::endl; + + log_function(s.str()); + } + + FunctionalLogHandler::FunctionalLogHandler(std::function _log_function) { + log_function = _log_function; + } } } -- cgit v1.2.1