
Untitled
By: a guest on
May 14th, 2012 | syntax:
None | size: 1.55 KB | hits: 19 | expires: Never
Qt: singleton logging class get different instance accross plugins and thread
#pragma once
#include <QtGui>
#include <QMutex>
//! Creates a fake call, to have a cleaner design.
extern void qInfo(const char* fmt);
#define QtTraceMsg QtMsgType(6)
#define QtInfoMsg QtMsgType(7)
class MsgHandler: public QObject
{
Q_OBJECT
signals:
void newMsg(QtMsgType type, const QString &msg);
public:
static MsgHandler *instance();
static void Handler(QtMsgType type, const char *msg);
private:
MsgHandler() { qRegisterMetaType<QtMsgType>("QtMsgType"); } // important
static MsgHandler* _instance;
};
#include "MsgHandler.h"
MsgHandler* MsgHandler::_instance = NULL;
MsgHandler * MsgHandler::instance()
{
static QMutex mutex;
if (_instance == NULL)
{
mutex.lock();
if (_instance == NULL)
_instance = new MsgHandler;
mutex.unlock();
}
return _instance;
}
void MsgHandler::Handler(QtMsgType type, const char *msg)
{
QString s = msg;
emit instance()->newMsg(type, s);
}
///////////////////////////////////////////////////////////////////////////////
void qInfo(const char *msg)
{
MsgHandler::instance()->Handler(QtInfoMsg, msg);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Handle error messages
qInstallMsgHandler(MsgHandler::Handler);
}
void MyPlugin::run()
{
qWarning("Test debug"); //works fine
MsgHandler::instance()->Handler(QtInfoMsg, "info 2"); //creates a new instance!
qInfo("test info"); //also creates a new instance!
}