From 38c3b4c58e6c6b86d6241183be1814bac2e037d2 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 16 Feb 2018 15:17:47 +0100 Subject: Log current device ID Allow logger to set global prefix Used to indicate current device Store USB path when used to connection as well Signed-off-by: Szczepan Zalega --- NitrokeyManager.cc | 16 +++++++++++++++- device.cc | 2 +- include/NitrokeyManager.h | 5 +++++ include/log.h | 5 +++++ log.cc | 17 +++++++++++++++-- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 162ad93..f2518b1 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -171,10 +171,14 @@ using nitrokey::misc::strcpyT; std::lock_guard lock(mex_dev_com_manager); auto position = connected_devices_byID.find(id); - if (position == connected_devices_byID.end()) return false; + if (position == connected_devices_byID.end()) { + LOGD1(std::string("Could not find device ")+id); + return false; + } auto d = connected_devices_byID[id]; device = d; + current_device_id = id; //validate connection try{ @@ -185,10 +189,13 @@ using nitrokey::misc::strcpyT; } catch (const DeviceCommunicationException &){ d->disconnect(); + current_device_id = ""; connected_devices_byID[id] = nullptr; connected_devices_byID.erase(position); return false; } + nitrokey::log::Log::setPrefix(id); + LOGD1("Device successfully changed"); return true; } @@ -221,6 +228,9 @@ using nitrokey::misc::strcpyT; } device = p; //previous device will be disconnected automatically + current_device_id = path; + nitrokey::log::Log::setPrefix(path); + LOGD1("Device successfully changed"); return true; } @@ -1105,5 +1115,9 @@ using nitrokey::misc::strcpyT; return data.data().SD_Card_Size_u8; } + const string NitrokeyManager::get_current_device_id() const { + return current_device_id; + } + } diff --git a/device.cc b/device.cc index 9321d2a..da5345b 100644 --- a/device.cc +++ b/device.cc @@ -248,7 +248,7 @@ Stick10::Stick10(): Stick20::Stick20(): - Device(0x20a0, 0x4109, DeviceModel::STORAGE, 40ms, 25, 40ms) + Device(0x20a0, 0x4109, DeviceModel::STORAGE, 40ms, 55, 40ms) { setDefaultDelay(); } diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 7ce432f..5a76616 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -203,6 +203,11 @@ char * strndup(const char* str, size_t maxlen); static shared_ptr _instance; std::shared_ptr device; + std::string current_device_id; + public: + const string get_current_device_id() const; + + private: std::unordered_map > connected_devices; std::unordered_map > connected_devices_byID; diff --git a/include/log.h b/include/log.h index 89eda56..b969b81 100644 --- a/include/log.h +++ b/include/log.h @@ -86,6 +86,11 @@ namespace nitrokey { private: LogHandler *mp_loghandler; Loglevel m_loglevel; + static std::string prefix; + public: + static void setPrefix(std::string prefix = std::string()); + + private: static Log *mp_instance; }; diff --git a/log.cc b/log.cc index 263ddd7..e66feb0 100644 --- a/log.cc +++ b/log.cc @@ -26,6 +26,7 @@ #include "log.h" #include +#include namespace nitrokey { namespace log { @@ -33,6 +34,9 @@ namespace nitrokey { Log *Log::mp_instance = nullptr; StdlogHandler stdlog_handler; + std::string Log::prefix = ""; + + std::string LogHandler::loglevel_to_str(Loglevel lvl) { switch (lvl) { case Loglevel::DEBUG_L1: @@ -52,8 +56,17 @@ 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 (mp_loghandler != nullptr){ + if ((int) lvl <= (int) m_loglevel) mp_loghandler->print(prefix+logstr, lvl); + } + } + + void Log::setPrefix(const string prefix) { + if (!prefix.empty()){ + Log::prefix = "["+prefix+"]"; + } else { + Log::prefix = ""; + } } void StdlogHandler::print(const std::string &str, Loglevel lvl) { -- cgit v1.2.3