Skip to content

Commit bed76b9

Browse files
authored
Merge pull request #466 from mxaddict/patch-7
Added error log tab in debug window
2 parents a8f425b + 0675b5a commit bed76b9

File tree

8 files changed

+349
-49
lines changed

8 files changed

+349
-49
lines changed

depends/packages/qt.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ $(package)_config_opts += -no-feature-concurrent
8080
$(package)_config_opts += -no-feature-sql
8181
$(package)_config_opts += -no-feature-statemachine
8282
$(package)_config_opts += -no-feature-syntaxhighlighter
83-
$(package)_config_opts += -no-feature-textbrowser
8483
$(package)_config_opts += -no-feature-textodfwriter
8584
$(package)_config_opts += -no-feature-udpsocket
8685
$(package)_config_opts += -no-feature-xml

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ void HandleSIGTERM(int)
324324

325325
void HandleSIGHUP(int)
326326
{
327-
fReopenDebugLog = true;
327+
fReopenLogFiles = true;
328328
}
329329

330330
bool static Bind(const CService &addr, unsigned int flags) {

src/qt/forms/debugwindow.ui

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,42 @@
15821582
</item>
15831583
</layout>
15841584
</widget>
1585+
<widget class="QWidget" name="tab_error_log">
1586+
<attribute name="title">
1587+
<string>&amp;Error Log</string>
1588+
</attribute>
1589+
<layout class="QVBoxLayout" name="verticalLayout_3">
1590+
<property name="spacing">
1591+
<number>3</number>
1592+
</property>
1593+
<property name="bottomMargin">
1594+
<number>5</number>
1595+
</property>
1596+
<item>
1597+
<widget class="QTextBrowser" name="errorLogTextBrowser">
1598+
<property name="minimumSize">
1599+
<size>
1600+
<width>100</width>
1601+
<height>100</height>
1602+
</size>
1603+
</property>
1604+
<property name="readOnly">
1605+
<bool>true</bool>
1606+
</property>
1607+
</widget>
1608+
</item>
1609+
<item>
1610+
<widget class="QPushButton" name="errorLogCopyClipboardButton">
1611+
<property name="toolTip">
1612+
<string>Copy Error Log contents to Clipboard</string>
1613+
</property>
1614+
<property name="text">
1615+
<string>Copy to Clipboard</string>
1616+
</property>
1617+
</widget>
1618+
</item>
1619+
</layout>
1620+
</widget>
15851621
</widget>
15861622
</item>
15871623
</layout>

src/qt/guiconstants.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
/* Milliseconds between model updates */
99
static const int MODEL_UPDATE_DELAY = 250;
1010

11+
/* Milliseconds between error log refreshes */
12+
static const int ERROR_LOG_UPDATE_DELAY = 2500;
13+
14+
/* Initial number of debug logs to parse error log entries from */
15+
static const int ERROR_LOG_INITIAL_COUNT = 500;
16+
1117
/* AskPassphraseDialog -- Maximum passphrase length */
1218
static const int MAX_PASSPHRASE_SIZE = 1024;
1319

src/qt/rpcconsole.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "bantablemodel.h"
1313
#include "clientmodel.h"
1414
#include "guiutil.h"
15+
#include "guiconstants.h"
1516
#include "platformstyle.h"
1617
#include "bantablemodel.h"
1718

@@ -28,16 +29,25 @@
2829
#include <db_cxx.h>
2930
#endif
3031

32+
#include <QFile>
3133
#include <QKeyEvent>
3234
#include <QMenu>
3335
#include <QScrollBar>
3436
#include <QSettings>
3537
#include <QSignalMapper>
38+
39+
#include <QString>
3640
#include <QStringList>
41+
#include <QTextStream>
42+
#include <QTextCursor>
3743
#include <QThread>
3844
#include <QTime>
3945
#include <QTimer>
4046

47+
#if QT_VERSION < 0x050000
48+
#include <QUrl>
49+
#endif
50+
4151
// TODO: add a scrollback limit, as there is currently none
4252
// TODO: make it possible to filter out categories (esp debug messages when implemented)
4353
// TODO: receive errors and debug messages through ClientModel
@@ -290,6 +300,11 @@ RPCConsole::RPCConsole(const PlatformStyle *platformStyle, QWidget *parent) :
290300
ui->detailWidget->hide();
291301
ui->peerHeading->setText(tr("Select a peer to view detailed information."));
292302

303+
// set up timer for auto refresh for error log
304+
errorLogTimer = new QTimer();
305+
connect(errorLogTimer, SIGNAL(timeout()), SLOT(errorLogRefresh()));
306+
errorLogTimer->setInterval(ERROR_LOG_UPDATE_DELAY);
307+
293308
QSettings settings;
294309
consoleFontSize = settings.value(fontSizeSettingsKey, QFontInfo(QFont()).pointSize()).toInt();
295310
clear();
@@ -300,10 +315,115 @@ RPCConsole::~RPCConsole()
300315
GUIUtil::saveWindowGeometry("nRPCConsoleWindow", this);
301316
Q_EMIT stopExecutor();
302317
RPCUnsetTimerInterface(rpcTimerInterface);
318+
errorLogFile->close();
303319
delete rpcTimerInterface;
304320
delete ui;
305321
}
306322

