QGIS API Documentation 4.1.0-Master (60fea48833c)
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
18#include <iostream>
19#include <stdio.h>
20
21#include "qgsapplication.h"
22#include "qgslogger.h"
23
24#include <QDateTime>
25#include <QMetaType>
26#include <QString>
27#include <QTextStream>
28
29#include "moc_qgsmessagelog.cpp"
30
31using namespace Qt::StringLiterals;
32
34
35void QgsMessageLog::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser, const char *file, const char *function, int line, Qgis::StringFormat format )
36{
37#ifndef QGISDEBUG
38 Q_UNUSED( file )
39 Q_UNUSED( function )
40 Q_UNUSED( line )
41#endif
42 switch ( level )
43 {
47 QgsDebugMsgLevelLoc( u"%1 %2[%3] %4"_s.arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( static_cast< int >( level ) ).arg( message ), 1, file, function, line );
48 break;
49
52 QgsDebugErrorLoc( u"%1 %2[%3] %4"_s.arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( static_cast< int >( level ) ).arg( message ), file, function, line );
53 break;
54 }
55
56 QgsApplication::messageLog()->emitMessage( message, tag, level, notifyUser, format );
57}
58
59void QgsMessageLog::emitMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, bool notifyUser, Qgis::StringFormat format )
60{
61 emit messageReceivedWithFormat( message, tag, level, format );
62 if ( level != Qgis::MessageLevel::Info && notifyUser && mAdviseBlockCount == 0 )
63 {
64 emit messageReceived( true );
65 }
66}
67
69 : QObject( QgsApplication::messageLog() )
70{
71 connect(
73 static_cast< void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel, Qgis::StringFormat ) >( &QgsMessageLog::messageReceivedWithFormat ),
74 this,
75 static_cast< void ( QgsMessageLogConsole::* )( const QString &, const QString &, Qgis::MessageLevel, Qgis::StringFormat ) >( &QgsMessageLogConsole::logMessage )
76 );
77}
78
79void QgsMessageLogConsole::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
80{
81 const QString formattedMessage = formatLogMessage( message, tag, level );
82 QTextStream cerr( stderr );
83 cerr << formattedMessage;
84}
85
86void QgsMessageLogConsole::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, Qgis::StringFormat format )
87{
88 Q_UNUSED( format );
89 logMessage( message, tag, level );
90}
91
92QString QgsMessageLogConsole::formatLogMessage( const QString &message, const QString &tag, Qgis::MessageLevel level ) const
93{
94 const QString time = QTime::currentTime().toString();
95 const QString levelStr = level == Qgis::MessageLevel::Info ? u"INFO"_s : level == Qgis::MessageLevel::Warning ? u"WARNING"_s : u"CRITICAL"_s;
96 const QString pid = QString::number( QCoreApplication::applicationPid() );
97 return u"%1 %2 %3[%4]: %5\n"_s.arg( time, levelStr, tag, pid, message );
98}
99
100//
101// QgsMessageLogNotifyBlocker
102//
103
108
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:160
@ NoLevel
No level.
Definition qgis.h:165
@ Warning
Warning message.
Definition qgis.h:162
@ Critical
Critical/error message.
Definition qgis.h:163
@ Info
Information message.
Definition qgis.h:161
@ Success
Used for reporting a successful operation.
Definition qgis.h:164
StringFormat
Format of log message.
Definition qgis.h:175
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 messageReceivedWithFormat(const QString &message, const QString &tag, Qgis::MessageLevel level, Qgis::StringFormat)
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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Q_DECL_DEPRECATED void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
#define QgsDebugErrorLoc(str, file, func, line)
Definition qgslogger.h:67
#define QgsDebugMsgLevelLoc(str, level, file, func, line)
Definition qgslogger.h:71