mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-05-18 13:21:47 +02:00
add syslog logging option
This commit is contained in:
parent
53b7eba31a
commit
5261a3e845
4 changed files with 73 additions and 7 deletions
41
Log.cpp
41
Log.cpp
|
@ -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)
|
|||
{
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue