QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <qgslogger.h>
18 #include <QDateTime>
19 #include <QMetaType>
20 #include <iostream>
21 
23 
24 QgsMessageLog *QgsMessageLog::sInstance = nullptr;
25 
26 QgsMessageLog::QgsMessageLog()
27  : QObject()
28 {
29  sInstance = this;
30 }
31 
33 {
34  if ( !sInstance )
35  {
36  qRegisterMetaType<QgsMessageLog::MessageLevel>( "QgsMessageLog::MessageLevel" );
37  sInstance = new QgsMessageLog();
38  }
39 
40  return sInstance;
41 }
42 
43 void QgsMessageLog::logMessage( const QString& message, const QString& tag, QgsMessageLog::MessageLevel level )
44 {
45  QgsDebugMsg( QString( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ), tag ).arg( level ).arg( message ) );
46 
47  QgsMessageLog::instance()->emitMessage( message, tag, level );
48 }
49 
50 void QgsMessageLog::emitMessage( const QString& message, const QString& tag, QgsMessageLog::MessageLevel level )
51 {
52  emit messageReceived( message, tag, level );
53  if ( level != QgsMessageLog::INFO )
54  {
55  emit messageReceived( true );
56  }
57 }
58 
60  : QObject( QgsMessageLog::instance() )
61 {
64 }
65 
67 {
68  std::cout
69  << tag.toLocal8Bit().data() << "[" <<
70  ( level == QgsMessageLog::INFO ? "INFO"
71  : level == QgsMessageLog::WARNING ? "WARNING"
72  : "CRITICAL" )
73  << "]: " << message.toLocal8Bit().data() << std::endl;
74 }
75 
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void logMessage(const QString &message, const QString &tag, QgsMessageLog::MessageLevel level)
static void logMessage(const QString &message, const QString &tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
Default implementation of message logging interface.
Definition: qgsmessagelog.h:68
QByteArray toLocal8Bit() const
QDateTime currentDateTime()
static QgsMessageLog * instance()
char * data()
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:31