323+
void RPCConsole::errorLogInitPos()
324+
{
325+
// Check if we already have the file
326+
if (errorLogFile != NULL) {
327+
// Get a QFile instance
328+
errorLogFile = new QFile(QString::fromStdString(GetErrorLogPath().string()));
329+
}
330+
331+
// Try to open file
332+
if (!errorLogFile->open(QFile::ReadOnly | QFile::Text))
333+
return;
334+
335+
// Seek to the end of file
336+
errorLogFile->seek(errorLogFile->size() - 1);
337+
338+
// We need to move the file pos back by ERROR_LOG_INITIAL_COUNT lines
339+
QString ch;
340+
int lineCount = 0;
341+
while ((lineCount < ERROR_LOG_INITIAL_COUNT) && (errorLogFile->pos() > 0))
342+
{
343+
// Load the current character
344+
ch = errorLogFile->read(1);
345+
346+
// Move pos back by 2 spaces
347+
errorLogFile->seek(errorLogFile->pos() - 2);
348+
349+
// Check if we have a newline
350+
if (ch == "\n")
351+
lineCount++; // Count it
352+
}
353+
354+
// Move pos forward by 2 spaces
355+
errorLogFile->seek(errorLogFile->pos() + 2);
356+
357+
// Clear the textarea
358+
ui->errorLogTextBrowser->setText("");
359+
360+
// Mark init as done
361+
errorLogInitPosDone = true;
362+
}
363+
364+
void RPCConsole::errorLogRefresh()
365+
{
366+
// Check if we are still refreshing
367+
if (errorLogRefreshing)
368+
return;
369+
370+
// Set to refreshing
371+
errorLogRefreshing = true;
372+
373+
// Check if we have initialized debug log already
374+
if (!errorLogInitPosDone)
375+
errorLogInitPos();
376+
377+
// Load the stream
378+
QTextStream in(errorLogFile);
379+
380+
// Load up the lines
381+
QString logLines = "";
382+
while (!in.atEnd()) {
383+
// Load the next line
384+
logLines += in.readLine() + "\n";
385+
}
386+
387+
// Check if we have lines
388+
if (logLines != "") {
389+
// Add the new lines, purpose of duplicate moveCursor calls is
390+
// to auto scroll to the end of the log instead of sticking to the
391+
// top of the text area
392+
ui->errorLogTextBrowser->moveCursor(QTextCursor::End);
393+
ui->errorLogTextBrowser->textCursor().insertText(logLines);
394+
ui->errorLogTextBrowser->moveCursor(QTextCursor::End);
395+
}
396+
397+
// Count the lines in the UI textarea
398+
int uiLineCount = ui->errorLogTextBrowser->document()->lineCount();
399+
400+
// Check if lines are more than ERROR_LOG_INITIAL_COUNT
401+
if (uiLineCount > ERROR_LOG_INITIAL_COUNT) {
402+
// Count how many to remove
403+
int lineCountDiff = uiLineCount - ERROR_LOG_INITIAL_COUNT;
404+
405+
// Get our cursor
406+
QTextCursor cursor = ui->errorLogTextBrowser->textCursor();
407+
408+
// REMOVE THEM
409+
for (int i = 0; i < lineCountDiff; i++) {
410+
cursor.movePosition(QTextCursor::Start);
411+
cursor.select(QTextCursor::LineUnderCursor);
412+
cursor.deleteChar(); // Remove the selected text
413+
cursor.deleteChar(); // This is by design, this removes the \n
414+
}
415+
416+
// Replace the cursor
417+
ui->errorLogTextBrowser->setTextCursor(cursor);
418+
419+
// Move cursor back to the end
420+
ui->errorLogTextBrowser->moveCursor(QTextCursor::End);
421+
}
422+
423+
// Mark as done
424+
errorLogRefreshing = false;
425+
}
426+
307427
bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
308428
{
309429
if(event->type() == QEvent::KeyPress) // Special key handling
@@ -672,6 +792,11 @@ void RPCConsole::on_tabWidget_currentChanged(int index)
672792
clearSelectedNode();
673793
}
674794

