当前位置: 首页 > news >正文

Qt自定义日志输出

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修改即可

http://www.lryc.cn/news/373987.html

相关文章:

  • [C++] vector list 等容器的迭代器失效问题
  • Java——变量作用域和生命周期
  • WPF界面设计
  • 【C#】使用JavaScriptSerializer序列化对象
  • HTML静态网页成品作业(HTML+CSS)—— 明星吴磊介绍网页(5个页面)
  • EasyRecovery2024数据恢复神器#电脑必备良品
  • 前端HTML相关知识
  • 集合面试题
  • 集成学习概述
  • 记录一次root过程
  • 函数(上)(C语言)
  • ARM-V9 RME(Realm Management Extension)系统架构之系统安全能力的侧信道抵御
  • Java高级技术探索:深入理解JVM内存分区与GC机制
  • 新视野大学英语2 词组 6.15
  • 【JavaScript】MDN
  • Qt/C++中的异步编程
  • 解决javadoc一直找不到路径的问题
  • 存储器的性能指标以及层次化存储器
  • 【C++】C++入门的杂碎知识点
  • springboot 整合redis问题,缓存击穿,穿透,雪崩,分布式锁
  • 免费个人站 独立站 wordpress 自建网站
  • 散列函数的基本概念
  • 【C++拷贝构造函数深浅拷贝】
  • 快速编译安装tensorrt_yolo
  • 外盘黄金期货需要注意什么?
  • Allegro光绘Gerber文件、IPC网表、坐标文件、装配PDF文件导出打包
  • mysql的索引可以分为哪些类型
  • Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
  • 【JavaEE进阶】——利用框架完成功能全面的图书管理系统
  • WDF驱动开发-内存缓冲区