aboutsummaryrefslogtreecommitdiff
path: root/log.cc
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2017-04-07 07:06:13 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2017-04-07 07:06:13 +0200
commitb2dec03e9b3df59550e598906a76e51bf721afa6 (patch)
tree604f6a258f90af0c46f84ded5522952d13072d82 /log.cc
parentd5ad36fab5ea6270ff6b0a8b14f8ce10f0fb3c34 (diff)
downloadlibnitrokey-b2dec03e9b3df59550e598906a76e51bf721afa6.tar.gz
libnitrokey-b2dec03e9b3df59550e598906a76e51bf721afa6.tar.bz2
Handle print function in logger by functor
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
Diffstat (limited to 'log.cc')
-rw-r--r--log.cc18
1 files changed, 18 insertions, 0 deletions
diff --git a/log.cc b/log.cc
index 3370959..84f34d7 100644
--- a/log.cc
+++ b/log.cc
@@ -4,6 +4,8 @@
#include <iomanip>
#include "log.h"
+#include <sstream>
+
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<void(std::string)> _log_function) {
+ log_function = _log_function;
+ }
}
}