Browse Source

Merge pull request #440 from majestrate/master

add syslog option for logging
pull/443/head
orignal 9 years ago
parent
commit
b74055478c
  1. 5
      Daemon.cpp
  2. 47
      Log.cpp
  3. 26
      Log.h
  4. 7
      docs/i2pd.conf

5
Daemon.cpp

@ -159,7 +159,10 @@ namespace i2p
if (isDaemon && (logs == "" || logs == "stdout")) if (isDaemon && (logs == "" || logs == "stdout"))
logs = "file"; logs = "file";
if (logs == "file") { if (logs == "syslog") {
// use syslog only no stdout
StartSyslog();
} else if (logs == "file") {
if (logfile == "") if (logfile == "")
logfile = i2p::fs::DataDirPath("i2pd.log"); logfile = i2p::fs::DataDirPath("i2pd.log");
StartLog (logfile); StartLog (logfile);

47
Log.cpp

@ -10,9 +10,35 @@ static const char * g_LogLevelStr[eNumLogLevels] =
"info", // eLogInfo "info", // eLogInfo
"debug" // eLogDebug "debug" // eLogDebug
}; };
#ifndef _WIN32
/** convert LogLevel enum to syslog priority level */
static int ToSyslogLevel(LogLevel lvl)
{
switch (lvl) {
case eLogError:
return LOG_ERR;
case eLogWarning:
return LOG_WARNING;
case eLogInfo:
return LOG_INFO;
case eLogDebug:
return LOG_DEBUG;
default:
// WTF? invalid log level?
return LOG_CRIT;
}
}
#endif
void LogMsg::Process() void LogMsg::Process()
{ {
#ifndef _WIN32
if (log && log->SyslogEnabled()) {
// only log to syslog
syslog(ToSyslogLevel(level), "%s", s.str().c_str());
return;
}
#endif
auto stream = log ? log->GetLogStream () : nullptr; auto stream = log ? log->GetLogStream () : nullptr;
auto& output = stream ? *stream : std::cout; auto& output = stream ? *stream : std::cout;
if (log) if (log)
@ -84,3 +110,24 @@ void Log::SetLogStream (std::shared_ptr<std::ostream> logStream)
{ {
m_LogStream = logStream; m_LogStream = logStream;
} }
void Log::StartSyslog(const std::string & ident, const int facility)
{
#ifndef _WIN32
m_Ident = ident;
openlog(m_Ident.c_str(), LOG_PID, facility);
#endif
}
void Log::StopSyslog()
{
#ifndef _WIN32
closelog();
m_Ident.clear();
#endif
}
bool Log::SyslogEnabled()
{
return m_Ident.size() > 0;
}

26
Log.h

@ -10,6 +10,10 @@
#include <memory> #include <memory>
#include "Queue.h" #include "Queue.h"
#ifndef _WIN32
#include <syslog.h>
#endif
enum LogLevel enum LogLevel
{ {
eLogError = 0, eLogError = 0,
@ -46,7 +50,12 @@ class Log: public i2p::util::MsgQueue<LogMsg>
const std::string& GetTimestamp (); const std::string& GetTimestamp ();
LogLevel GetLogLevel () { return m_MinLevel; }; LogLevel GetLogLevel () { return m_MinLevel; };
const std::string& GetFullFilePath () const { return m_FullFilePath; }; const std::string& GetFullFilePath () const { return m_FullFilePath; };
/** start logging to syslog */
void StartSyslog(const std::string & ident, const int facility);
/** stop logging to syslog */
void StopSyslog();
/** are we logging to syslog right now? */
bool SyslogEnabled();
private: private:
void Flush (); void Flush ();
@ -62,6 +71,7 @@ class Log: public i2p::util::MsgQueue<LogMsg>
#else #else
std::chrono::steady_clock::time_point m_LastTimestampUpdate; std::chrono::steady_clock::time_point m_LastTimestampUpdate;
#endif #endif
std::string m_Ident;
}; };
extern Log * g_Log; extern Log * g_Log;
@ -116,6 +126,20 @@ inline bool IsLogToFile ()
return g_Log ? !g_Log->GetFullFilePath ().empty () : false; return g_Log ? !g_Log->GetFullFilePath ().empty () : false;
} }
inline void StartSyslog()
{
StartLog("");
#ifndef _WIN32
g_Log->StartSyslog("i2pd", LOG_USER);
#endif
}
inline void StopSyslog()
{
if(g_Log)
g_Log->StopSyslog();
}
template<typename TValue> template<typename TValue>
void LogPrint (std::stringstream& s, TValue arg) void LogPrint (std::stringstream& s, TValue arg)
{ {

7
docs/i2pd.conf

@ -16,7 +16,10 @@
## Logging configuration section ## Logging configuration section
## By default logs go to stdout with level info ## By default logs go to stdout with level info
## ##
## Logs destination (stdout, file) ## Logs destination (stdout, file, syslog)
## stdout - print log entries to stdout
## file - log entries to a file
## syslog - use syslog, see man 3 syslog
#log = file #log = file
## Path to logfile (default - autodetect) ## Path to logfile (default - autodetect)
#logfile = /var/log/i2pd.log #logfile = /var/log/i2pd.log
@ -40,7 +43,7 @@
## don't just uncomment this ## don't just uncomment this
#port = 4321 #port = 4321
##Enable communication through ipv6 ##Enable communication through ipv6
ipv6 ipv6 = true
## Bandwidth configuration ## Bandwidth configuration
## L limit bandwidth to 32Kbs/sec, O - to 256Kbs/sec, P - unlimited ## L limit bandwidth to 32Kbs/sec, O - to 256Kbs/sec, P - unlimited
## Default is P for floodfill, L for regular node ## Default is P for floodfill, L for regular node

Loading…
Cancel
Save