From a1523fcb062c89da688f586aa83ff5413eaf6e8c Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 18 Jun 2012 20:05:12 -0700 Subject: [PATCH] Fix log chattiness. Add a new configuration parameter "debug_logfile" --- newcoind.cfg | 8 ++++++++ src/Application.cpp | 5 ++++- src/Config.cpp | 4 ++++ src/Config.h | 1 + src/Log.cpp | 31 ++++++++++++++++++++++++------- src/Log.h | 3 +++ 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/newcoind.cfg b/newcoind.cfg index b97fef5f2a..bc846fa14c 100644 --- a/newcoind.cfg +++ b/newcoind.cfg @@ -31,6 +31,11 @@ # Note: $XDG_CONFIG_HOME defaults to $HOME/.config # $XDG_DATA_HOME defaults to $HOME/.local/share # +# [debug_logfile] +# Specifies were a debug logfile is kept. By default, no debug log is kept +# +# Example: debug.log +# # [validators_site]: # Specifies where to find validators.txt for UNL boostrapping and RPC command unl_network. # During alpha testing, this defaults to: redstem.com @@ -117,6 +122,9 @@ [rpc_allow_remote] 1 +[debug_logfile] +debug.log + [validation_seed] snTBDmrhUK3znvF8AaQURLm4UCkbS diff --git a/src/Application.cpp b/src/Application.cpp index 9e4cdd5ec8..4300f0bf4b 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -12,6 +12,7 @@ #include "utils.h" #include "TaggedCache.h" #include "boost/filesystem.hpp" +#include "Log.h" Application* theApp = NULL; @@ -53,7 +54,9 @@ void Application::stop() void Application::run() { - assert(mTxnDB==NULL); + assert(mTxnDB == NULL); + if (!theConfig.DEBUG_LOGFILE.empty()) + Log::setLogFile(theConfig.DEBUG_LOGFILE); // // Construct databases. diff --git a/src/Config.cpp b/src/Config.cpp index f399b05965..be65c19846 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -7,6 +7,7 @@ #include #define SECTION_ACCOUNT_PROBE_MAX "account_probe_max" +#define SECTION_DEBUG_LOGFILE "debug_logfile" #define SECTION_FEE_ACCOUNT_CREATE "fee_account_create" #define SECTION_FEE_DEFAULT "fee_default" #define SECTION_FEE_NICKNAME_CREATE "fee_nickname_create" @@ -233,6 +234,9 @@ void Config::load() if (sectionSingleB(secConfig, SECTION_UNL_DEFAULT, strTemp)) UNL_DEFAULT = strTemp; + + if (sectionSingleB(secConfig, SECTION_DEBUG_LOGFILE, strTemp)) + DEBUG_LOGFILE = strTemp; } } } diff --git a/src/Config.h b/src/Config.h index 693b00d373..452e652988 100644 --- a/src/Config.h +++ b/src/Config.h @@ -47,6 +47,7 @@ public: boost::filesystem::path CONFIG_FILE; boost::filesystem::path CONFIG_DIR; boost::filesystem::path DATA_DIR; + boost::filesystem::path DEBUG_LOGFILE; boost::filesystem::path UNL_DEFAULT; std::string VALIDATORS_SITE; // Where to find validators.txt on the Internet. diff --git a/src/Log.cpp b/src/Log.cpp index ded888a6d6..5efe21a890 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -1,15 +1,15 @@ #include "Log.h" +#include + #include boost::recursive_mutex Log::sLock; -#ifdef DEBUG -LogSeverity Log::sMinSeverity = lsTRACE; -#else -LogSeverity Log::sMinSeverity = lsINFO; -#endif +LogSeverity Log::sMinSeverity = lsWARNING; + +std::ofstream* Log::outStream = NULL; Log::~Log() { @@ -26,9 +26,9 @@ Log::~Log() logMsg += oss.str(); boost::recursive_mutex::scoped_lock sl(sLock); if (mSeverity >= sMinSeverity) - { std::cerr << logMsg << std::endl; - } + if (outStream != NULL) + (*outStream) << logMsg << std::endl; } void Log::setMinSeverity(LogSeverity s) @@ -36,3 +36,20 @@ void Log::setMinSeverity(LogSeverity s) boost::recursive_mutex::scoped_lock sl(sLock); sMinSeverity = s; } + +void Log::setLogFile(boost::filesystem::path path) +{ + std::ofstream* newStream = new std::ofstream(path.c_str(), std::fstream::app); + if (!newStream->good()) + { + delete newStream; + newStream = NULL; + } + + boost::recursive_mutex::scoped_lock sl(sLock); + if (outStream != NULL) + delete outStream; + outStream = newStream; + if (outStream) + Log(lsINFO) << "Starting up"; +} diff --git a/src/Log.h b/src/Log.h index bb5bbc14fb..42c8bb00c9 100644 --- a/src/Log.h +++ b/src/Log.h @@ -4,6 +4,7 @@ #include #include +#include enum LogSeverity { @@ -24,6 +25,7 @@ private: protected: static boost::recursive_mutex sLock; static LogSeverity sMinSeverity; + static std::ofstream* outStream; mutable std::ostringstream oss; LogSeverity mSeverity; @@ -45,6 +47,7 @@ public: } static void setMinSeverity(LogSeverity); + static void setLogFile(boost::filesystem::path); }; #endif