Skip to content

Commit

Permalink
Added new error.log and optimized the error log ui text area
Browse files Browse the repository at this point in the history
  • Loading branch information
mxaddict committed May 5, 2019
1 parent 400dbc3 commit 3d24bae
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void HandleSIGTERM(int)

void HandleSIGHUP(int)
{
fReopenDebugLog = true;
fReopenLogFiles = true;
}

bool static Bind(const CService &addr, unsigned int flags) {
Expand Down
37 changes: 30 additions & 7 deletions src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <QString>
#include <QStringList>
#include <QTextStream>
#include <QTextCursor>
#include <QThread>
#include <QTime>
#include <QTimer>
Expand Down Expand Up @@ -320,8 +321,9 @@ RPCConsole::~RPCConsole()

void RPCConsole::errorLogInitPos()
{
error("errorLogInitPos");
// Get a QFile instance
errorLogFile = new QFile(QString::fromStdString(GetDebugLogPath().string()));
errorLogFile = new QFile(QString::fromStdString(GetErrorLogPath().string()));

// Try to open file
if (!errorLogFile->open(QFile::ReadOnly | QFile::Text))
Expand Down Expand Up @@ -370,15 +372,10 @@ void RPCConsole::errorLogRefresh()
QTextStream in(errorLogFile);

// Load up the lines
QString line;
QString logLines = "";
while (!in.atEnd()) {
// Load the next line
line = in.readLine() + "\n";

// Check if it's an error
if (line.contains("error", Qt::CaseInsensitive) && !line.contains("ErrorFile"))
logLines += line; // Use the line
logLines += in.readLine() + "\n";
}

// Check if we have lines
Expand All @@ -387,6 +384,32 @@ void RPCConsole::errorLogRefresh()
ui->errorLogTextBrowser->textCursor().insertText(logLines);
}

// Count the lines in the UI textarea
int uiLineCount = ui->errorLogTextBrowser->document()->lineCount();

// Check if lines are more than ERROR_LOG_INITIAL_COUNT
if (uiLineCount > ERROR_LOG_INITIAL_COUNT) {
// Count how many to remove
int lineCountDiff = uiLineCount - ERROR_LOG_INITIAL_COUNT;

// Get our cursor
QTextCursor cursor = ui->errorLogTextBrowser->textCursor();

// REMOVE THEM
for (int i = 0; i < lineCountDiff; i++) {
cursor.movePosition(QTextCursor::Start);
cursor.select(QTextCursor::LineUnderCursor);
cursor.deleteChar(); // Remove the selected text
cursor.deleteChar(); // This is by design, this removes the \n
}

// Replace the cursor
ui->errorLogTextBrowser->setTextCursor(cursor);

// Move cursor back to the end
ui->errorLogTextBrowser->moveCursor(QTextCursor::End);
}

// Mark as done
errorLogRefreshing = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/qt/ui_getaddresstoreceive.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/********************************************************************************
** Form generated from reading UI file 'getaddresstoreceive.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
** Created by: Qt User Interface Compiler version 5.7.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/qt/ui_navtechsetup.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/********************************************************************************
** Form generated from reading UI file 'navtechsetup.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
** Created by: Qt User Interface Compiler version 5.7.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
Expand Down
84 changes: 59 additions & 25 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ string strMiscWarning;
bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
bool fLogIPs = DEFAULT_LOGIPS;
std::atomic<bool> fReopenDebugLog(false);
std::atomic<bool> fReopenLogFiles(false);
CTranslationInterface translationInterface;

/** Init OpenSSL library multithreading support */
Expand Down Expand Up @@ -193,12 +193,13 @@ static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
* We use boost::call_once() to make sure mutexDebugLog and
* vMsgsBeforeOpenLog are initialized in a thread-safe manner.
*
* NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
* are leaked on exit. This is ugly, but will be cleaned up by
* the OS/libc. When the shutdown sequence is fully audited and
* NOTE: fileoutDebugLog, fileoutErrorLog, mutexDebugLog and sometimes
* vMsgsBeforeOpenLog 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* fileout = NULL;
static FILE* fileoutDebugLog = NULL;
static FILE* fileoutErrorLog = NULL;
static boost::mutex* mutexDebugLog = NULL;
static list<string> *vMsgsBeforeOpenLog;

Expand All @@ -207,6 +208,23 @@ static int FileWriteStr(const std::string &str, FILE *fp)
return fwrite(str.data(), 1, str.size(), fp);
}

static int LogWriteStr(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 size;
}

static void DebugPrintInit()
{
assert(mutexDebugLog == NULL);
Expand All @@ -219,15 +237,20 @@ void OpenDebugLog()
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);

