Skip to content
Permalink
Browse files

use signals for logging

  • Loading branch information
jef-n committed Jan 18, 2012
1 parent f09a6b7 commit a3452a5b9abad94a08e19d73892053f13984a4a5
9 python/core/qgsmessagelog.sip 100644 → 100755
@@ -3,8 +3,15 @@ class QgsMessageLog
%TypeHeaderCode
#include <qgsmessagelog.h>
%End

public:
static QgsMessageLog *instance();

//! add a message to the instance (and create it if necessary)
static void logMessage( QString message, QString tag = QString::null, int level = 0 );

signals:
void messageReceived( QString message, QString tag, int level );

private:
QgsMessageLog();
};
@@ -240,6 +240,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsmaplayerregistry.h
qgsmaprenderer.h
qgsmessageoutput.h
qgsmessagelog.h
qgscredentials.h
qgspluginlayer.h
qgsproject.h
34 src/core/qgsmessagelog.cpp 100644 → 100755
@@ -20,24 +20,42 @@

class QgsMessageLogConsole;

void ( *QgsMessageLog::gmLogger )( QString message, QString tag, int level ) = 0;
QgsMessageLog *QgsMessageLog::sInstance = 0;

void QgsMessageLog::setLogger( void ( *f )( QString message, QString tag, int level ) )
QgsMessageLog::QgsMessageLog()
: QObject()
{
gmLogger = f;
sInstance = this;
}

QgsMessageLog *QgsMessageLog::instance()
{
if ( !sInstance )
sInstance = new QgsMessageLog();

return sInstance;
}

void QgsMessageLog::logMessage( QString message, QString tag, int level )
{
QgsDebugMsg( QString( "%1 %2[%3] %4" ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ) ).arg( tag ).arg( level ).arg( message ) );

if ( !gmLogger )
QgsMessageLogConsole::logger( message, tag, level );
else
gmLogger( message, tag, level );
QgsMessageLog::instance()->emitMessage( message, tag, level );
}

void QgsMessageLog::emitMessage( QString message, QString tag, int level )
{
emit messageReceived( message, tag, level );
}

QgsMessageLogConsole::QgsMessageLogConsole()
: QObject( QgsMessageLog::instance() )
{
connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, int ) ),
this, SLOT( logMessage( QString, QString, tag ) ) );
}

void QgsMessageLogConsole::logger( QString message, QString tag, int level )
void QgsMessageLogConsole::logMessage( QString message, QString tag, int level )
{
std::cout << tag.toLocal8Bit().data() << "[" << level << "]: " << message.toLocal8Bit().data() << std::endl;
}
26 src/core/qgsmessagelog.h 100644 → 100755
@@ -30,18 +30,25 @@
* \note added in 1.9
*/
class CORE_EXPORT QgsMessageLog
class CORE_EXPORT QgsMessageLog : public QObject
{
Q_OBJECT;

public:
static QgsMessageLog *instance();

//! add a message to the instance (and create it if necessary)
static void logMessage( QString message, QString tag = QString::null, int level = 0 );

//! set log message
static void setLogger( void ( *logger )( QString message, QString tag, int level ) );
signals:
void messageReceived( QString message, QString tag, int level );

private:
//! function
static void ( *gmLogger )( QString message, QString tag, int level );
QgsMessageLog();

void emitMessage( QString message, QString tag, int level );

static QgsMessageLog *sInstance;
};


@@ -51,10 +58,15 @@ class CORE_EXPORT QgsMessageLog
This class outputs log messages to the standard output. Therefore it might
be the right choice for apps without GUI.
*/
class CORE_EXPORT QgsMessageLogConsole
class CORE_EXPORT QgsMessageLogConsole : public QObject
{
Q_OBJECT;

public:
static void logger( QString message, QString tag = QString::null, int level = 0 );
QgsMessageLogConsole();

public slots:
void logMessage( QString message, QString tag, int level );
};

#endif
16 src/gui/qgsmessagelogviewer.cpp 100644 → 100755
@@ -27,8 +27,6 @@
#include <QToolTip>
#include <QDockWidget>

static QgsMessageLogViewer *gmInstance = 0;

static QIcon icon( QString icon )
{
// try active theme
@@ -47,8 +45,9 @@ QgsMessageLogViewer::QgsMessageLogViewer( QStatusBar *statusBar, QWidget *parent
, mCount( 0 )
{
setupUi( this );
gmInstance = this;
QgsMessageLog::setLogger( logger );

connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, int ) ),
this, SLOT( logMessage( QString, QString, int ) ) );

if ( statusBar )
{
@@ -68,7 +67,6 @@ QgsMessageLogViewer::QgsMessageLogViewer( QStatusBar *statusBar, QWidget *parent

QgsMessageLogViewer::~QgsMessageLogViewer()
{
QgsMessageLog::setLogger( 0 );
}

void QgsMessageLogViewer::hideEvent( QHideEvent * )
@@ -100,14 +98,6 @@ void QgsMessageLogViewer::buttonToggled( bool checked )
w->hide();
}

void QgsMessageLogViewer::logger( QString message, QString tag, int level )
{
if ( !gmInstance )
return;

gmInstance->logMessage( message, tag, level );
}

void QgsMessageLogViewer::logMessage( QString message, QString tag, int level )
{
mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) );
@@ -32,16 +32,15 @@ class QHideEvent;
* A generic message for displaying QGIS log messages.
* \note added in 1.8
*/
class GUI_EXPORT QgsMessageLogViewer: public QDialog, public QgsMessageLog, private Ui::QgsMessageLogViewer
class GUI_EXPORT QgsMessageLogViewer: public QDialog, private Ui::QgsMessageLogViewer
{
Q_OBJECT
public:
QgsMessageLogViewer( QStatusBar *statusBar = 0, QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
~QgsMessageLogViewer();

void logMessage( QString message, QString tag = QString::null, int level = 0 );

static void logger( QString message, QString tag, int level );
public slots:
void logMessage( QString message, QString tag, int level );

private:
void showEvent( QShowEvent * );

0 comments on commit a3452a5

Please sign in to comment.
You can’t perform that action at this time.