mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 13:27:17 +01:00
* Log.{cpp,h}:
* use colors only when using stdout * use static string array instead bunch of #define's
This commit is contained in:
parent
6ee227675a
commit
10ebcff48e
23
Log.cpp
23
Log.cpp
|
@ -22,6 +22,22 @@ namespace log {
|
||||||
"debug" // eLogDebug
|
"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
|
#ifndef _WIN32
|
||||||
/**
|
/**
|
||||||
* @brief Maps our log levels to syslog one
|
* @brief Maps our log levels to syslog one
|
||||||
|
@ -42,7 +58,7 @@ namespace log {
|
||||||
|
|
||||||
Log::Log():
|
Log::Log():
|
||||||
m_Destination(eLogStdout), m_MinLevel(eLogInfo),
|
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:
|
default:
|
||||||
std::cout << TimeAsString(msg->timestamp)
|
std::cout << TimeAsString(msg->timestamp)
|
||||||
<< "@" << short_tid
|
<< "@" << short_tid
|
||||||
<< "/" << g_LogLevelStr[msg->level]
|
<< "/" << LogMsgColors[msg->level] << g_LogLevelStr[msg->level] << LogMsgColors[eNumLogLevels]
|
||||||
<< " - " << msg->text << std::endl;
|
<< " - " << msg->text << std::endl;
|
||||||
break;
|
break;
|
||||||
} // switch
|
} // switch
|
||||||
|
@ -138,6 +154,7 @@ namespace log {
|
||||||
auto os = std::make_shared<std::ofstream> (path, flags);
|
auto os = std::make_shared<std::ofstream> (path, flags);
|
||||||
if (os->is_open ())
|
if (os->is_open ())
|
||||||
{
|
{
|
||||||
|
m_HasColors = false;
|
||||||
m_Logfile = path;
|
m_Logfile = path;
|
||||||
m_Destination = eLogFile;
|
m_Destination = eLogFile;
|
||||||
m_LogStream = os;
|
m_LogStream = os;
|
||||||
|
@ -147,12 +164,14 @@ namespace log {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::SendTo (std::shared_ptr<std::ostream> os) {
|
void Log::SendTo (std::shared_ptr<std::ostream> os) {
|
||||||
|
m_HasColors = false;
|
||||||
m_Destination = eLogStream;
|
m_Destination = eLogStream;
|
||||||
m_LogStream = os;
|
m_LogStream = os;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
void Log::SendTo(const char *name, int facility) {
|
void Log::SendTo(const char *name, int facility) {
|
||||||
|
m_HasColors = false;
|
||||||
m_Destination = eLogSyslog;
|
m_Destination = eLogSyslog;
|
||||||
m_LogStream = nullptr;
|
m_LogStream = nullptr;
|
||||||
openlog(name, LOG_CONS | LOG_PID, facility);
|
openlog(name, LOG_CONS | LOG_PID, facility);
|
||||||
|
|
19
Log.h
19
Log.h
|
@ -40,17 +40,6 @@ enum LogType {
|
||||||
#endif
|
#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 i2p {
|
||||||
namespace log {
|
namespace log {
|
||||||
|
|
||||||
|
@ -68,6 +57,7 @@ namespace log {
|
||||||
char m_LastDateTime[64];
|
char m_LastDateTime[64];
|
||||||
i2p::util::Queue<std::shared_ptr<LogMsg> > m_Queue;
|
i2p::util::Queue<std::shared_ptr<LogMsg> > m_Queue;
|
||||||
volatile bool m_IsReady;
|
volatile bool m_IsReady;
|
||||||
|
bool m_HasColors;
|
||||||
mutable std::mutex m_OutputLock;
|
mutable std::mutex m_OutputLock;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -190,15 +180,8 @@ void LogPrint (LogLevel level, TArgs&&... args) noexcept
|
||||||
// fold message to single string
|
// fold message to single string
|
||||||
std::stringstream ss("");
|
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<TArgs>(args)...);
|
LogPrint (ss, std::forward<TArgs>(args)...);
|
||||||
|
|
||||||
// reset color
|
|
||||||
ss << LOG_COLOR_RESET;
|
|
||||||
|
|
||||||
auto msg = std::make_shared<i2p::log::LogMsg>(level, std::time(nullptr), ss.str());
|
auto msg = std::make_shared<i2p::log::LogMsg>(level, std::time(nullptr), ss.str());
|
||||||
msg->tid = std::this_thread::get_id();
|
msg->tid = std::this_thread::get_id();
|
||||||
log.Append(msg);
|
log.Append(msg);
|
||||||
|
|
Loading…
Reference in a new issue