Skip to content

Commit

Permalink
[FEATURE] Add "Save Log to File" action for network logger
Browse files Browse the repository at this point in the history
Comes with a big warning to users that the log is sensitive and
should be treated as confidential
  • Loading branch information
nyalldawson committed Mar 31, 2020
1 parent 16dc169 commit 48952cd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/app/devtools/networklogger/qgsnetworklogger.cpp
Expand Up @@ -206,6 +206,11 @@ void QgsNetworkLogger::removeRows( const QList<int> &rows )
}
}

QgsNetworkLoggerRootNode *QgsNetworkLogger::rootGroup()
{
return mRootNode.get();
}

int QgsNetworkLogger::rowCount( const QModelIndex &parent ) const
{
QgsNetworkLoggerNode *n = index2node( parent );
Expand Down
5 changes: 5 additions & 0 deletions src/app/devtools/networklogger/qgsnetworklogger.h
Expand Up @@ -81,6 +81,11 @@ class QgsNetworkLogger : public QAbstractItemModel
*/
void removeRows( const QList< int > &rows );

/**
* Returns the root node of the log.
*/
QgsNetworkLoggerRootNode *rootGroup();

static constexpr int MAX_LOGGED_REQUESTS = 1000;

public slots:
Expand Down
28 changes: 28 additions & 0 deletions src/app/devtools/networklogger/qgsnetworkloggerpanelwidget.cpp
Expand Up @@ -18,9 +18,13 @@
#include "qgsnetworklogger.h"
#include "qgssettings.h"
#include "qgsnetworkloggernode.h"
#include "qgsjsonutils.h"
#include <QFontDatabase>
#include <QMenu>
#include <QScrollBar>
#include <QFileDialog>
#include <QMessageBox>
#include <nlohmann/json.hpp>

//
// QgsNetworkLoggerTreeView
Expand Down Expand Up @@ -172,4 +176,28 @@ QgsNetworkLoggerPanelWidget::QgsNetworkLoggerPanelWidget( QgsNetworkLogger *logg
QgsSettings().setValue( QStringLiteral( "logNetworkRequests" ), enabled, QgsSettings::App );
mLogger->enableLogging( enabled );
} );
connect( mActionSaveLog, &QAction::triggered, this, [ = ]()
{
if ( QMessageBox::warning( this, tr( "Save Network Log" ),
tr( "Security warning: network logs may contain sensitive data including usernames or passwords. Treat this log as confidential and be careful who you share it with. Continue?" ), QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
return;

QString saveFilePath = QFileDialog::getSaveFileName( this, tr( "Save Network Log" ), QDir::homePath(), tr( "Log files" ) + " (*.json)" );
if ( saveFilePath.isEmpty() )
{
return;
}

QFile exportFile( saveFilePath );
if ( !exportFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
return;
}
QTextStream fout( &exportFile );

const QVariant value = mLogger->rootGroup()->toVariant();
const QString json = QString::fromStdString( QgsJsonUtils::jsonFromVariant( value ).dump( 2 ) );

fout << json;
} );
}
11 changes: 11 additions & 0 deletions src/ui/qgsnetworkloggerpanelbase.ui
Expand Up @@ -41,6 +41,8 @@
<addaction name="mActionClear"/>
<addaction name="mActionShowSuccessful"/>
<addaction name="mActionShowTimeouts"/>
<addaction name="separator"/>
<addaction name="mActionSaveLog"/>
</widget>
</item>
<item>
Expand Down Expand Up @@ -103,6 +105,15 @@
<string>Show Timeouts</string>
</property>
</action>
<action name="mActionSaveLog">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionFileSave.svg</normaloff>:/images/themes/default/mActionFileSave.svg</iconset>
</property>
<property name="text">
<string>Save Log…</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down

0 comments on commit 48952cd

Please sign in to comment.