Skip to content

Commit

Permalink
Added error log tab in debug window that parses last 500 debug.log en…
Browse files Browse the repository at this point in the history
…tries for errors and loads more errors as they come in while debugwindow is open
  • Loading branch information
mxaddict committed May 4, 2019
1 parent bb51359 commit 400dbc3
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 3 deletions.
36 changes: 36 additions & 0 deletions src/qt/forms/debugwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,42 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_error_log">
<attribute name="title">
<string>&amp;Error Log</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>3</number>
</property>
<property name="bottomMargin">
<number>5</number>
</property>
<item>
<widget class="QTextBrowser" name="errorLogTextBrowser">
<property name="minimumSize">
<size>
<width>100</width>
<height>100</height>
</size>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="errorLogCopyClipboardButton">
<property name="toolTip">
<string>Copy Error Log contents to Clipboard</string>
</property>
<property name="text">
<string>Copy to Clipboard</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
Expand Down
6 changes: 6 additions & 0 deletions src/qt/guiconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
/* Milliseconds between model updates */
static const int MODEL_UPDATE_DELAY = 250;

/* Milliseconds between error log refreshes */
static const int ERROR_LOG_UPDATE_DELAY = 2500;

/* Initial number of debug logs to parse error log entries from */
static const int ERROR_LOG_INITIAL_COUNT = 500;

/* AskPassphraseDialog -- Maximum passphrase length */
static const int MAX_PASSPHRASE_SIZE = 1024;

Expand Down
100 changes: 99 additions & 1 deletion src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "bantablemodel.h"
#include "clientmodel.h"
#include "guiutil.h"
#include "guiconstants.h"
#include "platformstyle.h"
#include "bantablemodel.h"

Expand All @@ -28,15 +29,18 @@
#include <db_cxx.h>
#endif

#include <QFile>
#include <QKeyEvent>
#include <QMenu>
#include <QScrollBar>
#include <QSettings>
#include <QSignalMapper>
#include <QString>
#include <QStringList>
#include <QTextStream>
#include <QThread>
#include <QTime>
#include <QTimer>
#include <QStringList>

#if QT_VERSION < 0x050000
#include <QUrl>
Expand Down Expand Up @@ -294,6 +298,11 @@ RPCConsole::RPCConsole(const PlatformStyle *platformStyle, QWidget *parent) :
ui->detailWidget->hide();
ui->peerHeading->setText(tr("Select a peer to view detailed information."));

// set up timer for auto refresh for error log
errorLogTimer = new QTimer();
connect(errorLogTimer, SIGNAL(timeout()), SLOT(errorLogRefresh()));
errorLogTimer->setInterval(ERROR_LOG_UPDATE_DELAY);

QSettings settings;
consoleFontSize = settings.value(fontSizeSettingsKey, QFontInfo(QFont()).pointSize()).toInt();
clear();
Expand All @@ -304,10 +313,84 @@ RPCConsole::~RPCConsole()
GUIUtil::saveWindowGeometry("nRPCConsoleWindow", this);
Q_EMIT stopExecutor();
RPCUnsetTimerInterface(rpcTimerInterface);
errorLogFile->close();
delete rpcTimerInterface;
delete ui;
}

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

// Try to open file
if (!errorLogFile->open(QFile::ReadOnly | QFile::Text))
return;

// Seek to the end of file
errorLogFile->seek(errorLogFile->size() - 1);

// We need to move the file pos back by ERROR_LOG_INITIAL_COUNT lines
QString ch;
int lineCount = 0;
while ((lineCount < ERROR_LOG_INITIAL_COUNT) && (errorLogFile->pos() > 0))
{
// Load the current character
ch = errorLogFile->read(1);

// Move pos back by 2 spaces
errorLogFile->seek(errorLogFile->pos() - 2);

// Check if we have a newline
if (ch == "\n")
lineCount++; // Count it
}

// Move pos forward by 2 spaces
errorLogFile->seek(errorLogFile->pos() + 2);

// Mark ini as done
errorLogInitPosDone = true;
}

