Qt自定义日志输出
简略版:
#include <QApplication>
#include <QDebug>
#include <QDateTime>
#include <QFileInfo>
// 将日志类型转换为字符串
QString typeToString(QtMsgType type) {switch (type) {case QtDebugMsg: return "Debug";case QtInfoMsg: return "Info";case QtWarningMsg: return "Warning";case QtCriticalMsg: return "Critical";case QtFatalMsg: return "Fatal";default: return "Unknown";}
}
void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {// 当前时间,只保留到秒QString timeText = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");// 简化文件名,只显示文件名,不显示路径QString file(context.file ? context.file : "no-file");file = QFileInfo(file).fileName();// 构建简化的日志信息QString formattedMessage = QString("%1 [%2] (%3:%4): %5").arg(timeText).arg(typeToString(type)).arg(file).arg(context.line).arg(msg);// 输出到控制台fprintf(stderr, "%s\n", formattedMessage.toLocal8Bit().constData());
}
int main(int argc, char *argv[]) {QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);qInstallMessageHandler(customMessageHandler);QApplication a(argc, argv);return QApplication::exec();
}
效果图:
详细版:
#include <QApplication>
#include <QPushButton>
#include <QDebug>
#include "gamepanel.h"
#include <QDateTime>
#include <QFileInfo>void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {// 当前时间QString timeText = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");// 构建日志信息QByteArray localMsg = msg.toLocal8Bit();const char* file = context.file ? context.file : "no-file";const char* function = context.function ? context.function : "no-function";switch (type) {case QtDebugMsg:fprintf(stderr, "%s [Debug] (%s:%u, %s): %s\n", timeText.toLocal8Bit().constData(), file, context.line, function, localMsg.constData());break;case QtInfoMsg:fprintf(stderr, "%s [Info] (%s:%u, %s): %s\n", timeText.toLocal8Bit().constData(), file, context.line, function, localMsg.constData());break;case QtWarningMsg:fprintf(stderr, "%s [Warning] (%s:%u, %s): %s\n", timeText.toLocal8Bit().constData(), file, context.line, function, localMsg.constData());break;case QtCriticalMsg:fprintf(stderr, "%s [Critical] (%s:%u, %s): %s\n", timeText.toLocal8Bit().constData(), file, context.line, function, localMsg.constData());break;case QtFatalMsg:fprintf(stderr, "%s [Fatal] (%s:%u, %s): %s\n", timeText.toLocal8Bit().constData(), file, context.line, function, localMsg.constData());abort(); // 此调用将终止程序}
}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);// 安装日志处理函数qInstallMessageHandler(customMessageHandler);// 生成日志消息qDebug() << "hello,world";return a.exec();
}
效果图
注意事项:
-
需要在main函数安装日志处理函数:
qInstallMessageHandler(customMessageHandler);
,安装这个函数以后,整个项目全局使用,不需要再重新安装 -
可以再CMakeLists.txt中添加以下设置用于控制日志是否输出,如果不禁止注释掉即可
add_definitions(-DQT_NO_DEBUG_OUTPUT) //禁用调试输出 add_definitions(-DQT_NO_INFO_OUTPUT) //禁用信息级别的日志输出 add_definitions(-DQT_NO_WARNING_OUTPUT) //禁用警告级别的日志输出
使用:
以下是详细版的修改版本,只保存主要的信息
我们可以创建一个命名空间用于存放这个自定义日志输出
- Logger.h
#ifndef MYLANDLARDS_SRC_LOGGER_H
#define MYLANDLARDS_SRC_LOGGER_H#include <QString>
#include <QMessageLogContext>namespace Logger {
void customMessageHandler(QtMsgType type,const QMessageLogContext &context,const QString &msg);
QString typeToString(QtMsgType type);
}#endif //MYLANDLARDS_SRC_LOGGER_H
- Logger.cpp
#include "Logger.h"
#include <QDateTime>
#include <QFileInfo>
#include <cstdio>namespace Logger {void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {QString timeText = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");QString file = QFileInfo(context.file ? context.file : "no-file").fileName();QString function = context.function ? context.function : "no-function";QString formattedMessage = QString("%1 [%2] (%3:%4, %5): %6").arg(timeText).arg(typeToString(type)).arg(file).arg(context.line).arg(function).arg(msg);fprintf(stderr, "%s\n", formattedMessage.toLocal8Bit().constData());
}QString typeToString(QtMsgType type) {switch (type) {case QtDebugMsg: return "Debug";case QtInfoMsg: return "Info";case QtWarningMsg: return "Warning";case QtCriticalMsg: return "Critical";case QtFatalMsg: return "Fatal";default: return "Unknown";}
}} // namespace Logger
- main.cpp
#include <QApplication>
#include <QDebug>
#include "Logger.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);qInstallMessageHandler(Logger::customMessageHandler);qDebug() << "hello,world";return QApplication::exec();
}
效果图:
如果最后需要将日志输出到文件中,可以叫ChatGPT修改即可