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 Jul 21, 2021
1 parent b5d6610 commit e2e7d52
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 @@ -109,6 +109,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 @@ -164,6 +170,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 e2e7d52

Please sign in to comment.