aboutsummaryrefslogtreecommitdiff
path: root/log.cc
diff options
context:
space:
mode:
Diffstat (limited to 'log.cc')
-rw-r--r--log.cc31
1 files changed, 16 insertions, 15 deletions
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<void(std::string)> _log_function) {
+ FunctionalLogHandler::FunctionalLogHandler(log_function_type _log_function) {
log_function = _log_function;
}
}