Skip to content
Permalink
Browse files
QgsMessageLogViewer: limit number of messages to avoid blowing RAM (f…
…ixes #44202)
  • Loading branch information
rouault authored and nyalldawson committed Aug 17, 2021
1 parent be8d76e commit 9f97ff32e5f69467f2592b76a5112cb5ab662d79
Showing with 10 additions and 0 deletions.
  1. +9 −0 src/gui/qgsmessagelogviewer.cpp
  2. +1 −0 src/gui/qgsmessagelogviewer.h
@@ -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" );
@@ -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() );
@@ -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.