assert(fileout == NULL);
assert(fileoutDebugLog == NULL);
assert(vMsgsBeforeOpenLog);
boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
fileout = fopen(pathDebug.string().c_str(), "a");
if (fileout) setbuf(fileout, NULL); // unbuffered

// Open the debug log
fileoutDebugLog = fopen(GetDebugLogPath().string().c_str(), "a");
if (fileoutDebugLog) setbuf(fileoutDebugLog, NULL); // unbuffered

// Open the error log
fileoutErrorLog = fopen(GetErrorLogPath().string().c_str(), "a");
if (fileoutErrorLog) setbuf(fileoutErrorLog, NULL); // unbuffered

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

Expand Down Expand Up @@ -298,6 +321,11 @@ boost::filesystem::path GetDebugLogPath()
return GetDataDir() / "debug.log";
}

boost::filesystem::path GetErrorLogPath()
{
return GetDataDir() / "error.log";
}

int LogPrintStr(const std::string &str)
{
int ret = 0; // Returns total number of characters written
Expand All @@ -317,24 +345,25 @@ int LogPrintStr(const std::string &str)
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);

// buffer if we haven't opened the log yet
if (fileout == NULL) {
if (fileoutDebugLog == NULL) {
assert(vMsgsBeforeOpenLog);
ret = strTimestamped.length();
vMsgsBeforeOpenLog->push_back(strTimestamped);
}
else
{
// reopen the log file, if requested
if (fReopenDebugLog) {
fReopenDebugLog = false;
boost::filesystem::path pathDebug = GetDebugLogPath();
if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
setbuf(fileout, NULL); // unbuffered
if (fReopenLogFiles) {
fReopenLogFiles = false;

// Open the log files
OpenDebugLog();
}

ret = FileWriteStr(strTimestamped, fileout);
ret = LogWriteStr(strTimestamped);
}
}

return ret;
}

Expand Down Expand Up @@ -747,19 +776,19 @@ bool TryCreateDirectory(const boost::filesystem::path& p)
return false;
}

void FileCommit(FILE *fileout)
void FileCommit(FILE *file)
{
fflush(fileout); // harmless if redundantly called
fflush(file); // harmless if redundantly called
#ifdef WIN32
HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
FlushFileBuffers(hFile);
#else
#if defined(__linux__) || defined(__NetBSD__)
fdatasync(fileno(fileout));
fdatasync(fileno(file));
#elif defined(__APPLE__) && defined(F_FULLFSYNC)
fcntl(fileno(fileout), F_FULLFSYNC, 0);
fcntl(fileno(file), F_FULLFSYNC, 0);
#else
fsync(fileno(fileout));
fsync(fileno(file));
#endif
#endif
}
Expand Down Expand Up @@ -842,11 +871,16 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
}

void ShrinkDebugFile()
{
ShrinkDebugFile(GetDebugLogPath(), 10); // Shrink the debug log
ShrinkDebugFile(GetErrorLogPath(), 2); // Shrink the error log
}

void ShrinkDebugFile(boost::filesystem::path pathLog, int maxSize)
{
// Scroll debug.log if it's getting too big
boost::filesystem::path pathLog = GetDataDir() / "debug.log";
FILE* file = fopen(pathLog.string().c_str(), "r");
if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
if (file && boost::filesystem::file_size(pathLog) > maxSize * 1000000)
{
// Restart the file with some of the end
std::vector <char> vch(200000,0);
Expand Down
8 changes: 6 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extern std::string strMiscWarning;
extern bool fLogTimestamps;
extern bool fLogTimeMicros;
extern bool fLogIPs;
extern std::atomic<bool> fReopenDebugLog;
extern std::atomic<bool> fReopenLogFiles;
extern CTranslationInterface translationInterface;

extern const char * const NAVCOIN_CONF_FILENAME;
Expand All @@ -76,6 +76,9 @@ bool LogAcceptCategory(const char* category);
/** Returns the path to the debug.log file */
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);

Expand Down Expand Up @@ -123,7 +126,7 @@ static inline bool error(std::string s)
}
void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
void ParseParameters(int argc, const char*const argv[]);
void FileCommit(FILE *fileout);
void FileCommit(FILE *file);
bool TruncateFile(FILE *file, unsigned int length);
int RaiseFileDescriptorLimit(int nMinFD);
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
Expand All @@ -147,6 +150,7 @@ boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
#endif
void OpenDebugLog();
void ShrinkDebugFile();
void ShrinkDebugFile(boost::filesystem::path pathLog, int maxSize);
void runCommand(const std::string& strCommand);

inline bool IsSwitchChar(char c)
Expand Down

0 comments on commit 3d24bae

Please sign in to comment.