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

@@ -4,7 +4,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <logging.h>
#include <util.h>
#include <utiltime.h>
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);
}
fs::path BCLog::Logger::GetDebugLogPath() const
{
fs::path logfile(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
return AbsPathForConfigVal(logfile);
}
bool BCLog::Logger::OpenDebugLog()
{
std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
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) {
return false;
}
@@ -228,8 +222,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
// reopen the log file, if requested
if (m_reopen_file) {
m_reopen_file = false;
fs::path pathDebug = GetDebugLogPath();
if (fsbridge::freopen(pathDebug,"a",m_fileout) != nullptr)
if (fsbridge::freopen(m_file_path,"a",m_fileout) != nullptr)
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)
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
assert(!m_file_path.empty());
// Scroll debug.log if it's getting too big
fs::path pathLog = GetDebugLogPath();
FILE* file = fsbridge::fopen(pathLog, "r");
FILE* file = fsbridge::fopen(m_file_path, "r");
// Special files (e.g. device nodes) may not have a size.
size_t log_size = 0;
try {
log_size = fs::file_size(pathLog);
log_size = fs::file_size(m_file_path);
} catch (boost::filesystem::filesystem_error &) {}
// 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);
fclose(file);
file = fsbridge::fopen(pathLog, "w");
file = fsbridge::fopen(m_file_path, "w");
if (file)
{
fwrite(vch.data(), 1, nBytes, file);