i2pd/Log.cpp

172 lines
4.0 KiB
C++
Raw Normal View History

2016-03-27 00:00:00 +00:00
/*
* Copyright (c) 2013-2016, The PurpleI2P Project
*
* 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"
2016-03-27 00:00:00 +00:00
namespace i2p {
namespace log {
Log logger;
/**
2016-05-31 00:00:00 +00:00
* @brief Maps our loglevel to their symbolic name
2016-03-27 00:00:00 +00:00
*/
static const char * g_LogLevelStr[eNumLogLevels] =
{
"error", // eLogError
"warn", // eLogWarn
"info", // eLogInfo
"debug" // eLogDebug
};
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
/**
* @brief Maps our log levels to syslog one
* @return syslog priority LOG_*, as defined in syslog.h
*/
static inline int GetSyslogPrio (enum LogLevel l) {
int priority = LOG_DEBUG;
switch (l) {
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),
m_LogStream (nullptr), m_Logfile(""), m_IsReady(false)
{
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 ()
{
2016-03-27 00:00:00 +00:00
switch (m_Destination) {
#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;
}
Process();
}
2016-03-27 00:00:00 +00:00
void Log::SetLogLevel (const std::string& level) {
if (level == "error") { m_MinLevel = eLogError; }
else if (level == "warn") { m_MinLevel = eLogWarning; }
else if (level == "info") { m_MinLevel = eLogInfo; }
else if (level == "debug") { m_MinLevel = eLogDebug; }
else {
LogPrint(eLogError, "Log: unknown loglevel: ", level);
return;
}
LogPrint(eLogInfo, "Log: min messages level set to ", level);
}
const char * Log::TimeAsString(std::time_t t) {
if (t != m_LastTimestamp) {
strftime(m_LastDateTime, sizeof(m_LastDateTime), "%H:%M:%S", localtime(&t));
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.
*/
2016-03-27 00:00:00 +00:00
void Log::Process() {
std::unique_lock<std::mutex> l(m_OutputLock);
2016-03-28 14:00:00 +00:00
std::hash<std::thread::id> hasher;
unsigned short short_tid;
2016-03-27 00:00:00 +00:00
while (1) {
auto msg = m_Queue.GetNextWithTimeout (1);
if (!msg)
break;
2016-03-28 14:00:00 +00:00
short_tid = (short) (hasher(msg->tid) % 1000);
2016-03-27 00:00:00 +00:00
switch (m_Destination) {
#ifndef _WIN32
case eLogSyslog:
2016-03-28 14:00:00 +00:00
syslog(GetSyslogPrio(msg->level), "[%03u] %s", short_tid, msg->text.c_str());
2016-03-27 00:00:00 +00:00
break;
#endif
case eLogFile:
case eLogStream:
2016-05-30 16:08:20 +00:00
if (m_LogStream)
*m_LogStream << TimeAsString(msg->timestamp)
<< "@" << short_tid
<< "/" << g_LogLevelStr[msg->level]
<< " - " << msg->text << std::endl;
2016-03-27 00:00:00 +00:00
break;
2016-03-28 14:00:00 +00:00
case eLogStdout:
2016-03-27 00:00:00 +00:00
default:
2016-03-28 14:00:00 +00:00
std::cout << TimeAsString(msg->timestamp)
<< "@" << short_tid
<< "/" << g_LogLevelStr[msg->level]
<< " - " << msg->text << std::endl;
2016-03-27 00:00:00 +00:00
break;
} // switch
} // while
}
2016-02-04 18:53:38 +00:00
2016-03-27 00:00:00 +00:00
void Log::Append(std::shared_ptr<i2p::log::LogMsg> & msg) {
m_Queue.Put(msg);
if (!m_IsReady)
return;
Process();
}
2015-12-28 00:00:00 +00:00
2016-05-30 13:41:45 +00:00
void Log::SendTo (const std::string& path)
{
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);
2016-05-30 13:41:45 +00:00
if (os->is_open ())
{
2016-03-27 00:00:00 +00:00
m_Logfile = path;
m_Destination = eLogFile;
m_LogStream = os;
return;
}
LogPrint(eLogError, "Log: can't open file ", path);
}
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) {
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) {
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;
}
} // log
} // i2p