diff options
author | Evan Klitzke <evan@eklitzke.org> | 2018-03-26 21:35:35 -0700 |
---|---|---|
committer | practicalswift <practicalswift@users.noreply.github.com> | 2018-04-17 09:58:05 +0200 |
commit | 6a3b0d3d1aee1ab924f30b9910bc517c764917cd (patch) | |
tree | bd324a04e7e50a1851a98c04d28e90eb92be6e6d /src | |
parent | 07825088f9cfd8abece774b9d978c36ab90ce3d1 (diff) |
Print to console by default when not run with -daemon
Printing to the debug log file can be disabled with -nodebulogfile
Diffstat (limited to 'src')
-rw-r--r-- | src/init.cpp | 26 | ||||
-rw-r--r-- | src/util.cpp | 15 |
2 files changed, 28 insertions, 13 deletions
diff --git a/src/init.cpp b/src/init.cpp index abff5ec79d..9174413610 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -815,14 +815,25 @@ static std::string ResolveErrMsg(const char * const optname, const std::string& return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind); } +/** + * Initialize global loggers. + * + * Note that this is called very early in the process lifetime, so you should be + * careful about what global state you rely on here. + */ void InitLogging() { - fPrintToConsole = gArgs.GetBoolArg("-printtoconsole", false); + // Add newlines to the logfile to distinguish this execution from the last + // one; called before console logging is set up, so this is only sent to + // debug.log. + LogPrintf("\n\n\n\n\n"); + + fPrintToConsole = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false)); + fPrintToDebugLog = !gArgs.IsArgNegated("-debuglogfile"); fLogTimestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS); fLogTimeMicros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS); fLogIPs = gArgs.GetBoolArg("-logips", DEFAULT_LOGIPS); - LogPrintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); std::string version_string = FormatFullVersion(); #ifdef DEBUG version_string += " (debug build)"; @@ -1216,13 +1227,12 @@ bool AppInitMain() #ifndef WIN32 CreatePidFile(GetPidFile(), getpid()); #endif - if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) { - // Do this first since it both loads a bunch of debug.log into memory, - // and because this needs to happen before any other debug.log printing - ShrinkDebugFile(); - } - if (fPrintToDebugLog) { + if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) { + // Do this first since it both loads a bunch of debug.log into memory, + // and because this needs to happen before any other debug.log printing + ShrinkDebugFile(); + } if (!OpenDebugLog()) { return InitError(strprintf("Could not open debug log file %s", GetDebugLogPath().string())); } diff --git a/src/util.cpp b/src/util.cpp index 1fb40ae7a1..6b0bffa35a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -341,14 +341,12 @@ int LogPrintStr(const std::string &str) std::string strTimestamped = LogTimestampStr(str, &fStartedNewLine); - if (fPrintToConsole) - { + if (fPrintToConsole) { // print to console ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); fflush(stdout); } - else if (fPrintToDebugLog) - { + if (fPrintToDebugLog) { std::call_once(debugPrintInitFlag, &DebugPrintInit); std::lock_guard<std::mutex> scoped_lock(*mutexDebugLog); @@ -1126,9 +1124,16 @@ void ShrinkDebugFile() // Scroll debug.log if it's getting too big fs::path pathLog = GetDebugLogPath(); FILE* file = fsbridge::fopen(pathLog, "r"); + + // Special files (e.g. device nodes) may not have a size. + size_t log_size = 0; + try { + log_size = fs::file_size(pathLog); + } catch (boost::filesystem::filesystem_error &) {} + // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes - if (file && fs::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) + if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) { // Restart the file with some of the end std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0); |