Skip to content

Commit

Permalink
QgsMessageLogViewer: limit number of messages to avoid blowing RAM (f…
Browse files Browse the repository at this point in the history
…ixes #44202)
  • Loading branch information
rouault authored and nyalldawson committed Aug 17, 2021
1 parent be8d76e commit 9f97ff3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/gui/qgsmessagelogviewer.cpp
Expand Up @@ -96,6 +96,12 @@ void QgsMessageLogViewer::reject()

void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
{
constexpr int MESSAGE_COUNT_LIMIT = 10000;
// Avoid logging too many messages, which might blow memory.
if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
return;
++mMessageLoggedCount;

QString cleanedTag = tag;
if ( cleanedTag.isNull() )
cleanedTag = tr( "General" );
Expand Down Expand Up @@ -151,6 +157,9 @@ void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag
QString prefix = QStringLiteral( "<font color=\"%1\">%2 &nbsp;&nbsp;&nbsp; %3 &nbsp;&nbsp;&nbsp;</font>" )
.arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
QString cleanedMessage = message;
if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
cleanedMessage = tr( "Message log truncated" );

cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
w->appendHtml( cleanedMessage );
w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsmessagelogviewer.h
Expand Up @@ -64,6 +64,7 @@ class GUI_EXPORT QgsMessageLogViewer: public QDialog, private Ui::QgsMessageLogV

QString mClickedAnchor;
QMenu *mTabBarContextMenu = nullptr;
int mMessageLoggedCount = 0;
};

#endif

0 comments on commit 9f97ff3

Please sign in to comment.