From a0e8fe5848a009d8d6775ab9a1fe503628934bba Mon Sep 17 00:00:00 2001 From: hagen Date: Mon, 28 Dec 2015 00:00:00 +0000 Subject: [PATCH] * implement --loglevel option --- Daemon.cpp | 5 +++-- Log.cpp | 13 +++++++++++++ Log.h | 10 +++++++--- docs/configuration.md | 1 + 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Daemon.cpp b/Daemon.cpp index 3fdc8a48..e2d7d895 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -110,9 +110,10 @@ namespace i2p logfile_path.append("\\i2pd.log"); #endif StartLog (logfile_path); - } - else + } else { StartLog (""); // write to stdout + } + g_Log->SetLogLevel(i2p::util::config::GetArg("-loglevel", "info")); } LogPrint(eLogInfo, "Daemon: staring HTTP Server"); diff --git a/Log.cpp b/Log.cpp index c20e0761..4f31fb31 100644 --- a/Log.cpp +++ b/Log.cpp @@ -55,6 +55,19 @@ void Log::SetLogFile (const std::string& fullFilePath) delete logFile; } +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 msg level set to ", level); +} + void Log::SetLogStream (std::ostream * logStream) { if (m_LogStream) delete m_LogStream; diff --git a/Log.h b/Log.h index 09e2156a..c4b0f276 100644 --- a/Log.h +++ b/Log.h @@ -38,9 +38,11 @@ class Log: public i2p::util::MsgQueue ~Log () { delete m_LogStream; }; void SetLogFile (const std::string& fullFilePath); + void SetLogLevel (const std::string& level); void SetLogStream (std::ostream * logStream); std::ostream * GetLogStream () const { return m_LogStream; }; const std::string& GetTimestamp (); + LogLevel GetLogLevel () { return m_MinLevel; }; private: @@ -49,6 +51,7 @@ class Log: public i2p::util::MsgQueue private: std::ostream * m_LogStream; + enum LogLevel m_MinLevel; std::string m_Timestamp; #if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) && !defined(__clang__) // gcc 4.6 std::chrono::monotonic_clock::time_point m_LastTimestampUpdate; @@ -108,13 +111,14 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args) template void LogPrint (LogLevel level, TArgs... args) { + if (g_Log && level > g_Log->GetLogLevel ()) + return; LogMsg * msg = new LogMsg (g_Log, level); LogPrint (msg->s, args...); msg->s << std::endl; - if (g_Log) + if (g_Log) { g_Log->Put (msg); - else - { + } else { msg->Process (); delete msg; } diff --git a/docs/configuration.md b/docs/configuration.md index 44ec9d8d..3755153a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -6,6 +6,7 @@ i2pd cmdline options * --httpaddress= - The address to listen on (HTTP server) * --httpport= - The port to listen on (HTTP server) * --log= - Enable or disable logging to file. 1 for yes, 0 for no. +* --loglevel= - Log messages above this level (debug, *info, warn, error) * --daemon= - Enable or disable daemon mode. 1 for yes, 0 for no. * --service= - 1 if uses system folders (/var/run/i2pd.pid, /var/log/i2pd.log, /var/lib/i2pd). * --v6= - 1 if supports communication through ipv6, off by default