Skip to content

Commit

Permalink
util: Move debug file management functions into Logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Apr 11, 2020
1 parent 5a42d82 commit a2fb3fd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 54 deletions.
9 changes: 5 additions & 4 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,10 +1066,11 @@ bool AppInit2()
#ifndef WIN32
CreatePidFile(GetPidFile(), getpid());
#endif
if (GetBoolArg("-shrinkdebugfile", logCategories != BCLog::NONE))
ShrinkDebugFile();
if (g_logger->fPrintToDebugLog && !OpenDebugLog()) {
return UIError(strprintf("Could not open debug log file %s", GetDebugLogPath().string()));
if (g_logger->fPrintToDebugLog) {
if (GetBoolArg("-shrinkdebugfile", logCategories != BCLog::NONE))
g_logger->ShrinkDebugFile();
if (!g_logger->OpenDebugLog())
return UIError(strprintf("Could not open debug log file %s", g_logger->GetDebugLogPath().string()));
}
#ifdef ENABLE_WALLET
LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0));
Expand Down
55 changes: 9 additions & 46 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#include "util.h"
#include "utilstrencodings.h"

#include <list>
#include <mutex>

#include <boost/filesystem/fstream.hpp>

const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
Expand All @@ -38,40 +35,12 @@ bool fLogIPs = DEFAULT_LOGIPS;
/** Log categories bitfield. Leveldb/libevent need special handling if their flags are changed at runtime. */
std::atomic<uint32_t> logCategories(0);

/**
* LogPrintf() has been broken a couple of times now
* by well-meaning people adding mutexes in the most straightforward way.
* It breaks because it may be called by global destructors during shutdown.
* Since the order of destruction of static/global objects is undefined,
* defining a mutex as a global object doesn't work (the mutex gets
* destroyed, and then some later destructor calls OutputDebugStringF,
* maybe indirectly, and you get a core dump at shutdown trying to lock
* the mutex).
*/

static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;

/**
* We use boost::call_once() to make sure these are initialized
* in a thread-safe manner the first time called:
*/
static FILE* fileout = nullptr;
static boost::mutex* mutexDebugLog = nullptr;
static std::list<std::string> *vMsgsBeforeOpenLog;

static int FileWriteStr(const std::string &str, FILE *fp)
{
return fwrite(str.data(), 1, str.size(), fp);
}

static void DebugPrintInit()
{
assert(mutexDebugLog == nullptr);
mutexDebugLog = new boost::mutex();
vMsgsBeforeOpenLog = new std::list<std::string>;
}

fs::path GetDebugLogPath()
fs::path BCLog::Logger::GetDebugLogPath() const
{
fs::path logfile(GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
if (logfile.is_absolute()) {
Expand All @@ -81,12 +50,10 @@ fs::path GetDebugLogPath()
}
}

bool OpenDebugLog()
bool BCLog::Logger::OpenDebugLog()
{
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
std::lock_guard<std::mutex> scoped_lock(mutexDebugLog);
assert(fileout == nullptr);
assert(vMsgsBeforeOpenLog);

fs::path pathDebug = GetDebugLogPath();

Expand All @@ -95,13 +62,11 @@ bool OpenDebugLog()

setbuf(fileout, nullptr); // unbuffered
// dump buffered messages from before we opened the log
while (!vMsgsBeforeOpenLog->empty()) {
FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
vMsgsBeforeOpenLog->pop_front();
while (!vMsgsBeforeOpenLog.empty()) {
FileWriteStr(vMsgsBeforeOpenLog.front(), fileout);
vMsgsBeforeOpenLog.pop_front();
}

delete vMsgsBeforeOpenLog;
vMsgsBeforeOpenLog = nullptr;
return true;
}

Expand Down Expand Up @@ -217,16 +182,14 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
ret = fwrite(str.data(), 1, str.size(), stdout);
fflush(stdout);
} else if (fPrintToDebugLog) {
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
std::lock_guard<std::mutex> scoped_lock(mutexDebugLog);

std::string strTimestamped = LogTimestampStr(str);

// buffer if we haven't opened the log yet
if (fileout == NULL) {
assert(vMsgsBeforeOpenLog);
ret = strTimestamped.length();
vMsgsBeforeOpenLog->push_back(strTimestamped);
vMsgsBeforeOpenLog.push_back(strTimestamped);

} else {
// reopen the log file, if requested
Expand All @@ -244,7 +207,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
return ret;
}

void ShrinkDebugFile()
void BCLog::Logger::ShrinkDebugFile()
{
// Scroll debug.log if it's getting too big
fs::path pathLog = GetDebugLogPath();
Expand Down
15 changes: 11 additions & 4 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include "tinyformat.h"

#include <atomic>
#include <cstdint>
#include <list>
#include <mutex>
#include <vector>

#include <boost/filesystem.hpp>
Expand Down Expand Up @@ -67,6 +70,10 @@ namespace BCLog {
class Logger
{
private:
FILE* fileout = nullptr;
std::mutex mutexDebugLog;
std::list<std::string> vMsgsBeforeOpenLog;

/**
* fStartedNewLine is a state variable that will suppress printing of
* the timestamp when multiple calls are made that don't end in a
Expand All @@ -90,6 +97,10 @@ namespace BCLog {

/** Returns whether logs will be written to any output */
bool Enabled() const { return fPrintToConsole || fPrintToDebugLog; }

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

} // namespace BCLog
Expand Down Expand Up @@ -139,8 +150,4 @@ template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt,
} \
} while(0)

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

#endif // BITCOIN_LOGGING_H

0 comments on commit a2fb3fd

Please sign in to comment.