Skip to content

Commit

Permalink
add loglevel none (closing #998)
Browse files Browse the repository at this point in the history
  • Loading branch information
r4sas committed Nov 15, 2017
1 parent d9b9457 commit a6fb3b6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
3 changes: 2 additions & 1 deletion contrib/i2pd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
# log = file
## Path to logfile (default - autodetect)
# logfile = /var/log/i2pd.log
## Log messages above this level (debug, *info, warn, error)
## Log messages above this level (debug, *info, warn, error, none)
## If you set it to none, logging will be disabled
# loglevel = info

## Path to storage of i2pd data (RI, keys, peer profiles, ...)
Expand Down
37 changes: 21 additions & 16 deletions libi2pd/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ namespace log {
*/
static const char * g_LogLevelStr[eNumLogLevels] =
{
"none", // eLogNone
"error", // eLogError
"warn", // eLogWarn
"info", // eLogInfo
"debug" // eLogDebug
"debug" // eLogDebug
};

/**
Expand All @@ -46,6 +47,7 @@ namespace log {
static inline int GetSyslogPrio (enum LogLevel l) {
int priority = LOG_DEBUG;
switch (l) {
case eLogNone : priority = LOG_CRIT; break;
case eLogError : priority = LOG_ERR; break;
case eLogWarning : priority = LOG_WARNING; break;
case eLogInfo : priority = LOG_INFO; break;
Expand All @@ -71,15 +73,15 @@ namespace log {
void Log::Start ()
{
if (!m_IsRunning)
{
m_IsRunning = true;
{
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&Log::Run, this));
}
}

void Log::Stop ()
void Log::Stop ()
{
switch (m_Destination)
switch (m_Destination)
{
#ifndef _WIN32
case eLogSyslog :
Expand All @@ -97,15 +99,16 @@ namespace log {
m_IsRunning = false;
m_Queue.WakeUp ();
if (m_Thread)
{
m_Thread->join ();
{
m_Thread->join ();
delete m_Thread;
m_Thread = nullptr;
}
}
}

void Log::SetLogLevel (const std::string& level) {
if (level == "error") { m_MinLevel = eLogError; }
if (level == "none") { m_MinLevel = eLogNone; }
else 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; }
Expand All @@ -115,7 +118,7 @@ namespace log {
}
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), m_TimeFormat.c_str(), localtime(&t));
Expand All @@ -129,7 +132,7 @@ namespace log {
* Unfortunately, with current startup process with late fork() this
* will give us nothing but pain. Maybe later. See in NetDb as example.
*/
void Log::Process(std::shared_ptr<LogMsg> msg)
void Log::Process(std::shared_ptr<LogMsg> msg)
{
if (!msg) return;
std::hash<std::thread::id> hasher;
Expand Down Expand Up @@ -171,19 +174,20 @@ namespace log {
if (m_IsRunning)
m_Queue.Wait ();
}
}
}

void Log::Append(std::shared_ptr<i2p::log::LogMsg> & msg)
void Log::Append(std::shared_ptr<i2p::log::LogMsg> & msg)
{
m_Queue.Put(msg);
}

void Log::SendTo (const std::string& path)
void Log::SendTo (const std::string& path)
{
if (m_LogStream) m_LogStream = nullptr; // close previous
if (m_LogStream) m_LogStream = nullptr; // close previous
if (m_MinLevel == eLogNone) return;
auto flags = std::ofstream::out | std::ofstream::app;
auto os = std::make_shared<std::ofstream> (path, flags);
if (os->is_open ())
if (os->is_open ())
{
m_HasColors = false;
m_Logfile = path;
Expand All @@ -202,6 +206,7 @@ namespace log {

#ifndef _WIN32
void Log::SendTo(const char *name, int facility) {
if (m_MinLevel == eLogNone) return;
m_HasColors = false;
m_Destination = eLogSyslog;
m_LogStream = nullptr;
Expand Down
5 changes: 3 additions & 2 deletions libi2pd/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@

enum LogLevel
{
eLogError = 0,
eLogNone = 0,
eLogError,
eLogWarning,
eLogInfo,
eLogDebug,
eLogDebug,
eNumLogLevels
};

Expand Down

0 comments on commit a6fb3b6

Please sign in to comment.