/****************************Log.h***********************/
{
Q_OBJECT
public:
//class member declaration
static Log& ReturnInstance()
{
static Log Log_obj; //The only Log object that will be used
return Log_obj;
}
void myMessageHandler
(QtMsgType type,QMessageLogContext
&context,
const QString &msg
);
private:
//some member variable declartion
};
/****************************Log.cpp***********************/
//constructor definition
//destructor definition
void Log::myMessageHandler(QtMsgType type, QMessageLogContext
&context,
const QString &msg
) {
switch (type) {
case QtDebugMsg:
ostream<<("Debugging Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtInfoMsg:
ostream<<("Information Message: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function);
break;
case QtWarningMsg:
ostream<<("Warnning Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtFatalMsg:
ostream<<("Fatal Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtCriticalMsg:
ostream<<("Critical Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
default:
break;
}
}
/****************************Log.h***********************/
class Log : public QObject
{
Q_OBJECT
public:
//class member declaration
static Log& ReturnInstance()
{
static Log Log_obj; //The only Log object that will be used
return Log_obj;
}
void myMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg);
private:
//some member variable declartion
QFile m_file;
};
/****************************Log.cpp***********************/
//constructor definition
//destructor definition
void Log::myMessageHandler(QtMsgType type, QMessageLogContext &context, const QString &msg)
{
QTextStream ostream(&m_file);
switch (type) {
case QtDebugMsg:
ostream<<("Debugging Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtInfoMsg:
ostream<<("Information Message: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function);
break;
case QtWarningMsg:
ostream<<("Warnning Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtFatalMsg:
ostream<<("Fatal Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtCriticalMsg:
ostream<<("Critical Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
default:
break;
}
}
To copy to clipboard, switch view to plain text mode
And here is the installation of the message handler function:
#include "mainwindow.h"
#include <QApplication>
#include "logger.h"
int main(int argc, char *argv[])
{
Log::ReturnInstance();
qInstallMessageHandler(Log::ReturnInstance().myMessageHandler); //<------ The error
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
#include "logger.h"
int main(int argc, char *argv[])
{
Log::ReturnInstance();
qInstallMessageHandler(Log::ReturnInstance().myMessageHandler); //<------ The error
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks