2016-03-27 00:00:00 +00:00
|
|
|
/*
|
2022-12-18 20:26:18 +00:00
|
|
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
2016-03-27 00:00:00 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
2013-12-10 13:00:13 +00:00
|
|
|
|
2016-03-27 00:00:00 +00:00
|
|
|
#include "Log.h"
|
2020-12-07 03:22:16 +00:00
|
|
|
#include "util.h"
|
2014-04-22 22:10:21 +00:00
|
|
|
|
2018-05-19 15:03:49 +00:00
|
|
|
//for std::transform
|
|
|
|
#include <algorithm>
|
|
|
|
|
2016-03-27 00:00:00 +00:00
|
|
|
namespace i2p {
|
|
|
|
namespace log {
|
2016-09-15 22:47:38 +00:00
|
|
|
static Log logger;
|
2016-03-27 00:00:00 +00:00
|
|
|
/**
|
2016-05-31 00:00:00 +00:00
|
|
|
* @brief Maps our loglevel to their symbolic name
|
2016-03-27 00:00:00 +00:00
|
|
|
*/
|
2022-12-18 20:26:18 +00:00
|
|
|
static const char *g_LogLevelStr[eNumLogLevels] =
|
2016-03-27 00:00:00 +00:00
|
|
|
{
|
2017-11-15 18:30:00 +00:00
|
|
|
"none", // eLogNone
|
2016-03-27 00:00:00 +00:00
|
|
|
"error", // eLogError
|
2022-12-18 20:26:18 +00:00
|
|
|
"warn", // eLogWarning
|
2016-03-27 00:00:00 +00:00
|
|
|
"info", // eLogInfo
|
2017-11-15 18:30:00 +00:00
|
|
|
"debug" // eLogDebug
|
2016-03-27 00:00:00 +00:00
|
|
|
};
|
2016-03-26 13:40:19 +00:00
|
|
|
|
2016-11-04 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Colorize log output -- array of terminal control sequences
|
|
|
|
* @note Using ISO 6429 (ANSI) color sequences
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
2017-11-15 18:51:03 +00:00
|
|
|
static const char *LogMsgColors[] = { "", "", "", "", "", "" };
|
2016-11-04 00:00:00 +00:00
|
|
|
#else /* UNIX */
|
|
|
|
static const char *LogMsgColors[] = {
|
2022-12-18 20:26:18 +00:00
|
|
|
"\033[1;32m", /* none: green */
|
|
|
|
"\033[1;31m", /* error: red */
|
|
|
|
"\033[1;33m", /* warning: yellow */
|
|
|
|
"\033[1;36m", /* info: cyan */
|
|
|
|
"\033[1;34m", /* debug: blue */
|
|
|
|
"\033[0m" /* reset */
|
2016-11-04 00:00:00 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-03-26 13:49:45 +00:00
|
|
|
#ifndef _WIN32
|
2016-03-27 00:00:00 +00:00
|
|
|
/**
|
2022-05-20 16:56:05 +00:00
|
|
|
* @brief Maps our log levels to syslog one
|
2016-03-27 00:00:00 +00:00
|
|
|
* @return syslog priority LOG_*, as defined in syslog.h
|
|
|
|
*/
|
|
|
|
static inline int GetSyslogPrio (enum LogLevel l) {
|
|
|
|
int priority = LOG_DEBUG;
|
|
|
|
switch (l) {
|
2017-11-15 18:30:00 +00:00
|
|
|
case eLogNone : priority = LOG_CRIT; break;
|
2016-03-27 00:00:00 +00:00
|
|
|
case eLogError : priority = LOG_ERR; break;
|
|
|
|
case eLogWarning : priority = LOG_WARNING; break;
|
|
|
|
case eLogInfo : priority = LOG_INFO; break;
|
|
|
|
case eLogDebug : priority = LOG_DEBUG; break;
|
|
|
|
default : priority = LOG_DEBUG; break;
|
|
|
|
}
|
|
|
|
return priority;
|
|
|
|
}
|
2016-03-26 13:49:45 +00:00
|
|
|
#endif
|
2014-04-23 16:49:02 +00:00
|
|
|
|
2016-03-27 00:00:00 +00:00
|
|
|
Log::Log():
|
|
|
|
m_Destination(eLogStdout), m_MinLevel(eLogInfo),
|
2017-09-18 22:49:03 +00:00
|
|
|
m_LogStream (nullptr), m_Logfile(""), m_HasColors(true), m_TimeFormat("%H:%M:%S"),
|
2016-11-17 20:46:28 +00:00
|
|
|
m_IsRunning (false), m_Thread (nullptr)
|
2015-05-06 15:24:35 +00:00
|
|
|
{
|
2016-03-27 00:00:00 +00:00
|
|
|
}
|
2014-04-23 16:49:02 +00:00
|
|
|
|
2016-03-27 00:00:00 +00:00
|
|
|
Log::~Log ()
|
2014-12-08 21:27:10 +00:00
|
|
|
{
|
2016-11-17 20:46:28 +00:00
|
|
|
delete m_Thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::Start ()
|
|
|
|
{
|
|
|
|
if (!m_IsRunning)
|
2017-11-15 18:30:00 +00:00
|
|
|
{
|
|
|
|
m_IsRunning = true;
|
2016-11-17 20:46:28 +00:00
|
|
|
m_Thread = new std::thread (std::bind (&Log::Run, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 18:30:00 +00:00
|
|
|
void Log::Stop ()
|
2016-11-17 20:46:28 +00:00
|
|
|
{
|
2017-11-15 18:30:00 +00:00
|
|
|
switch (m_Destination)
|
2016-11-17 20:46:28 +00:00
|
|
|
{
|
2016-03-27 00:00:00 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
case eLogSyslog :
|
|
|
|
closelog();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case eLogFile:
|
|
|
|
case eLogStream:
|
2016-05-30 16:08:20 +00:00
|
|
|
if (m_LogStream) m_LogStream->flush();
|
2016-03-27 00:00:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* do nothing */
|
|
|
|
break;
|
|
|
|
}
|
2016-11-17 20:46:28 +00:00
|
|
|
m_IsRunning = false;
|
|
|
|
m_Queue.WakeUp ();
|
|
|
|
if (m_Thread)
|
2017-11-15 18:30:00 +00:00
|
|
|
{
|
|
|
|
m_Thread->join ();
|
2016-11-17 20:46:28 +00:00
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = nullptr;
|
2017-11-15 18:30:00 +00:00
|
|
|
}
|
2016-03-27 00:00:00 +00:00
|
|
|
}
|
2014-12-08 21:27:10 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
std::string str_tolower(std::string s) {
|
|
|
|
std::transform(s.begin(), s.end(), s.begin(),
|
2022-05-20 16:56:05 +00:00
|
|
|
// static_cast<int(*)(int)>(std::tolower) // wrong
|
|
|
|
// [](int c){ return std::tolower(c); } // wrong
|
|
|
|
// [](char c){ return std::tolower(c); } // wrong
|
|
|
|
[](unsigned char c){ return std::tolower(c); } // correct
|
|
|
|
);
|
2020-03-01 10:25:50 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::SetLogLevel (const std::string& level_) {
|
|
|
|
std::string level=str_tolower(level_);
|
2017-11-15 18:30:00 +00:00
|
|
|
if (level == "none") { m_MinLevel = eLogNone; }
|
|
|
|
else if (level == "error") { m_MinLevel = eLogError; }
|
2016-03-27 00:00:00 +00:00
|
|
|
else if (level == "warn") { m_MinLevel = eLogWarning; }
|
2023-01-19 05:33:03 +00:00
|
|
|
else if (level == "info") { m_MinLevel = eLogInfo; }
|
2016-03-27 00:00:00 +00:00
|
|
|
else if (level == "debug") { m_MinLevel = eLogDebug; }
|
|
|
|
else {
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogError, "Log: Unknown loglevel: ", level);
|
2016-03-27 00:00:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "Log: Logging level set to ", level);
|
2016-03-27 00:00:00 +00:00
|
|
|
}
|
2017-11-15 18:30:00 +00:00
|
|
|
|
2016-03-27 00:00:00 +00:00
|
|
|
const char * Log::TimeAsString(std::time_t t) {
|
|
|
|
if (t != m_LastTimestamp) {
|
2017-09-18 22:49:03 +00:00
|
|
|
strftime(m_LastDateTime, sizeof(m_LastDateTime), m_TimeFormat.c_str(), localtime(&t));
|
2016-03-27 00:00:00 +00:00
|
|
|
m_LastTimestamp = t;
|
|
|
|
}
|
|
|
|
return m_LastDateTime;
|
2016-02-04 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* @note This function better to be run in separate thread due to disk i/o.
|
|
|
|
* Unfortunately, with current startup process with late fork() this
|
|
|
|
* will give us nothing but pain. Maybe later. See in NetDb as example.
|
|
|
|
*/
|
2017-11-15 18:30:00 +00:00
|
|
|
void Log::Process(std::shared_ptr<LogMsg> msg)
|
2016-11-17 20:46:28 +00:00
|
|
|
{
|
|
|
|
if (!msg) return;
|
2016-03-28 14:00:00 +00:00
|
|
|
std::hash<std::thread::id> hasher;
|
|
|
|
unsigned short short_tid;
|
2016-11-17 20:46:28 +00:00
|
|
|
short_tid = (short) (hasher(msg->tid) % 1000);
|
|
|
|
switch (m_Destination) {
|
2016-03-27 00:00:00 +00:00
|
|
|
#ifndef _WIN32
|
2016-11-17 20:46:28 +00:00
|
|
|
case eLogSyslog:
|
|
|
|
syslog(GetSyslogPrio(msg->level), "[%03u] %s", short_tid, msg->text.c_str());
|
|
|
|
break;
|
2016-03-27 00:00:00 +00:00
|
|
|
#endif
|
2016-11-17 20:46:28 +00:00
|
|
|
case eLogFile:
|
|
|
|
case eLogStream:
|
|
|
|
if (m_LogStream)
|
|
|
|
*m_LogStream << TimeAsString(msg->timestamp)
|
2016-03-28 14:00:00 +00:00
|
|
|
<< "@" << short_tid
|
2016-11-17 20:46:28 +00:00
|
|
|
<< "/" << g_LogLevelStr[msg->level]
|
2016-03-28 14:00:00 +00:00
|
|
|
<< " - " << msg->text << std::endl;
|
2016-11-17 20:46:28 +00:00
|
|
|
break;
|
|
|
|
case eLogStdout:
|
|
|
|
default:
|
2022-05-20 16:56:05 +00:00
|
|
|
std::cout << TimeAsString(msg->timestamp)
|
2016-11-17 20:46:28 +00:00
|
|
|
<< "@" << short_tid
|
|
|
|
<< "/" << LogMsgColors[msg->level] << g_LogLevelStr[msg->level] << LogMsgColors[eNumLogLevels]
|
|
|
|
<< " - " << msg->text << std::endl;
|
|
|
|
break;
|
|
|
|
} // switch
|
2016-03-27 00:00:00 +00:00
|
|
|
}
|
2016-02-04 18:53:38 +00:00
|
|
|
|
2016-11-17 20:46:28 +00:00
|
|
|
void Log::Run ()
|
|
|
|
{
|
2020-12-07 03:22:16 +00:00
|
|
|
i2p::util::SetThreadName("Logging");
|
|
|
|
|
2016-12-22 15:08:35 +00:00
|
|
|
Reopen ();
|
2016-11-17 20:46:28 +00:00
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
std::shared_ptr<LogMsg> msg;
|
2017-11-14 05:38:45 +00:00
|
|
|
while ((msg = m_Queue.Get ()))
|
2016-11-17 20:46:28 +00:00
|
|
|
Process (msg);
|
|
|
|
if (m_LogStream) m_LogStream->flush();
|
|
|
|
if (m_IsRunning)
|
|
|
|
m_Queue.Wait ();
|
|
|
|
}
|
2017-11-15 18:30:00 +00:00
|
|
|
}
|
2016-11-17 20:46:28 +00:00
|
|
|
|
2017-11-15 18:30:00 +00:00
|
|
|
void Log::Append(std::shared_ptr<i2p::log::LogMsg> & msg)
|
2016-11-17 20:46:28 +00:00
|
|
|
{
|
2016-03-27 00:00:00 +00:00
|
|
|
m_Queue.Put(msg);
|
|
|
|
}
|
2015-12-28 00:00:00 +00:00
|
|
|
|
2017-11-15 18:30:00 +00:00
|
|
|
void Log::SendTo (const std::string& path)
|
2016-05-30 13:41:45 +00:00
|
|
|
{
|
2017-11-15 18:30:00 +00:00
|
|
|
if (m_LogStream) m_LogStream = nullptr; // close previous
|
2016-03-27 00:00:00 +00:00
|
|
|
auto flags = std::ofstream::out | std::ofstream::app;
|
|
|
|
auto os = std::make_shared<std::ofstream> (path, flags);
|
2017-11-15 18:30:00 +00:00
|
|
|
if (os->is_open ())
|
2016-05-30 13:41:45 +00:00
|
|
|
{
|
2016-11-04 00:00:00 +00:00
|
|
|
m_HasColors = false;
|
2016-03-27 00:00:00 +00:00
|
|
|
m_Logfile = path;
|
|
|
|
m_Destination = eLogFile;
|
|
|
|
m_LogStream = os;
|
|
|
|
return;
|
|
|
|
}
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogError, "Log: Can't open file ", path);
|
2016-03-27 00:00:00 +00:00
|
|
|
}
|
2016-03-26 13:40:19 +00:00
|
|
|
|
2016-03-27 00:00:00 +00:00
|
|
|
void Log::SendTo (std::shared_ptr<std::ostream> os) {
|
2016-11-04 00:00:00 +00:00
|
|
|
m_HasColors = false;
|
2016-03-27 00:00:00 +00:00
|
|
|
m_Destination = eLogStream;
|
|
|
|
m_LogStream = os;
|
|
|
|
}
|
2016-03-26 13:40:19 +00:00
|
|
|
|
2016-03-26 13:49:45 +00:00
|
|
|
#ifndef _WIN32
|
2016-03-27 00:00:00 +00:00
|
|
|
void Log::SendTo(const char *name, int facility) {
|
2017-11-15 18:30:00 +00:00
|
|
|
if (m_MinLevel == eLogNone) return;
|
2016-11-04 00:00:00 +00:00
|
|
|
m_HasColors = false;
|
2016-03-27 00:00:00 +00:00
|
|
|
m_Destination = eLogSyslog;
|
|
|
|
m_LogStream = nullptr;
|
|
|
|
openlog(name, LOG_CONS | LOG_PID, facility);
|
|
|
|
}
|
2016-03-26 13:49:45 +00:00
|
|
|
#endif
|
2016-03-26 13:40:19 +00:00
|
|
|
|
2016-03-27 00:00:00 +00:00
|
|
|
void Log::Reopen() {
|
|
|
|
if (m_Destination == eLogFile)
|
|
|
|
SendTo(m_Logfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log & Logger() {
|
|
|
|
return logger;
|
|
|
|
}
|
2020-05-05 15:13:59 +00:00
|
|
|
|
|
|
|
static ThrowFunction g_ThrowFunction;
|
|
|
|
ThrowFunction GetThrowFunction () { return g_ThrowFunction; }
|
|
|
|
void SetThrowFunction (ThrowFunction f) { g_ThrowFunction = f; }
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2016-03-27 00:00:00 +00:00
|
|
|
} // log
|
|
|
|
} // i2p
|
2020-05-05 15:13:59 +00:00
|
|
|
|