【Qt】日志系统技巧
本文主要介绍使用Qt自身的日志系统的核心技巧:1、日志系统的构建;2、release版本完整显示日志的技巧;3、循环写日志的方式;
1、日志系统的构建
日志系统的构建,本身需要在main函数中进行注册,使用这个函数:qInstallMessageHandler(MessageOutput)。而要自定义日志输出到文件的显示结构,我们这么做(请看以下代码):
void MessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QString strMsg("");
switch(type)
{
case QtDebugMsg:
strMsg = QString("Debug:");
break;
case QtWarningMsg:
strMsg = QString("Warning:");
break;
case QtCriticalMsg:
strMsg = QString("Critical:");
break;
case QtFatalMsg:
strMsg = QString("Fatal:");
break;
}
// 设置输出信息格式
QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString strMessage = strMsg + QString("File:%1 Line:%2 Function:%3 Message:%4 DateTime:%5")
.arg(context.file).arg(context.line).arg(context.function).arg(localMsg.constData()).arg(strDateTime);
...
...
}
这样就定义了日志的级别与输出结构。
2、循环写日志的方式
为了循环操作日志,我们在上面的函数中继续追加以下内容:
{
...
// 加锁
static QMutex mutex;
mutex.lock();
// 循环写日志
QFile file1("log1.txt");
QFile file2("log2.txt");
file1.open(QIODevice::WriteOnly | QIODevice::Append);
if(file1.size() >= 20000000 )
{
file1.close();
file2.remove();
QFile::copy("log1.txt","log2.txt");
file1.remove();
QFile file3("log1.txt");
file3.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream stream(&file3);
stream << strMessage << endl;
}
else
{
QTextStream stream(&file1);
stream << strMessage << endl;
}
// 解锁
mutex.unlock();
}
这样,就可以判定日志满了,就做备份,然后继续写进去。从而实现循环记录并且有备份的现实要求。
3、release版本完整显示日志的技巧
通常情况下,debug版本的日志是完全输出,没有大问题的,但是当我们切换到release版本时候,会发现出现这样的情况:
Debug:File: Line:0 Function:
即:File、Line、function这些的输出没有显示出来。怎么回事了?我们的debug版本就好好的啊!!!
这是因为Qt默认在release版本关闭调试上下文,我们需要手动来开启。这么做,看这里:在pro文件中追加一句:
DEFINES += QT_MESSAGELOGCONTEXT
然后重新qmake,再构建程序,你会欣喜的发现,一切又完全恢复过来了!
下面贴出来完整的代码吧:
#include <QCoreApplication>
#include <QFile>
#include <QDateTime>
#include <QThread>
#include <QDebug>
void MessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QString strMsg("");
switch(type)
{
case QtDebugMsg:
strMsg = QString("Debug:");
break;
case QtWarningMsg:
strMsg = QString("Warning:");
break;
case QtCriticalMsg:
strMsg = QString("Critical:");
break;
case QtFatalMsg:
strMsg = QString("Fatal:");
break;
}
// 设置输出信息格式
QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString strMessage = strMsg + QString("File:%1 Line:%2 Function:%3 Message:%4 DateTime:%5")
.arg(context.file).arg(context.line).arg(context.function).arg(localMsg.constData()).arg(strDateTime);
// 加锁
static QMutex mutex;
mutex.lock();
// 循环写日志
QFile file1("log1.txt");
QFile file2("log2.txt");
file1.open(QIODevice::WriteOnly | QIODevice::Append);
if(file1.size() >= 20000000 )
{
file1.close();
file2.remove();
QFile::copy("log1.txt","log2.txt");
file1.remove();
QFile file3("log1.txt");
file3.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream stream(&file3);
stream << strMessage << endl;
}
else
{
QTextStream stream(&file1);
stream << strMessage << endl;
}
// 解锁
mutex.unlock();
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(MessageOutput);
QCoreApplication a(argc, argv);
while (true) {
qDebug()<<QDateTime::currentDateTime().toString("yy-MM-dd hh:mm:ss");
QThread::sleep(1);
}
return a.exec();
}