添加头文件:
#include <QDateTime>
#include <QFile>
#include <QTextStream>
定义输出格式:
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
//去除qt不必要的log信息
if (msg.contains("Unknown property min-weidth") || msg.contains("content-type missing in HTTP POST")
|| msg.contains("the source rect is not contained by the pixmap's rectangle") )
{
return;
}
static QMutex mutex;
mutex.lock();
QString text;
switch (type)
{
case QtDebugMsg:
text = QString("Debug:");
break;
case QtWarningMsg:
text = QString("Warning:");
break;
case QtCriticalMsg:
text = QString("Critical:");
break;
case QtFatalMsg:
text = QString("Fatal:");
}
QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString current_date = QString("(%1)").arg(current_date_time);
QString message = QString("%1 %2 %3 %4").arg(current_date).arg(text).arg(context_info).arg(msg);
QString logFile = QCoreApplication::applicationDirPath() + "/log.txt";
QFile file(logFile);
file.open(QIODevice::WriteOnly | QIODevice::Append);
if (file.size() >= 10 * 1024 * 1024) //文件达到10M后先备份再清空
{
QFile::remove("log_00.txt");//删除原来的备份文件
file.copy("log_00.txt"); //拷贝文件至备份文件
file.resize(0); //清空文件内容
}
QTextStream text_stream(&file);
text_stream << message << "\r\n";
file.flush();
file.close();
mutex.unlock();
}
注册MessageHandler:
int main()
{
//注册MessageHandler
qInstallMessageHandler(outputMessage);
}
打印日志会不会影响程序运行的流畅性??会不会带来卡的感觉??
debug release 都会有打印信息,ms级打印,时效性是够用的(想一些QT本身开发时,打印调试信息的实效就好理解了)
没法添加新的QtMsgType,灵活性不够
#include <QDateTime>
#include <QFile>
#include <QTextStream>
定义输出格式:
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
//去除qt不必要的log信息
if (msg.contains("Unknown property min-weidth") || msg.contains("content-type missing in HTTP POST")
|| msg.contains("the source rect is not contained by the pixmap's rectangle") )
{
return;
}
static QMutex mutex;
mutex.lock();
QString text;
switch (type)
{
case QtDebugMsg:
text = QString("Debug:");
break;
case QtWarningMsg:
text = QString("Warning:");
break;
case QtCriticalMsg:
text = QString("Critical:");
break;
case QtFatalMsg:
text = QString("Fatal:");
}
QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString current_date = QString("(%1)").arg(current_date_time);
QString message = QString("%1 %2 %3 %4").arg(current_date).arg(text).arg(context_info).arg(msg);
QString logFile = QCoreApplication::applicationDirPath() + "/log.txt";
QFile file(logFile);
file.open(QIODevice::WriteOnly | QIODevice::Append);
if (file.size() >= 10 * 1024 * 1024) //文件达到10M后先备份再清空
{
QFile::remove("log_00.txt");//删除原来的备份文件
file.copy("log_00.txt"); //拷贝文件至备份文件
file.resize(0); //清空文件内容
}
QTextStream text_stream(&file);
text_stream << message << "\r\n";
file.flush();
file.close();
mutex.unlock();
}
注册MessageHandler:
int main()
{
//注册MessageHandler
qInstallMessageHandler(outputMessage);
}
打印日志会不会影响程序运行的流畅性??会不会带来卡的感觉??
debug release 都会有打印信息,ms级打印,时效性是够用的(想一些QT本身开发时,打印调试信息的实效就好理解了)
弊端:
这种方式,可以理解为重写了QT原本的输出格式。
没法添加新的QtMsgType,灵活性不够