diff options
| author | Szczepan Zalega <szczepan@nitrokey.com> | 2018-02-16 15:17:47 +0100 | 
|---|---|---|
| committer | Szczepan Zalega <szczepan@nitrokey.com> | 2018-02-28 20:07:14 +0100 | 
| commit | 38c3b4c58e6c6b86d6241183be1814bac2e037d2 (patch) | |
| tree | 2f5dd6ea25a64f446133c48209ceb53fd4615d71 | |
| parent | d6ae8192be443749fcd1f593db0f9be4d039da93 (diff) | |
| download | libnitrokey-38c3b4c58e6c6b86d6241183be1814bac2e037d2.tar.gz libnitrokey-38c3b4c58e6c6b86d6241183be1814bac2e037d2.tar.bz2 | |
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 <szczepan@nitrokey.com>
| -rw-r--r-- | NitrokeyManager.cc | 16 | ||||
| -rw-r--r-- | device.cc | 2 | ||||
| -rw-r--r-- | include/NitrokeyManager.h | 5 | ||||
| -rw-r--r-- | include/log.h | 5 | ||||
| -rw-r--r-- | 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<std::mutex> 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; +    } +  } @@ -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 <NitrokeyManager> _instance;          std::shared_ptr<Device> device; +        std::string current_device_id; +    public: +        const string get_current_device_id() const; + +    private:          std::unordered_map<std::string, shared_ptr<Device> > connected_devices;          std::unordered_map<std::string, shared_ptr<Device> > 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;      }; @@ -26,6 +26,7 @@  #include "log.h"  #include <sstream> +#include <NitrokeyManager.h>  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) { | 
