QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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( 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::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  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::Info ? QStringLiteral( "INFO" ) :
61  level == Qgis::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 }
Qgis::Warning
@ Warning
Definition: qgis.h:91
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsMessageLogConsole::formatLogMessage
QString formatLogMessage(const QString &message, const QString &tag, Qgis::MessageLevel level=Qgis::Info) const
Formats a log message.
Definition: qgsmessagelog.cpp:57
QgsMessageLog::messageReceived
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
QgsMessageLogConsole::QgsMessageLogConsole
QgsMessageLogConsole()
Constructor for QgsMessageLogConsole.
Definition: qgsmessagelog.cpp:43
qgsapplication.h
QgsApplication::messageLog
static QgsMessageLog * messageLog()
Returns the application's message log.
Definition: qgsapplication.cpp:2248
Qgis::Info
@ Info
Definition: qgis.h:90
QgsMessageLogConsole::logMessage
virtual void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to stderr.
Definition: qgsmessagelog.cpp:50
QgsMessageLog
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:39
QgsApplication
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
Definition: qgsapplication.h:83
QgsMessageLogNotifyBlocker::~QgsMessageLogNotifyBlocker
~QgsMessageLogNotifyBlocker()
Definition: qgsmessagelog.cpp:76
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:89
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
qgslogger.h
QgsMessageLogNotifyBlocker::QgsMessageLogNotifyBlocker
QgsMessageLogNotifyBlocker()
Constructor for QgsMessageLogNotifyBlocker.
Definition: qgsmessagelog.cpp:71
QgsMessageLogConsole
Default implementation of message logging interface.
Definition: qgsmessagelog.h:137
qgsmessagelog.h