795+
void RPCConsole::on_errorLogCopyClipboardButton_clicked()
796+
{
797+
GUIUtil::setClipboard(ui->errorLogTextBrowser->toPlainText());
798+
}
799+
675800
void RPCConsole::on_openDebugLogfileButton_clicked()
676801
{
677802
GUIUtil::openDebugLogfile();
@@ -837,6 +962,15 @@ void RPCConsole::showEvent(QShowEvent *event)
837962
{
838963
QWidget::showEvent(event);
839964

965+
// Mark init as not done
966+
errorLogInitPosDone = false;
967+
968+
// Load error log initial data
969+
errorLogRefresh();
970+
971+
// Start the error log timer
972+
errorLogTimer->start();
973+
840974
if (!clientModel || !clientModel->getPeerTableModel())
841975
return;
842976

@@ -848,6 +982,9 @@ void RPCConsole::hideEvent(QHideEvent *event)
848982
{
849983
QWidget::hideEvent(event);
850984

985+
// Stop the error log timer
986+
errorLogTimer->stop();
987+
851988
if (!clientModel || !clientModel->getPeerTableModel())
852989
return;
853990

src/qt/rpcconsole.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
#include "net.h"
1212

13-
#include <QWidget>
1413
#include <QCompleter>
14+
#include <QFile>
15+
#include <QString>
16+
#include <QTimer>
17+
#include <QWidget>
1518

1619
class ClientModel;
1720
class PlatformStyle;
@@ -61,6 +64,8 @@ private Q_SLOTS:
6164
void on_tabWidget_currentChanged(int index);
6265
/** open the debug.log from the current datadir */
6366
void on_openDebugLogfileButton_clicked();
67+
/** copy the error log text from errorLogTextBrowser */
68+
void on_errorLogCopyClipboardButton_clicked();
6469
/** change the time range of the network traffic graph */
6570
void on_sldGraphRange_valueChanged(int value);
6671
/** update traffic statistics */
@@ -106,6 +111,10 @@ public Q_SLOTS:
106111
void unbanSelectedNode();
107112
/** set which tab has the focus (is visible) */
108113
void setTabFocus(enum TabTypes tabType);
114+
/** load the error log initial file pos */
115+
void errorLogInitPos();
116+
/** update the error log */
117+
void errorLogRefresh();
109118

110119
Q_SIGNALS:
111120
// For RPC command executor
@@ -140,6 +149,10 @@ public Q_SLOTS:
140149
QMenu *banTableContextMenu;
141150
int consoleFontSize;
142151
QCompleter *autoCompleter;
152+
QTimer *errorLogTimer;
153+
QFile *errorLogFile;
154+
bool errorLogInitPosDone = false;
155+
bool errorLogRefreshing = false;
143156
};
144157

145158
#endif // NAVCOIN_QT_RPCCONSOLE_H

0 commit comments

Comments
 (0)