Skip to content

Commit

Permalink
util: Store debug log file path in BCLog::Logger member.
Browse files Browse the repository at this point in the history
This breaks the cyclic between logging and util.

Backports bitcoin/bitcoin@8c2d695
  • Loading branch information
random-zebra committed Apr 11, 2020
1 parent 2f03e85 commit 4daa10a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
20 changes: 16 additions & 4 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,16 +901,28 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&

void InitLogging()
{
g_logger->m_print_to_console = GetBoolArg("-printtoconsole", !GetBoolArg("-daemon", false));
//g_logger->m_print_to_file = !IsArgNegated("-debuglogfile");
g_logger->m_print_to_file = true;
g_logger->m_file_path = AbsPathForConfigVal(GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));

// 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\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

g_logger->m_print_to_console = GetBoolArg("-printtoconsole", !GetBoolArg("-daemon", false));
g_logger->m_log_timestamps = GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
g_logger->m_log_time_micros = GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);

fLogIPs = 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");
LogPrintf("PIVX version %s (%s)\n", FormatFullVersion(), CLIENT_DATE);
std::string version_string = FormatFullVersion();
#ifdef DEBUG
version_string += " (debug build)";
#else
version_string += " (release build)";
#endif
LogPrintf("PIVX version %s (%s)\n", version_string, CLIENT_DATE);
}

/** Initialize pivx.
Expand Down Expand Up @@ -1066,7 +1078,7 @@ bool AppInit2()
if (GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile()))
g_logger->ShrinkDebugFile();
if (!g_logger->OpenDebugLog())
return UIError(strprintf("Could not open debug log file %s", g_logger->GetDebugLogPath().string()));
return UIError(strprintf("Could not open debug log file %s", g_logger->m_file_path.string()));
}
#ifdef ENABLE_WALLET
LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0));
Expand Down
33 changes: 13 additions & 20 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "chainparamsbase.h"
#include "logging.h"
#include "util.h"
#include "utiltime.h"

#include <boost/filesystem/fstream.hpp>

Expand Down Expand Up @@ -36,24 +36,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(GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
if (logfile.is_absolute()) {
return logfile;
} else {
return GetDataDir() / 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_fileout == nullptr);
assert(!m_file_path.empty());

m_fileout = fopen(pathDebug.string().c_str(), "a");
m_fileout = fopen(m_file_path.string().c_str(), "a");
if (!m_fileout) return false;

setbuf(m_fileout, nullptr); // unbuffered
Expand Down Expand Up @@ -225,8 +215,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 (freopen(pathDebug.string().c_str(),"a",m_fileout) != NULL)
if (freopen(m_file_path.string().c_str(),"a",m_fileout) != NULL)
setbuf(m_fileout, NULL); // unbuffered
}

Expand All @@ -239,17 +228,21 @@ int BCLog::Logger::LogPrintStr(const std::string &str)

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 = fopen(pathLog.string().c_str(), "r");
if (file && fs::file_size(pathLog) > 10 * 1000000) {
FILE* file = fopen(m_file_path.string().c_str(), "r");
if (file && fs::file_size(m_file_path) > RECENT_DEBUG_HISTORY_SIZE) {
// Restart the file with some of the end
std::vector<char> vch(200000, 0);
fseek(file, -((long)vch.size()), SEEK_END);
int nBytes = fread(vch.data(), 1, vch.size(), file);
fclose(file);

file = fopen(pathLog.string().c_str(), "w");
file = fopen(m_file_path.string().c_str(), "w");
if (file) {
fwrite(vch.data(), 1, nBytes, file);
fclose(file);
Expand Down
4 changes: 2 additions & 2 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ namespace BCLog {

public:
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_time_micros = DEFAULT_LOGTIMEMICROS;

boost::filesystem::path m_file_path;
std::atomic<bool> m_reopen_file{false};

/** Send a string to the log output */
Expand All @@ -99,7 +100,6 @@ namespace BCLog {
/** Returns whether logs will be written to any output */
bool Enabled() const { return m_print_to_console || m_print_to_file; }

boost::filesystem::path GetDebugLogPath() const;
bool OpenDebugLog();
void ShrinkDebugFile();

Expand Down
1 change: 0 additions & 1 deletion src/test/test_pivx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ BasicTestingSetup::BasicTestingSetup()
RandomInit();
ECC_Start();
SetupEnvironment();
g_logger->m_print_to_file = false; // don't want to write to debug.log file
fCheckBlockIndex = true;
SelectParams(CBaseChainParams::MAIN);
}
Expand Down
8 changes: 8 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
ClearDatadirCache();
}

fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
{
if (path.is_absolute()) {
return path;
}
return fs::absolute(path, GetDataDir(net_specific));
}

#ifndef WIN32
fs::path GetPidFile()
{
Expand Down
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@ void TraceThread(const char* name, Callable func)
}
}

boost::filesystem::path AbsPathForConfigVal(const boost::filesystem::path& path, bool net_specific = true);

#endif // BITCOIN_UTIL_H

0 comments on commit 4daa10a

Please sign in to comment.