void RPCConsole::errorLogRefresh()
{
// Check if we are still refreshing
if (errorLogRefreshing)
return;

// Set to refreshing
errorLogRefreshing = true;

// Check if we have initialized debug log already
if (!errorLogInitPosDone)
errorLogInitPos();

// Load the stream
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
}

// Check if we have lines
if (logLines != "") {
ui->errorLogTextBrowser->moveCursor(QTextCursor::End);
ui->errorLogTextBrowser->textCursor().insertText(logLines);
}

// Mark as done
errorLogRefreshing = false;
}

bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
{
if(event->type() == QEvent::KeyPress) // Special key handling
Expand Down Expand Up @@ -676,6 +759,11 @@ void RPCConsole::on_tabWidget_currentChanged(int index)
clearSelectedNode();
}

void RPCConsole::on_errorLogCopyClipboardButton_clicked()
{
GUIUtil::setClipboard(ui->errorLogTextBrowser->toPlainText());
}

void RPCConsole::on_openDebugLogfileButton_clicked()
{
GUIUtil::openDebugLogfile();
Expand Down Expand Up @@ -841,6 +929,13 @@ void RPCConsole::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);

// Load error log initial data
errorLogRefresh();

// Start the error log timer
errorLogRefresh();
errorLogTimer->start();

if (!clientModel || !clientModel->getPeerTableModel())
return;

Expand All @@ -852,6 +947,9 @@ void RPCConsole::hideEvent(QHideEvent *event)
{
QWidget::hideEvent(event);

// Stop the error log timer
errorLogTimer->stop();

if (!clientModel || !clientModel->getPeerTableModel())
return;

Expand Down
15 changes: 14 additions & 1 deletion src/qt/rpcconsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

#include "net.h"

#include <QWidget>
#include <QCompleter>
#include <QFile>
#include <QString>
#include <QTimer>
#include <QWidget>

class ClientModel;
class PlatformStyle;
Expand Down Expand Up @@ -61,6 +64,8 @@ private Q_SLOTS:
void on_tabWidget_currentChanged(int index);
/** open the debug.log from the current datadir */
void on_openDebugLogfileButton_clicked();
/** copy the error log text from errorLogTextBrowser */
void on_errorLogCopyClipboardButton_clicked();
/** change the time range of the network traffic graph */
void on_sldGraphRange_valueChanged(int value);
/** update traffic statistics */
Expand Down Expand Up @@ -106,6 +111,10 @@ public Q_SLOTS:
void unbanSelectedNode();
/** set which tab has the focus (is visible) */
void setTabFocus(enum TabTypes tabType);
/** load the error log initial file pos */
void errorLogInitPos();
/** update the error log */
void errorLogRefresh();

Q_SIGNALS:
// For RPC command executor
Expand Down Expand Up @@ -140,6 +149,10 @@ public Q_SLOTS:
QMenu *banTableContextMenu;
int consoleFontSize;
QCompleter *autoCompleter;
QTimer *errorLogTimer;
QFile *errorLogFile;
bool errorLogInitPosDone = false;
bool errorLogRefreshing = false;
};

#endif // NAVCOIN_QT_RPCCONSOLE_H
7 changes: 6 additions & 1 deletion src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine
return strStamped;
}

boost::filesystem::path GetDebugLogPath()
{
return GetDataDir() / "debug.log";
}

int LogPrintStr(const std::string &str)
{
int ret = 0; // Returns total number of characters written
Expand Down Expand Up @@ -322,7 +327,7 @@ int LogPrintStr(const std::string &str)
// reopen the log file, if requested
if (fReopenDebugLog) {
fReopenDebugLog = false;
boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
boost::filesystem::path pathDebug = GetDebugLogPath();
if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
setbuf(fileout, NULL); // unbuffered
}
Expand Down
4 changes: 4 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ bool SetupNetworking();

/** Return true if log accepts specified category */
bool LogAcceptCategory(const char* category);

/** Returns the path to the debug.log file */
boost::filesystem::path GetDebugLogPath();

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

Expand Down

0 comments on commit 400dbc3

Please sign in to comment.