aboutsummaryrefslogtreecommitdiff
path: root/log.cc
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2017-05-12 20:44:42 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2017-05-12 20:59:34 +0200
commitcc71df8100b33904d2c12a92fef237bebacbe1cd (patch)
tree9ebf7952d6f638e361169172d2041744888e0509 /log.cc
parentebfcff1730b5db2ad85ad47c4ac3963581ef43ba (diff)
downloadlibnitrokey-cc71df8100b33904d2c12a92fef237bebacbe1cd.tar.gz
libnitrokey-cc71df8100b33904d2c12a92fef237bebacbe1cd.tar.bz2
Refactor log code
Inverse log levels order. Allow to change logging level with int. Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
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;
}
}