Browse Source

add syslog logging option

pull/440/head
Jeff Becker 8 years ago
parent
commit
5261a3e845
No known key found for this signature in database
GPG Key ID: AB950234D6EA286B
  1. 5
      Daemon.cpp
  2. 41
      Log.cpp
  3. 27
      Log.h
  4. 7
      docs/i2pd.conf

5
Daemon.cpp

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

41
Log.cpp

@ -11,8 +11,32 @@ static const char * g_LogLevelStr[eNumLogLevels] = @@ -11,8 +11,32 @@ static const char * g_LogLevelStr[eNumLogLevels] =
"debug" // eLogDebug
};
/** 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;
}
}
void LogMsg::Process()
{
if (log && log->SyslogEnabled()) {
// only log to syslog
syslog(ToSyslogLevel(level), "%s", s.str().c_str());
return;
}
auto stream = log ? log->GetLogStream () : nullptr;
auto& output = stream ? *stream : std::cout;
if (log)
@ -84,3 +108,20 @@ void Log::SetLogStream (std::shared_ptr<std::ostream> logStream) @@ -84,3 +108,20 @@ void Log::SetLogStream (std::shared_ptr<std::ostream> logStream)
{
m_LogStream = logStream;
}
void Log::StartSyslog(const std::string & ident, const int facility)
{
m_Ident = ident;
openlog(m_Ident.c_str(), LOG_PID, facility);
}
void Log::StopSyslog()
{
closelog();
m_Ident.clear();
}
bool Log::SyslogEnabled()
{
return m_Ident.size() > 0;
}

27
Log.h

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include <functional>
#include <chrono>
#include <memory>
#include <syslog.h>
#include "Queue.h"
enum LogLevel
@ -45,14 +46,19 @@ class Log: public i2p::util::MsgQueue<LogMsg> @@ -45,14 +46,19 @@ class Log: public i2p::util::MsgQueue<LogMsg>
std::shared_ptr<std::ostream> GetLogStream () const { return m_LogStream; };
const std::string& GetTimestamp ();
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 = LOG_USER);
/** stop logging to syslog */
void StopSyslog();
/** are we logging to syslog right now? */
bool SyslogEnabled();
private:
void Flush ();
private:
std::string m_FullFilePath; // empty if stream
std::shared_ptr<std::ostream> m_LogStream;
enum LogLevel m_MinLevel;
@ -61,7 +67,8 @@ class Log: public i2p::util::MsgQueue<LogMsg> @@ -61,7 +67,8 @@ class Log: public i2p::util::MsgQueue<LogMsg>
std::chrono::monotonic_clock::time_point m_LastTimestampUpdate;
#else
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
#endif
#endif
std::string m_Ident;
};
extern Log * g_Log;
@ -116,6 +123,18 @@ inline bool IsLogToFile () @@ -116,6 +123,18 @@ inline bool IsLogToFile ()
return g_Log ? !g_Log->GetFullFilePath ().empty () : false;
}
inline void StartSyslog()
{
StartLog("");
g_Log->StartSyslog("i2pd");
}
inline void StopSyslog()
{
if(g_Log)
g_Log->StopSyslog();
}
template<typename TValue>
void LogPrint (std::stringstream& s, TValue arg)
{

7
docs/i2pd.conf

@ -16,7 +16,10 @@ @@ -16,7 +16,10 @@
## Logging configuration section
## 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
## Path to logfile (default - autodetect)
#logfile = /var/log/i2pd.log
@ -40,7 +43,7 @@ @@ -40,7 +43,7 @@
## don't just uncomment this
#port = 4321
##Enable communication through ipv6
ipv6
ipv6 = true
## Bandwidth configuration
## L limit bandwidth to 32Kbs/sec, O - to 256Kbs/sec, P - unlimited
## Default is P for floodfill, L for regular node

Loading…
Cancel
Save