QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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 "moc_qgsmessagelog.cpp"
18#include "qgsapplication.h"
19#include "qgslogger.h"
20#include <QDateTime>
21#include <QMetaType>
22#include <QTextStream>
23#include <iostream>
24#include <stdio.h>
25
27
28void QgsMessageLog::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser )
29{
30 switch ( level )
31 {
35 QgsDebugMsgLevel( QStringLiteral( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( static_cast< int >( level ) ).arg( message ), 1 );
36 break;
37
40 QgsDebugError( QStringLiteral( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( static_cast< int >( level ) ).arg( message ) );
41 break;
42 }
43
44 QgsApplication::messageLog()->emitMessage( message, tag, level, notifyUser );
45}
46
47void QgsMessageLog::emitMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser )
48{
49 emit messageReceived( message, tag, level );
50 if ( level != Qgis::MessageLevel::Info && notifyUser && mAdviseBlockCount == 0 )
51 {
52 emit messageReceived( true );
53 }
54}
55
57 : QObject( QgsApplication::messageLog() )
58{
59 connect( QgsApplication::messageLog(), static_cast < void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel ) >( &QgsMessageLog::messageReceived ),
61}
62
63void QgsMessageLogConsole::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
64{
65 const QString formattedMessage = formatLogMessage( message, tag, level );
66 QTextStream cerr( stderr );
67 cerr << formattedMessage;
68}
69
70QString QgsMessageLogConsole::formatLogMessage( const QString &message, const QString &tag, Qgis::MessageLevel level ) const
71{
72 const QString time = QTime::currentTime().toString();
73 const QString levelStr = level == Qgis::MessageLevel::Info ? QStringLiteral( "INFO" ) :
74 level == Qgis::MessageLevel::Warning ? QStringLiteral( "WARNING" ) :
75 QStringLiteral( "CRITICAL" );
76 const QString pid = QString::number( QCoreApplication::applicationPid() );
77 return QStringLiteral( "%1 %2 %3[%4]: %5\n" ).arg( time, levelStr, tag, pid, message );
78}
79
80//
81// QgsMessageLogNotifyBlocker
82//
83
88
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ NoLevel
No level.
Definition qgis.h:159
@ Warning
Warning message.
Definition qgis.h:156
@ Critical
Critical/error message.
Definition qgis.h:157
@ Info
Information message.
Definition qgis.h:155
@ Success
Used for reporting a successful operation.
Definition qgis.h:158
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.
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