diff options
| author | Szczepan Zalega <szczepan@nitrokey.com> | 2017-04-07 07:06:13 +0200 | 
|---|---|---|
| committer | Szczepan Zalega <szczepan@nitrokey.com> | 2017-04-07 07:06:13 +0200 | 
| commit | b2dec03e9b3df59550e598906a76e51bf721afa6 (patch) | |
| tree | 604f6a258f90af0c46f84ded5522952d13072d82 | |
| parent | d5ad36fab5ea6270ff6b0a8b14f8ce10f0fb3c34 (diff) | |
| download | libnitrokey-b2dec03e9b3df59550e598906a76e51bf721afa6.tar.gz libnitrokey-b2dec03e9b3df59550e598906a76e51bf721afa6.tar.bz2 | |
Handle print function in logger by functor
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
| -rw-r--r-- | NitrokeyManager.cc | 8 | ||||
| -rw-r--r-- | include/NitrokeyManager.h | 1 | ||||
| -rw-r--r-- | include/log.h | 12 | ||||
| -rw-r--r-- | log.cc | 18 | 
4 files changed, 38 insertions, 1 deletions
| diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index a4ce3a5..3af74ea 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -8,7 +8,7 @@  #include "include/misc.h"  #include <mutex>  #include "include/cxx_semantics.h" - +#include <functional>  namespace nitrokey{ @@ -95,6 +95,12 @@ namespace nitrokey{          return device != nullptr;      } + +    void NitrokeyManager::set_log_function(std::function<void(std::string)> log_function){ +      static nitrokey::log::FunctionalLogHandler handler(log_function); +      nitrokey::log::Log::instance().set_handler(&handler); +    } +      bool NitrokeyManager::set_default_commands_delay(int delay){        if (delay < 20){          LOG("Delay set too low: " + to_string(delay), Loglevel::WARNING); diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index a815571..51cd966 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -139,6 +139,7 @@ namespace nitrokey {          int get_minor_firmware_version();          explicit NitrokeyManager(); +        void set_log_function(std::function<void(std::string)> log_function);      private:          static shared_ptr <NitrokeyManager> _instance; diff --git a/include/log.h b/include/log.h index 38fc122..e7c790a 100644 --- a/include/log.h +++ b/include/log.h @@ -4,6 +4,8 @@  #include <string>  #include <cstddef> +#include <functional> +  namespace nitrokey {    namespace log { @@ -29,6 +31,14 @@ namespace nitrokey {        virtual void print(const std::string &, Loglevel lvl);      }; +    class FunctionalLogHandler : public LogHandler { +      std::function<void(std::string)> log_function; +    public: +      FunctionalLogHandler(std::function<void(std::string)> _log_function); +      virtual void print(const std::string &, Loglevel lvl); + +    }; +      extern StdlogHandler stdlog_handler;      class Log { @@ -56,8 +66,10 @@ namespace nitrokey {  #ifdef NO_LOG  #define LOG(string, level) while(false){} +#define LOGD(string, level) while(false){}  #else  #define LOG(string, level) nitrokey::log::Log::instance()((string), (level)) +#define LOGD(string) nitrokey::log::Log::instance()((string), (nitrokey::log::Loglevel::DEBUG_L2))  #endif  #endif @@ -4,6 +4,8 @@  #include <iomanip>  #include "log.h" +#include <sstream> +  namespace nitrokey {    namespace log { @@ -39,5 +41,21 @@ namespace nitrokey {                  << std::put_time(&tm, "%c %z")                  << "]\t" << str << std::endl;      } + +    void FunctionalLogHandler::print(const std::string &str, Loglevel lvl) { +      std::time_t t = std::time(nullptr); +      std::tm tm = *std::localtime(&t); + +      std::stringstream s; +      s << "[" << loglevel_to_str(lvl) << "] [" +                << std::put_time(&tm, "%c %z") +                << "]\t" << str << std::endl; + +      log_function(s.str()); +    } + +    FunctionalLogHandler::FunctionalLogHandler(std::function<void(std::string)> _log_function) { +      log_function = _log_function; +    }    }  } | 
