• Qt利用qInstallMessageHandler输出日志QtLogOut


    相关资料:

    https://blog.csdn.net/technologyleader/article/details/82225205
    http://blog.sina.com.cn/s/blog_a6fb6cc90101guc5.html

    qDebug:调试信息
    qWarning:警告信息
    qCritical:严重错误
    qFatal:致命错误

    .pro

     1 QT       += core gui
     2 
     3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
     4 
     5 CONFIG += c++11
     6 
     7 # The following define makes your compiler emit warnings if you use
     8 # any Qt feature that has been marked deprecated (the exact warnings
     9 # depend on your compiler). Please consult the documentation of the
    10 # deprecated API in order to know how to port your code away from it.
    11 DEFINES += QT_DEPRECATED_WARNINGS
    12 
    13 # You can also make your code fail to compile if it uses deprecated APIs.
    14 # In order to do so, uncomment the following line.
    15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
    16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    17 
    18 SOURCES += 
    19     main.cpp 
    20     mainwindow.cpp
    21 
    22 HEADERS += 
    23     mainwindow.h
    24 
    25 FORMS += 
    26     mainwindow.ui
    27 
    28 # Default rules for deployment.
    29 qnx: target.path = /tmp/$${TARGET}/bin
    30 else: unix:!android: target.path = /opt/$${TARGET}/bin
    31 !isEmpty(target.path): INSTALLS += target
    View Code

    main.cpp

     1 #include "mainwindow.h"
     2 
     3 #include <QApplication>
     4 #include <QMutex>
     5 #include <QFile>
     6 #include <QTextStream>
     7 #include <QDateTime>
     8 
     9 void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    10 {
    11     static QMutex mutex;
    12     mutex.lock();
    13 
    14     QString text;
    15     switch(type)
    16     {
    17     case QtDebugMsg:
    18         text = QString("Debug:");
    19         break;
    20 
    21     case QtWarningMsg:
    22         text = QString("Warning:");
    23         break;
    24 
    25     case QtCriticalMsg:
    26         text = QString("Critical:");
    27         break;
    28 
    29     case QtFatalMsg:
    30         text = QString("Fatal:");
    31     }
    32 
    33     QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    34     QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    35     QString current_date = QString("(%1)").arg(current_date_time);
    36     QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);
    37 
    38     QFile file("d:/log.txt");
    39     file.open(QIODevice::WriteOnly | QIODevice::Append);
    40     QTextStream text_stream(&file);
    41     text_stream << message << "
    ";
    42     file.flush();
    43     file.close();
    44 
    45     mutex.unlock();
    46 }
    47 
    48 int main(int argc, char *argv[])
    49 {
    50     QApplication a(argc, argv);
    51 
    52     //注册MessageHandler
    53     qInstallMessageHandler(outputMessage);
    54 
    55     //打印日志到文件中
    56     qDebug("This is a debug message");
    57     qWarning("This is a warning message");
    58     qCritical("This is a critical message");
    59     qFatal("This is a fatal message!");
    60 
    61     MainWindow w;
    62     w.show();
    63     return a.exec();
    64 }
    View Code

    mainwindow.h

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 
     6 QT_BEGIN_NAMESPACE
     7 namespace Ui { class MainWindow; }
     8 QT_END_NAMESPACE
     9 
    10 class MainWindow : public QMainWindow
    11 {
    12     Q_OBJECT
    13 
    14 public:
    15     MainWindow(QWidget *parent = nullptr);
    16     ~MainWindow();
    17 
    18 private:
    19     Ui::MainWindow *ui;
    20 };
    21 #endif // MAINWINDOW_H
    View Code

    mainwindow.cpp

     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 
     4 MainWindow::MainWindow(QWidget *parent)
     5     : QMainWindow(parent)
     6     , ui(new Ui::MainWindow)
     7 {
     8     ui->setupUi(this);
     9 }
    10 
    11 MainWindow::~MainWindow()
    12 {
    13     delete ui;
    14 }
    View Code
    作者:疯狂Delphi
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    欢迎关注我,一起进步!扫描下方二维码即可加我

  • 相关阅读:
    快慢指针
    成绩排序
    简单排序
    个人作业——软件工程实践总结作业
    团队作业——项目验收与总结博客(麻瓜制造者)
    Beta阶段总结博客(麻瓜制造者)
    “福大易宝”宣传文案
    Beta冲刺(5/5)(麻瓜制造者)
    Beta冲刺(4/5)(麻瓜制造者)
    个人作业——软件工程实践总结作业(待续)
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/14666547.html
Copyright © 2020-2023  润新知