Skip to content

Commit

Permalink
Replaced the prefix checking code with dedicated function calls from …
Browse files Browse the repository at this point in the history
…error( to insert in error.log
  • Loading branch information
mxaddict committed May 7, 2019
1 parent cafacd6 commit 95e8db0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ RPCConsole::~RPCConsole()

void RPCConsole::errorLogInitPos()
{
error("LOADING errorLogInitPos");
// Check if we already have the file
if (errorLogFile != NULL) {
// Get a QFile instance
Expand Down
115 changes: 87 additions & 28 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,57 +188,66 @@ instance_of_cinit;
*/

static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
static boost::once_flag errorPrintInitFlag = BOOST_ONCE_INIT;

/**
* We use boost::call_once() to make sure mutexDebugLog and
* vMsgsBeforeOpenLog are initialized in a thread-safe manner.
* vMsgsBeforeOpenDebugLog are initialized in a thread-safe manner.
*
* NOTE: fileoutDebugLog, fileoutErrorLog, mutexDebugLog and sometimes
* vMsgsBeforeOpenLog are leaked on exit. This is ugly, but will be cleaned
* vMsgsBeforeOpenDebugLog are leaked on exit. This is ugly, but will be cleaned
* up by the OS/libc. When the shutdown sequence is fully audited and
* tested, explicit destruction of these objects can be implemented.
*/
static FILE* fileoutDebugLog = NULL;
static FILE* fileoutErrorLog = NULL;
static boost::mutex* mutexDebugLog = NULL;
static list<string> *vMsgsBeforeOpenLog;
static boost::mutex* mutexErrorLog = NULL;
static list<string> *vMsgsBeforeOpenDebugLog;
static list<string> *vMsgsBeforeOpenErrorLog;

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

static int LogWriteStr(const std::string &str)
static int DebugLogWriteStr(const std::string &str)
{
// Save the size of written data
int size = FileWriteStr(str, fileoutDebugLog); // write to debug log

// What is our prefix
std::string prefix = "ERROR";

// Check if we need to write to error log
if (str.find(prefix) != string::npos) {
FileWriteStr(str, fileoutErrorLog); // write to error log
}
// Return the int from the write size
return FileWriteStr(str, fileoutDebugLog); // write to debug log
}

static int ErrorLogWriteStr(const std::string &str)
{
// Return the int from the write size
return size;
return FileWriteStr(str, fileoutErrorLog); // write to error log
}

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

static void ErrorPrintInit()
{
assert(mutexErrorLog == NULL);
mutexErrorLog = new boost::mutex();
vMsgsBeforeOpenErrorLog = new list<string>;
}

void OpenDebugLog()
{
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
boost::call_once(&ErrorPrintInit, errorPrintInitFlag);
boost::mutex::scoped_lock scoped_lock_debug(*mutexDebugLog);
boost::mutex::scoped_lock scoped_lock_error(*mutexErrorLog);

assert(fileoutDebugLog == NULL);
assert(vMsgsBeforeOpenLog);
assert(fileoutErrorLog == NULL);
assert(vMsgsBeforeOpenDebugLog);
assert(vMsgsBeforeOpenErrorLog);

// Open the debug log
fileoutDebugLog = fopen(GetDebugLogPath().string().c_str(), "a");
Expand All @@ -249,13 +258,21 @@ void OpenDebugLog()
if (fileoutErrorLog) setbuf(fileoutErrorLog, NULL); // unbuffered

// dump buffered messages from before we opened the log
while (!vMsgsBeforeOpenLog->empty()) {
LogWriteStr(vMsgsBeforeOpenLog->front()); // write to log
vMsgsBeforeOpenLog->pop_front();
while (!vMsgsBeforeOpenDebugLog->empty()) {
DebugLogWriteStr(vMsgsBeforeOpenDebugLog->front()); // write to log
vMsgsBeforeOpenDebugLog->pop_front();
}

// dump buffered messages from before we opened the log
while (!vMsgsBeforeOpenErrorLog->empty()) {
ErrorLogWriteStr(vMsgsBeforeOpenErrorLog->front()); // write to log
vMsgsBeforeOpenErrorLog->pop_front();
}

delete vMsgsBeforeOpenLog;
vMsgsBeforeOpenLog = NULL;
delete vMsgsBeforeOpenDebugLog;
delete vMsgsBeforeOpenErrorLog;
vMsgsBeforeOpenDebugLog = NULL;
vMsgsBeforeOpenErrorLog = NULL;
}

bool LogAcceptCategory(const char* category)
Expand Down Expand Up @@ -284,6 +301,7 @@ bool LogAcceptCategory(const char* category)
setCategories.count(string(category)) == 0)
return false;
}

return true;
}

Expand Down Expand Up @@ -326,7 +344,7 @@ boost::filesystem::path GetErrorLogPath()
return GetDataDir() / "error.log";
}

