QGIS API Documentation 4.3.0-Master (ca159e4aeaf)
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{
62 emit messageReceived( message, tag, level );
64 emit messageReceivedWithFormat( message, tag, level, format );
65 if ( level != Qgis::MessageLevel::Info && notifyUser && mAdviseBlockCount == 0 )
66 {
67 emit messageReceived( true );
68 }
69}
70
72 : QObject( QgsApplication::messageLog() )
73{
74 connect(
76 static_cast< void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel, Qgis::StringFormat ) >( &QgsMessageLog::messageReceivedWithFormat ),
77 this,
78 static_cast< void ( QgsMessageLogConsole::* )( const QString &, const QString &, Qgis::MessageLevel, Qgis::StringFormat ) >( &QgsMessageLogConsole::logMessage )
79 );
80}
81
82void QgsMessageLogConsole::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
83{
84 const QString formattedMessage = formatLogMessage( message, tag, level );
85 QTextStream cerr( stderr );
86 cerr << formattedMessage;
87}
88
89void QgsMessageLogConsole::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level, Qgis::StringFormat format )
90{
91 Q_UNUSED( format );
92 logMessage( message, tag, level );
93}
94
95QString QgsMessageLogConsole::formatLogMessage( const QString &message, const QString &tag, Qgis::MessageLevel level ) const
96{
97 const QString time = QTime::currentTime().toString();
98 const QString levelStr = level == Qgis::MessageLevel::Info ? u"INFO"_s : level == Qgis::MessageLevel::Warning ? u"WARNING"_s : u"CRITICAL"_s;
99 const QString pid = QString::number( QCoreApplication::applicationPid() );
100 return u"%1 %2 %3[%4]: %5\n"_s.arg( time, levelStr, tag, pid, message );
101}
102
103//
104// QgsMessageLogNotifyBlocker
105//
106
111
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 Q_NOWARN_DEPRECATED_POP
Definition qgis.h:8361
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:8360
#define QgsDebugErrorLoc(str, file, func, line)
Definition qgslogger.h:89
#define QgsDebugMsgLevelLoc(str, level, file, func, line)
Definition qgslogger.h:98