Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error log tab in debug window #466

Merged
merged 7 commits into from
May 30, 2019
Merged
1 change: 0 additions & 1 deletion depends/packages/qt.mk
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ $(package)_config_opts += -no-feature-concurrent
$(package)_config_opts += -no-feature-sql
$(package)_config_opts += -no-feature-statemachine
$(package)_config_opts += -no-feature-syntaxhighlighter
$(package)_config_opts += -no-feature-textbrowser
$(package)_config_opts += -no-feature-textodfwriter
$(package)_config_opts += -no-feature-udpsocket
$(package)_config_opts += -no-feature-xml
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ void HandleSIGTERM(int)

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

bool static Bind(const CService &addr, unsigned int flags) {
Expand Down
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
137 changes: 137 additions & 0 deletions 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,16 +29,25 @@
#include <db_cxx.h>
#endif

#include <QFile>
#include <QKeyEvent>
#include <QMenu>
#include <QScrollBar>
#include <QSettings>
#include <QSignalMapper>

#include <QString>
#include <QStringList>
#include <QTextStream>
#include <QTextCursor>
#include <QThread>
#include <QTime>
#include <QTimer>

#if QT_VERSION < 0x050000
#include <QUrl>
#endif

// TODO: add a scrollback limit, as there is currently none
// TODO: make it possible to filter out categories (esp debug messages when implemented)
// TODO: receive errors and debug messages through ClientModel
Expand Down Expand Up @@ -290,6 +300,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 @@ -300,10 +315,115 @@ RPCConsole::~RPCConsole()
GUIUtil::saveWindowGeometry("nRPCConsoleWindow", this);
Q_EMIT stopExecutor();
RPCUnsetTimerInterface(rpcTimerInterface);
errorLogFile->close();
delete rpcTimerInterface;
delete ui;
}

void RPCConsole::errorLogInitPos()
{
// Check if we already have the file
if (errorLogFile != NULL) {
// Get a QFile instance
errorLogFile = new QFile(QString::fromStdString(GetErrorLogPath().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);

// Clear the textarea
ui->errorLogTextBrowser->setText("");

// Mark init 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 logLines = "";
while (!in.atEnd()) {
// Load the next line
logLines += in.readLine() + "\n";
}

// Check if we have lines
if (logLines != "") {
// Add the new lines, purpose of duplicate moveCursor calls is
// to auto scroll to the end of the log instead of sticking to the
// top of the text area
ui->errorLogTextBrowser->moveCursor(QTextCursor::End);
ui->errorLogTextBrowser->textCursor().insertText(logLines);
ui->errorLogTextBrowser->moveCursor(QTextCursor::End);
}

// 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;
}

bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
{
if(event->type() == QEvent::KeyPress) // Special key handling
Expand Down Expand Up @@ -672,6 +792,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 @@ -837,6 +962,15 @@ void RPCConsole::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);

// Mark init as not done
errorLogInitPosDone = false;

// Load error log initial data
errorLogRefresh();

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

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

Expand All @@ -848,6 +982,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
Loading