Qt中的自定义和默认消息处理程序 [英] Custom and Default Message Handler in Qt

查看:117
本文介绍了Qt中的自定义和默认消息处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用默认的Qt消息处理程序,而将消息记录到日志文件,除了qDebug。
在这里我的解决方案,你有任何建议或这个实现的可能问题是什么?

I want to use default Qt message handler while logging messages to a log file except qDebug. Here my solution, do you have any suggestion or what are the possible problems with this implementation?

标题:

#ifndef QLOGGER_H
#define QLOGGER_H

#include <QtCore/QObject>
#include <QtCore/QFile>
#include <QtCore/QDateTime>

class QLogger : public QObject
{
Q_OBJECT

public:
    QLogger();
   ~QLogger();

    static void start();
    static void finish();
    static QString filename;

private:
    static void myloggerfunction(QtMsgType type, const char *msg);

};

#endif // QLOGGER_H

资料来源:

#include <QTextStream>
#include <QDateTime>
#include <QDir>
#include <iostream>

#include "qlogger.h"

using namespace std;

QString QLogger::filename=QString("log.txt");

QLogger::QLogger()
{
}

QLogger::~QLogger()
{
}

void QLogger::start()
{
    qInstallMsgHandler(myloggerfunction);
}

void QLogger::finish()
{
    qInstallMsgHandler(0);
}

void QLogger::myloggerfunction(QtMsgType type, const char *msg)
{
    QDir dir;
    dir.cd("LOG");

    QFile logFile(filename);

    if (logFile.open(QIODevice::Append | QIODevice::Text))
    {
        QTextStream streamer(&logFile);

        switch (type){
                 case QtDebugMsg:
                     finish();
                     qDebug(msg);
                     break;
                 case QtWarningMsg:
                     streamer << QDateTime::currentDateTime().toString() <<" Warning: " << msg <<  "\n";
                     finish();
                     qWarning(msg);
                     break;
                 case QtCriticalMsg:
                     streamer << QDateTime::currentDateTime().toString() <<" Critical: " << msg <<  "\n";
                     finish();
                     qCritical(msg);
                     break;
                 case QtFatalMsg:
                     streamer << QDateTime::currentDateTime().toString() <<" Fatal: " << msg <<  "\n";
                     finish();
                     qFatal(msg);
                     break;
        }
        logFile.close();
    }
    start();
}


推荐答案

Afaik工作,你正在实现消息处理函数作为成员函数给一个对象 qInstallMessageHandler 需要的函数的签名 void myMsgHandler(QtMsgType, const char *);

Afaik this is not going to work, you are implementing the message handler function as a member function to a an object the signature of the function that qInstallMessageHandler takes is void myMsgHandler(QtMsgType, const char *);

你可以将函数实现为一个简单的独立函数,或者使用一个带有静态访问器的单例,例如

You can either implement the function as a plain freestanding function or use a singleton with a static accessor e.g.

void msgHandler(QtMsgType type, const char * msg)
{
   Logger::instance()->handleMessage(type,msg);
}

class Logger
{
  static Logger* instance() {... }
  void handleMessage(QtMsgType type, const char* msg) { ... }
}

这给你一个函数,用于 qInstallMsgHandler 和用于包装日志的对象

this gives you a function to use for qInstallMsgHandler and an object to wrap the logging

这篇关于Qt中的自定义和默认消息处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