QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
25 
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( QString message, QString tag, QgsMessageLog::MessageLevel level )
44 {
45  QgsDebugMsg( QString( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ) ).arg( tag ).arg( level ).arg( message ) );
46 
47  QgsMessageLog::instance()->emitMessage( message, tag, level );
48 }
49 
50 void QgsMessageLog::emitMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
51 {
52  emit messageReceived( message, tag, level );
53 }
54 
56  : QObject( QgsMessageLog::instance() )
57 {
58  connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ),
59  this, SLOT( logMessage( QString, QString, QgsMessageLog::MessageLevel ) ) );
60 }
61 
62 void QgsMessageLogConsole::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
63 {
64  std::cout
65  << tag.toLocal8Bit().data() << "[" <<
66  ( level == QgsMessageLog::INFO ? "INFO"
67  : level == QgsMessageLog::WARNING ? "WARNING"
68  : "CRITICAL" )
69  << "]: " << message.toLocal8Bit().data() << std::endl;
70 }
71 
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
static QgsMessageLog * sInstance
Definition: qgsmessagelog.h:58
static void logMessage(QString message, QString tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
void emitMessage(QString message, QString tag, QgsMessageLog::MessageLevel level)
void logMessage(QString message, QString tag, QgsMessageLog::MessageLevel level)
Default implementation of message logging interface.
Definition: qgsmessagelog.h:68
void messageReceived(QString message, QString tag, QgsMessageLog::MessageLevel level)
static QgsMessageLog * instance()
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:33