QGIS API Documentation  3.2.0-Bonn (bc43194)
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 <iostream>
22 
24 
25 void QgsMessageLog::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser )
26 {
27  QgsDebugMsg( QStringLiteral( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( level ).arg( message ) );
28 
29  QgsApplication::messageLog()->emitMessage( message, tag, level, notifyUser );
30 }
31 
32 void QgsMessageLog::emitMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser )
33 {
34  emit messageReceived( message, tag, level );
35  if ( level != Qgis::Info && notifyUser && mAdviseBlockCount == 0 )
36  {
37  emit messageReceived( true );
38  }
39 }
40 
42  : QObject( QgsApplication::messageLog() )
43 {
44  connect( QgsApplication::messageLog(), static_cast < void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel ) >( &QgsMessageLog::messageReceived ),
46 }
47 
48 void QgsMessageLogConsole::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
49 {
50  std::cout
51  << tag.toLocal8Bit().data() << "[" <<
52  ( level == Qgis::Info ? "INFO"
53  : level == Qgis::Warning ? "WARNING"
54  : "CRITICAL" )
55  << "]: " << message.toLocal8Bit().data() << std::endl;
56 }
57 
58 //
59 // QgsMessageLogNotifyBlocker
60 //
61 
63 {
64  QgsApplication::messageLog()->mAdviseBlockCount++;
65 }
66 
68 {
69  QgsApplication::messageLog()->mAdviseBlockCount--;
70 }
Extends QApplication to provide access to QGIS specific resources such as theme paths, database paths etc.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:78
static QgsMessageLog * messageLog()
Returns the application&#39;s message log.
void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
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).
Default implementation of message logging interface.
QgsMessageLogNotifyBlocker()
Constructor for QgsMessageLogNotifyBlocker.
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:38