aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NitrokeyManager.cc6
-rw-r--r--include/NitrokeyManager.h2
-rw-r--r--include/log.h13
-rw-r--r--log.cc31
4 files changed, 34 insertions, 18 deletions
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc
index 9fbd303..096a8c4 100644
--- a/NitrokeyManager.cc
+++ b/NitrokeyManager.cc
@@ -187,6 +187,12 @@ char * strndup(const char* str, size_t maxlen){
return false;
}
+ void NitrokeyManager::set_loglevel(int loglevel) {
+ loglevel = max(loglevel, static_cast<int>(Loglevel::ERROR));
+ loglevel = min(loglevel, static_cast<int>(Loglevel::DEBUG_L2));
+ Log::instance().set_loglevel(static_cast<Loglevel>(loglevel));
+ }
+
void NitrokeyManager::set_loglevel(Loglevel loglevel) {
Log::instance().set_loglevel(loglevel);
}
diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h
index e3dcc45..de14fbc 100644
--- a/include/NitrokeyManager.h
+++ b/include/NitrokeyManager.h
@@ -179,6 +179,8 @@ char * strndup(const char* str, size_t maxlen);
public:
bool set_current_device_speed(int retry_delay, int send_receive_delay);
void set_loglevel(Loglevel loglevel);
+
+ void set_loglevel(int loglevel);
};
}
diff --git a/include/log.h b/include/log.h
index e7c790a..e559845 100644
--- a/include/log.h
+++ b/include/log.h
@@ -16,7 +16,11 @@ namespace nitrokey {
enum class Loglevel : int {
- DEBUG_L2, DEBUG, INFO, WARNING, ERROR
+ ERROR,
+ WARNING,
+ INFO,
+ DEBUG,
+ DEBUG_L2
};
class LogHandler {
@@ -24,6 +28,8 @@ namespace nitrokey {
virtual void print(const std::string &, Loglevel lvl) = 0;
protected:
std::string loglevel_to_str(Loglevel);
+ std::string format_message_to_string(const std::string &str, const Loglevel &lvl);
+
};
class StdlogHandler : public LogHandler {
@@ -32,9 +38,10 @@ namespace nitrokey {
};
class FunctionalLogHandler : public LogHandler {
- std::function<void(std::string)> log_function;
+ using log_function_type = std::function<void(std::string)>;
+ log_function_type log_function;
public:
- FunctionalLogHandler(std::function<void(std::string)> _log_function);
+ FunctionalLogHandler(log_function_type _log_function);
virtual void print(const std::string &, Loglevel lvl);
};
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;
}
}