From ff9c37701bd0bd9de5a32ecfaa5e12e7c43acca4 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Wed, 4 Mar 2020 12:35:41 +0100 Subject: [PATCH] WIP --- slsSupportLib/include/logger.h | 26 +++------ slsSupportLib/include/logger2.h | 87 +++++++++++++++++++++++++++++ slsSupportLib/tests/test-logger.cpp | 43 ++++++++++++++ 3 files changed, 138 insertions(+), 18 deletions(-) create mode 100644 slsSupportLib/include/logger2.h diff --git a/slsSupportLib/include/logger.h b/slsSupportLib/include/logger.h index c5e8933f3..5239cc0f2 100755 --- a/slsSupportLib/include/logger.h +++ b/slsSupportLib/include/logger.h @@ -5,7 +5,7 @@ #include #include #include -#include + #ifdef FIFODEBUG @@ -22,8 +22,8 @@ #endif -// #define STRINGIFY(x) #x -// #define TOSTRING(x) STRINGIFY(x) +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) #define MYCONCAT(x,y) #define __AT__ std::string(__FILE__) + std::string("::") + std::string(__func__) + std::string("(): ") #define __SHORT_FORM_OF_FILE__ \ @@ -162,23 +162,13 @@ inline FILE*& Output2FILE::Stream() return pStream; } -// inline void Output2FILE::Output(const std::string& msg) -// { -// FILE* pStream = Stream(); -// if (!pStream) -// return; -// fprintf(pStream, "%s", msg.c_str()); -// fflush(pStream); -// } - inline void Output2FILE::Output(const std::string& msg) { - std::cout << msg; - // FILE* pStream = Stream(); - // if (!pStream) - // return; - // fprintf(pStream, "%s", msg.c_str()); - // fflush(pStream); + FILE* pStream = Stream(); + if (!pStream) + return; + fprintf(pStream, "%s", msg.c_str()); + fflush(pStream); } inline void Output2FILE::Output(const std::string& msg, TLogLevel level) diff --git a/slsSupportLib/include/logger2.h b/slsSupportLib/include/logger2.h new file mode 100644 index 000000000..239a7567e --- /dev/null +++ b/slsSupportLib/include/logger2.h @@ -0,0 +1,87 @@ +#pragma once +/*Utility to log to console*/ + +#include "ansi.h" //Colors +#include "logger.h" +#include +#include +#include +#include +#include + +/* +Define the max level that is visible +The compiler should optimize away any calls below +this level +*/ +#ifndef LOG_MAX_LEVEL +#define LOG_MAX_LEVEL logINFO +#endif + +namespace sls { +class Logger { + std::ostringstream os; + TLogLevel level = LOG_MAX_LEVEL; + + public: + Logger() = default; + Logger(TLogLevel level) : level(level){}; + ~Logger() { + // output happens in the destructor to allow for << + os << Reset() << '\n'; + std::clog << os.str(); // Single write + } + + static TLogLevel &ReportingLevel() { // singelton eeh + static TLogLevel reportingLevel = logINFO; + return reportingLevel; + } + + // Danger this buffer need as many elements as TLogLevel + static const char *Color(TLogLevel level) { + static const char *const colors[] = { + RED BOLD, YELLOW BOLD, RESET, BLUE, RED, RESET, + RESET, RESET, RESET, RESET, RESET, RESET}; + return colors[level]; + } + static const char *Reset() { + static const char *reset = RESET; + return reset; + } + + // Danger this buffer need as many elements as TLogLevel + static std::string ToString(TLogLevel level) { + static const char *const buffer[] = { + "ERROR", "WARNING", "INFO", "INFO", "INFO", "INFO", + "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4", "DEBUG5"}; + return buffer[level]; + } + + std::ostringstream &Get() { + os << Color(level); + os << "- " << Time(); + os << " " << ToString(level) << ": "; + return os; + } + std::string Time(decltype(std::chrono::system_clock::now()) now = + std::chrono::system_clock::now()) { + std::ostringstream oss; + auto ms = std::chrono::duration_cast( + now.time_since_epoch()) % + 1000; + std::time_t now_time = std::chrono::system_clock::to_time_t(now); + oss << std::put_time(std::localtime(&now_time), "%H:%M:%S") << "." + << std::setw(3) << std::setfill('0') << ms.count(); + return oss.str(); + } +}; + +#define LOG(level) \ + if (level > LOG_MAX_LEVEL) \ + ; \ + else if (level > sls::Logger::ReportingLevel()) \ + ; \ + else \ + sls::Logger(level).Get() + +} // namespace sls diff --git a/slsSupportLib/tests/test-logger.cpp b/slsSupportLib/tests/test-logger.cpp index 7d6439b6b..a150ae812 100644 --- a/slsSupportLib/tests/test-logger.cpp +++ b/slsSupportLib/tests/test-logger.cpp @@ -1,13 +1,56 @@ #include "catch.hpp" #include "logger.h" +#include "logger2.h" #include #include +#include + +TEST_CASE("Get time"){ + auto now = std::chrono::system_clock::now(); + sls::Logger log; + auto time = log.Time(now); + +} + TEST_CASE("fail"){ FILE_LOG(logINFO) << "A message"; + FILE_LOG(logWARNING) << "An error"; + FILE_LOG(logERROR) << "A warning"; + +// sls::Logger::ReportingLevel() = logERROR; + // std::cout << sls::Logger::ReportingLevel() << '\n'; + LOG(logINFO) << "A new message"; + LOG(logERROR) << "A new error"; + LOG(logWARNING) << "A new warning"; + + LOG(logDEBUG3) << "This should not be printed"; + + std::ostringstream local; + auto clog_buff = std::clog.rdbuf(); + std::clog.rdbuf(local.rdbuf()); + LOG(logERROR) << "This should also not be printed"; + + std::clog.rdbuf(clog_buff); // restore + LOG(logERROR) << "But this should"; + + std::cout << "we got: " << local.str() << '\n'; + // sls::Logger::ReportingLevel() = logDEBUG1; + // std::cout << sls::Logger::ReportingLevel() << '\n'; + // std::ostream& os = std::cout; + // Output2FILE::Stream(); + + // os << "hej pa dig\n"; + + // std::ostringstream oss; + // std::ostream& os = oss; + // auto& out = Output2FILE::Stream(); + // out = os; + // Output2FILE::Stream() = std::cout; + // FILE_LOG(logERROR) << "An error message"; // CHECK(false);