QGIS API Documentation 3.37.0-Master (fa0c88a90b0)
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
27void QgsMessageLog::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser )
28{
29 switch ( level )
30 {
31 case Qgis::MessageLevel::Info:
32 case Qgis::MessageLevel::Success:
33 case Qgis::MessageLevel::NoLevel:
34 QgsDebugMsgLevel( QStringLiteral( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( static_cast< int >( level ) ).arg( message ), 1 );
35 break;
36
37 case Qgis::MessageLevel::Warning:
38 case Qgis::MessageLevel::Critical:
39 QgsDebugError( QStringLiteral( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( static_cast< int >( level ) ).arg( message ) );
40 break;
41 }
42
43 QgsApplication::messageLog()->emitMessage( message, tag, level, notifyUser );
44}
45
46void QgsMessageLog::emitMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser )
47{
48 emit messageReceived( message, tag, level );
49 if ( level != Qgis::MessageLevel::Info && notifyUser && mAdviseBlockCount == 0 )
50 {
51 emit messageReceived( true );
52 }
53}
54
56 : QObject( QgsApplication::messageLog() )
57{
58 connect( QgsApplication::messageLog(), static_cast < void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel ) >( &QgsMessageLog::messageReceived ),
60}
61
62void QgsMessageLogConsole::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
63{
64 const QString formattedMessage = formatLogMessage( message, tag, level );
65 QTextStream cerr( stderr );
66 cerr << formattedMessage;
67}
68
69QString QgsMessageLogConsole::formatLogMessage( const QString &message, const QString &tag, Qgis::MessageLevel level ) const
70{
71 const QString time = QTime::currentTime().toString();
72 const QString levelStr = level == Qgis::MessageLevel::Info ? QStringLiteral( "INFO" ) :
73 level == Qgis::MessageLevel::Warning ? QStringLiteral( "WARNING" ) :
74 QStringLiteral( "CRITICAL" );
75 const QString pid = QString::number( QCoreApplication::applicationPid() );
76 return QStringLiteral( "%1 %2 %3[%4]: %5\n" ).arg( time, levelStr, tag, pid, message );
77}
78
79//
80// QgsMessageLogNotifyBlocker
81//
82
84{
85 QgsApplication::messageLog()->mAdviseBlockCount++;
86}
87
89{
90 QgsApplication::messageLog()->mAdviseBlockCount--;
91}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:99
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 QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugError(str)
Definition: qgslogger.h:38