QGIS API Documentation  3.21.0-Master (909859188c)
qgsmessagelog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmessagelog.h - interface for logging messages
3  ----------------------
4  begin : October 2011
5  copyright : (C) 2011 by Juergen E. Fischer
6  email : jef at norbit dot de
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsmessagelog.h"
17 #include "qgsapplication.h"
18 #include "qgslogger.h"
19 #include <QDateTime>
20 #include <QMetaType>
21 #include <QTextStream>
22 #include <iostream>
23 #include <stdio.h>
24 
26 
27 void QgsMessageLog::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser )
28 {
29  QgsDebugMsg( QStringLiteral( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( static_cast< int >( level ) ).arg( message ) );
30 
31  QgsApplication::messageLog()->emitMessage( message, tag, level, notifyUser );
32 }
33 
34 void QgsMessageLog::emitMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser )
35 {
36  emit messageReceived( message, tag, level );
37  if ( level != Qgis::MessageLevel::Info && notifyUser && mAdviseBlockCount == 0 )
38  {
39  emit messageReceived( true );
40  }
41 }
42 
44  : QObject( QgsApplication::messageLog() )
45 {
46  connect( QgsApplication::messageLog(), static_cast < void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel ) >( &QgsMessageLog::messageReceived ),
48 }
49 
50 void QgsMessageLogConsole::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
51 {
52  const QString formattedMessage = formatLogMessage( message, tag, level );
53  QTextStream cerr( stderr );
54  cerr << formattedMessage;
55 }
56 
57 QString QgsMessageLogConsole::formatLogMessage( const QString &message, const QString &tag, Qgis::MessageLevel level ) const
58 {
59  const QString time = QTime::currentTime().toString();
60  const QString levelStr = level == Qgis::MessageLevel::Info ? QStringLiteral( "INFO" ) :
61  level == Qgis::MessageLevel::Warning ? QStringLiteral( "WARNING" ) :
62  QStringLiteral( "CRITICAL" );
63  const QString pid = QString::number( QCoreApplication::applicationPid() );
64  return QStringLiteral( "%1 %2 %3[%4]: %5\n" ).arg( time, levelStr, tag, pid, message );
65 }
66 
67 //
68 // QgsMessageLogNotifyBlocker
69 //
70 
72 {
73  QgsApplication::messageLog()->mAdviseBlockCount++;
74 }
75 
77 {
78  QgsApplication::messageLog()->mAdviseBlockCount--;
79 }
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:106
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
static QgsMessageLog * messageLog()
Returns the application's message log.
Default implementation of message logging interface.
QString formatLogMessage(const QString &message, const QString &tag, Qgis::MessageLevel level=Qgis::MessageLevel::Info) const
Formats a log message.
QgsMessageLogConsole()
Constructor for QgsMessageLogConsole.
virtual void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to stderr.
QgsMessageLogNotifyBlocker()
Constructor for QgsMessageLogNotifyBlocker.
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:40
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
#define QgsDebugMsg(str)
Definition: qgslogger.h:38