QT中日志框架

#include "mainwindow.h"
#include <QtWidgets/QApplication>
#include <QSettings>
#include <QDebug>
#include <QFile>
#include <iostream>
#include <QDebug>
#include <QtMessageHandler>
#include <QFile>
#include <QDir>
#include <QDateTime>
using namespace std;
void WriteLog(QString str, QString LogType)
{
    QString fileFolder = qApp->applicationDirPath() + "/log/" + QDateTime::currentDateTime().toString("yyyy-MM-dd");
    QDir dir(fileFolder);
    if (!dir.exists())
    {
        dir.mkpath(fileFolder);
    }
    QString filePath = QString("%1/%2.log").arg(fileFolder).arg(LogType);
    QString strToWrite = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    strToWrite.append(QString("\r\n日志消息:%1").arg(str));
    strToWrite.append("\r\n---------------------------------------------------------------------");
    QFile file(filePath);
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << strToWrite << "\r\n";
    file.flush();
    file.close();
    //打印到控制台
    //std::cout << strToWrite.toLocal8Bit().constData() << std::endl;
}
//注册函数
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QString txtMessage = "";
    QString messageType = "";
    switch (type) {
    case QtDebugMsg:    //调试信息提示
        messageType = "Debug";
        txtMessage = QString("Debug: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
        break;
    case QtInfoMsg:
        messageType = "Info";
        txtMessage = QString("Warning: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
        break;
    case QtWarningMsg:    //一般的warning提示
        messageType = "Waring";
        txtMessage = QString("Warning: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
        break;
    case QtCriticalMsg:    //严重错误提示
        messageType = "Critical";
        txtMessage = QString("Critical: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
        //    PostErrorMessage(txtMessage,messageType);
        break;
    case QtFatalMsg:    //致命错误提示
        messageType = "Fatal";
        txtMessage = QString("Fatal: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
        abort();
    }
    WriteLog(txtMessage, messageType);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //注册监听级别函数
    qInstallMessageHandler(myMessageOutput);

    MainWindow w;
    //w.show();

    return a.exec();
}

在QT的main.cpp文件中加入以上代码即可,使用时直接用不同的 qDebug() << "输出》》》"就行;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值