From 10ebcff48e6f0b7b9cc753ce7f7d4da699c89590 Mon Sep 17 00:00:00 2001 From: hagen Date: Fri, 4 Nov 2016 00:00:00 +0000 Subject: [PATCH] * Log.{cpp,h}: * use colors only when using stdout * use static string array instead bunch of #define's --- Log.cpp | 23 +++++++++++++++++++++-- Log.h | 19 +------------------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Log.cpp b/Log.cpp index 5660821f..268e9667 100644 --- a/Log.cpp +++ b/Log.cpp @@ -22,6 +22,22 @@ namespace log { "debug" // eLogDebug }; + /** + * @brief Colorize log output -- array of terminal control sequences + * @note Using ISO 6429 (ANSI) color sequences + */ +#ifdef _WIN32 + static const char *LogMsgColors[] = { "", "", "", "", "" }; +#else /* UNIX */ + static const char *LogMsgColors[] = { + [eLogError] = "\033[1;31m", /* red */ + [eLogWarning] = "\033[1;33m", /* yellow */ + [eLogInfo] = "\033[1;36m", /* cyan */ + [eLogDebug] = "\033[1;34m", /* blue */ + [eNumLogLevels] = "\033[0m", /* reset */ + }; +#endif + #ifndef _WIN32 /** * @brief Maps our log levels to syslog one @@ -42,7 +58,7 @@ namespace log { Log::Log(): m_Destination(eLogStdout), m_MinLevel(eLogInfo), - m_LogStream (nullptr), m_Logfile(""), m_IsReady(false) + m_LogStream (nullptr), m_Logfile(""), m_IsReady(false), m_HasColors(true) { } @@ -117,7 +133,7 @@ namespace log { default: std::cout << TimeAsString(msg->timestamp) << "@" << short_tid - << "/" << g_LogLevelStr[msg->level] + << "/" << LogMsgColors[msg->level] << g_LogLevelStr[msg->level] << LogMsgColors[eNumLogLevels] << " - " << msg->text << std::endl; break; } // switch @@ -138,6 +154,7 @@ namespace log { auto os = std::make_shared (path, flags); if (os->is_open ()) { + m_HasColors = false; m_Logfile = path; m_Destination = eLogFile; m_LogStream = os; @@ -147,12 +164,14 @@ namespace log { } void Log::SendTo (std::shared_ptr os) { + m_HasColors = false; m_Destination = eLogStream; m_LogStream = os; } #ifndef _WIN32 void Log::SendTo(const char *name, int facility) { + m_HasColors = false; m_Destination = eLogSyslog; m_LogStream = nullptr; openlog(name, LOG_CONS | LOG_PID, facility); diff --git a/Log.h b/Log.h index b4eb70cd..a6fc2222 100644 --- a/Log.h +++ b/Log.h @@ -40,17 +40,6 @@ enum LogType { #endif }; -#ifdef _WIN32 - const char LOG_COLOR_ERROR[] = ""; - const char LOG_COLOR_WARNING[] = ""; - const char LOG_COLOR_RESET[] = ""; -#else - const char LOG_COLOR_ERROR[] = "\033[1;31m"; - const char LOG_COLOR_WARNING[] = "\033[1;33m"; - const char LOG_COLOR_RESET[] = "\033[0m"; -#endif - - namespace i2p { namespace log { @@ -68,6 +57,7 @@ namespace log { char m_LastDateTime[64]; i2p::util::Queue > m_Queue; volatile bool m_IsReady; + bool m_HasColors; mutable std::mutex m_OutputLock; private: @@ -190,15 +180,8 @@ void LogPrint (LogLevel level, TArgs&&... args) noexcept // fold message to single string std::stringstream ss(""); - if(level == eLogError) // if log level is ERROR color log message red - ss << LOG_COLOR_ERROR; - else if (level == eLogWarning) // if log level is WARN color log message yellow - ss << LOG_COLOR_WARNING; LogPrint (ss, std::forward(args)...); - // reset color - ss << LOG_COLOR_RESET; - auto msg = std::make_shared(level, std::time(nullptr), ss.str()); msg->tid = std::this_thread::get_id(); log.Append(msg);