int LogPrintStr(const std::string &str)
int DebugLogPrintStr(const std::string &str)
{
int ret = 0; // Returns total number of characters written
static bool fStartedNewLine = true;
Expand All @@ -342,13 +360,54 @@ int LogPrintStr(const std::string &str)
else if (fPrintToDebugLog)
{
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
boost::mutex::scoped_lock scoped_lock_debug(*mutexDebugLog);

// buffer if we haven't opened the log yet
if (fileoutDebugLog == NULL) {
assert(vMsgsBeforeOpenLog);
assert(vMsgsBeforeOpenDebugLog);
ret = strTimestamped.length();
vMsgsBeforeOpenDebugLog->push_back(strTimestamped);
}
else
{
// reopen the log file, if requested
if (fReopenLogFiles) {
fReopenLogFiles = false;

// Open the log files
OpenDebugLog();
}

ret = DebugLogWriteStr(strTimestamped);
}
}

return ret;
}

int ErrorLogPrintStr(const std::string &str)
{
int ret = 0; // Returns total number of characters written
static bool fStartedNewLine = true;

string strTimestamped = LogTimestampStr(str, &fStartedNewLine);

if (fPrintToConsole)
{
// print to console
ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
fflush(stdout);
}
else if (fPrintToDebugLog)
{
boost::call_once(&ErrorPrintInit, errorPrintInitFlag);
boost::mutex::scoped_lock scoped_lock_error(*mutexErrorLog);

// buffer if we haven't opened the log yet
if (fileoutErrorLog == NULL) {
assert(vMsgsBeforeOpenErrorLog);
ret = strTimestamped.length();
vMsgsBeforeOpenLog->push_back(strTimestamped);
vMsgsBeforeOpenErrorLog->push_back(strTimestamped);
}
else
{
Expand All @@ -360,7 +419,7 @@ int LogPrintStr(const std::string &str)
OpenDebugLog();
}

ret = LogWriteStr(strTimestamped);
ret = ErrorLogWriteStr(strTimestamped);
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ boost::filesystem::path GetDebugLogPath();
/** Returns the path to the error.log file */
boost::filesystem::path GetErrorLogPath();

/** Send a string to the log output */
int LogPrintStr(const std::string &str);
/** Send a string to the debug log output */
int DebugLogPrintStr(const std::string &str);

/** Send a string to the error log output */
int ErrorLogPrintStr(const std::string &str);

#define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)

Expand All @@ -94,14 +97,14 @@ static inline int LogPrint(const char* category, const char* fmt, const T1& v1,
} catch (std::runtime_error &e) {
_log_msg_ = "Error \"" + std::string(e.what()) + "\" while formatting log message: " + fmt;
}
return LogPrintStr(_log_msg_);

return DebugLogPrintStr(_log_msg_);
}

template<typename T1, typename... Args>
bool error(const char* fmt, const T1& v1, const Args&... args)
{
LogPrintStr("ERROR: " + tfm::format(fmt, v1, args...) + "\n");
ErrorLogPrintStr("ERROR: " + tfm::format(fmt, v1, args...) + "\n");
return false;
}

Expand All @@ -113,11 +116,11 @@ bool error(const char* fmt, const T1& v1, const Args&... args)
static inline int LogPrint(const char* category, const char* s)
{
if(!LogAcceptCategory(category)) return 0;
return LogPrintStr(s);
return DebugLogPrintStr(s);
}
static inline bool error(const char* s)
{
LogPrintStr(std::string("ERROR: ") + s + "\n");
ErrorLogPrintStr(std::string("ERROR: ") + s + "\n");
return false;
}
static inline bool error(std::string s)
Expand Down

0 comments on commit 95e8db0

Please sign in to comment.