util: Store debug log file path in BCLog::Logger member.

This breaks the cyclic between logging and util.
This commit is contained in:
Jim Posen 2018-04-20 01:11:44 -07:00
parent 8e7b961388
commit 8c2d695c4a
5 changed files with 16 additions and 21 deletions

View file

@ -46,7 +46,6 @@ main(int argc, char** argv)
RandomInit(); RandomInit();
ECC_Start(); ECC_Start();
SetupEnvironment(); SetupEnvironment();
g_logger->m_print_to_file = false; // don't want to write to debug.log file
int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS); int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER); std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);

View file

@ -826,13 +826,15 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&
*/ */
void InitLogging() void InitLogging()
{ {
g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
g_logger->m_file_path = AbsPathForConfigVal(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
// Add newlines to the logfile to distinguish this execution from the last // 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 // one; called before console logging is set up, so this is only sent to
// debug.log. // debug.log.
LogPrintf("\n\n\n\n\n"); LogPrintf("\n\n\n\n\n");
g_logger->m_print_to_console = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false)); g_logger->m_print_to_console = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false));
g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
g_logger->m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS); g_logger->m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
g_logger->m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS); g_logger->m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
@ -1233,7 +1235,7 @@ bool AppInitMain()
} }
if (!g_logger->OpenDebugLog()) { if (!g_logger->OpenDebugLog()) {
return InitError(strprintf("Could not open debug log file %s", return InitError(strprintf("Could not open debug log file %s",
g_logger->GetDebugLogPath().string())); g_logger->m_file_path.string()));
} }
} }

View file

@ -4,7 +4,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <logging.h> #include <logging.h>
#include <util.h> #include <utiltime.h>
const char * const DEFAULT_DEBUGLOGFILE = "debug.log"; const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
@ -30,20 +30,14 @@ static int FileWriteStr(const std::string &str, FILE *fp)
return fwrite(str.data(), 1, str.size(), fp); return fwrite(str.data(), 1, str.size(), fp);
} }
fs::path BCLog::Logger::GetDebugLogPath() const
{
fs::path logfile(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
return AbsPathForConfigVal(logfile);
}
bool BCLog::Logger::OpenDebugLog() bool BCLog::Logger::OpenDebugLog()
{ {
std::lock_guard<std::mutex> scoped_lock(m_file_mutex); std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
assert(m_fileout == nullptr); assert(m_fileout == nullptr);
fs::path pathDebug = GetDebugLogPath(); assert(!m_file_path.empty());
m_fileout = fsbridge::fopen(pathDebug, "a"); m_fileout = fsbridge::fopen(m_file_path, "a");
if (!m_fileout) { if (!m_fileout) {
return false; return false;
} }
@ -228,8 +222,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
// reopen the log file, if requested // reopen the log file, if requested
if (m_reopen_file) { if (m_reopen_file) {
m_reopen_file = false; m_reopen_file = false;
fs::path pathDebug = GetDebugLogPath(); if (fsbridge::freopen(m_file_path,"a",m_fileout) != nullptr)
if (fsbridge::freopen(pathDebug,"a",m_fileout) != nullptr)
setbuf(m_fileout, nullptr); // unbuffered setbuf(m_fileout, nullptr); // unbuffered
} }
@ -243,14 +236,16 @@ void BCLog::Logger::ShrinkDebugFile()
{ {
// Amount of debug.log to save at end when shrinking (must fit in memory) // Amount of debug.log to save at end when shrinking (must fit in memory)
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
assert(!m_file_path.empty());
// Scroll debug.log if it's getting too big // Scroll debug.log if it's getting too big
fs::path pathLog = GetDebugLogPath(); FILE* file = fsbridge::fopen(m_file_path, "r");
FILE* file = fsbridge::fopen(pathLog, "r");
// Special files (e.g. device nodes) may not have a size. // Special files (e.g. device nodes) may not have a size.
size_t log_size = 0; size_t log_size = 0;
try { try {
log_size = fs::file_size(pathLog); log_size = fs::file_size(m_file_path);
} catch (boost::filesystem::filesystem_error &) {} } catch (boost::filesystem::filesystem_error &) {}
// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
@ -263,7 +258,7 @@ void BCLog::Logger::ShrinkDebugFile()
int nBytes = fread(vch.data(), 1, vch.size(), file); int nBytes = fread(vch.data(), 1, vch.size(), file);
fclose(file); fclose(file);
file = fsbridge::fopen(pathLog, "w"); file = fsbridge::fopen(m_file_path, "w");
if (file) if (file)
{ {
fwrite(vch.data(), 1, nBytes, file); fwrite(vch.data(), 1, nBytes, file);

View file

@ -77,11 +77,12 @@ namespace BCLog {
public: public:
bool m_print_to_console = false; bool m_print_to_console = false;
bool m_print_to_file = true; bool m_print_to_file = false;
bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS; bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
bool m_log_time_micros = DEFAULT_LOGTIMEMICROS; bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
fs::path m_file_path;
std::atomic<bool> m_reopen_file{false}; std::atomic<bool> m_reopen_file{false};
/** Send a string to the log output */ /** Send a string to the log output */
@ -90,7 +91,6 @@ namespace BCLog {
/** Returns whether logs will be written to any output */ /** Returns whether logs will be written to any output */
bool Enabled() const { return m_print_to_console || m_print_to_file; } bool Enabled() const { return m_print_to_console || m_print_to_file; }
fs::path GetDebugLogPath() const;
bool OpenDebugLog(); bool OpenDebugLog();
void ShrinkDebugFile(); void ShrinkDebugFile();

View file

@ -47,7 +47,6 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
SetupNetworking(); SetupNetworking();
InitSignatureCache(); InitSignatureCache();
InitScriptExecutionCache(); InitScriptExecutionCache();
g_logger->m_print_to_file = false; // don't want to write to debug.log file
fCheckBlockIndex = true; fCheckBlockIndex = true;
SelectParams(chainName); SelectParams(chainName);
noui_connect(); noui_connect();