Chinaunix首页 | 论坛 | 博客
  • 博客访问: 307271
  • 博文数量: 63
  • 博客积分: 1482
  • 博客等级: 上尉
  • 技术积分: 1185
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-12 19:06
个人简介

hello world!

文章分类

全部博文(63)

分类: C/C++

2013-09-08 00:57:50

Qt 简便日志系统

 

在编写程序是如何将qDebug()写的调试语句信息,从Qt输出窗口重定义到一个txt仍本中呢,这就需要通过使用qInstallMsgHandler。从Qt的帮助手册中可看到相关信息。如果您需要更强大的日志系统可以使用第三方开源库Log4Qt

 

 qInstallMsgHandler( handler)



Installs a Qt message handler which has been defined previously. This method is deprecated, use qInstallMessageHandler instead.

See also  and ().

 

 qInstallMessageHandler( handler)


Installs a Qt message handler which has been defined previously. Returns a pointer to the previous message handler (which may be 0).

 

The message handler is a function that prints out debug messages, warnings, critical and fatal error messages. The Qt library (debug mode) contains hundreds of warning messages that are printed when internal errors (usually invalid function arguments) occur. Qt built in release mode also contains such warnings unless QT_NO_WARNING_OUTPUT and/or QT_NO_DEBUG_OUTPUT have been set during compilation. If you implement your own message handler, you get total control of these messages.

 

The default message handler prints the message to the standard output under X11 or to the debugger under Windows. If it is a fatal message, the application aborts immediately.

 

Only one message handler can be defined, since this is usually done on an application-wide basis to control debug output.

 

To restore the message handler, call qInstallMessageHandler(0).

    简而言之,就是用此函数可将qDebug()是的输入信息重定向到自定义的函数,在函数中你可以将调试信息输出到文本。

 

#include

#include

#include

 

#define LOGFILEMAX 10000

 

/**

* @brief  custom message handler

* @param type [QtMsgType]

* @param msg [const char *]

* @return void

*/



void logMsgHandler(QtMsgType type, const char *msg)

{

    QString strLog;

    switch (type) {

    case QtDebugMsg:

        strLog = QString(QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss ")+"[Debug] %1").arg(QObject::tr(msg));

        break;

    case QtWarningMsg:

        strLog = QString(QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss ")+"[Warn] %1").arg(QObject::tr(msg));

    break;

    case QtCriticalMsg:

        strLog = QString(QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss ")+"[Critical] %1").arg(QObject::tr(msg));

    break;

    case QtFatalMsg:

        strLog = QString(QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss ")+"[Fatal] %1").arg(QObject::tr(msg));

        abort();

    }

 

    QFile outFile("./logs/debug.log");

    outFile.open(QIODevice::WriteOnly | QIODevice::Append);

 

        /**< the max size of log.txt.*/

    if(outFile.size()/1000>LOGFILEMAX)

    {

        outFile.close();

        outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Truncate);

        outFile.close();

        outFile.open(QIODevice::WriteOnly | QIODevice::Append);

    }

 

    QTextStream ts(&outFile);

    ts << strLog << endl;

}

 

int main(int argc, char *argv[])

{

    QApplication a(argc, argv);

    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());

 

    /**< install qDebug function handler.*/

    qInstallMsgHandler(logMsgHandler);

    MainWindow w;

    w.show(); 

    return a.exec();

}



 


阅读(7804) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~