Qt利用qInstallMessageHandler输出日志QtLogOut
2023-09-14 08:57:11 时间
相关资料:
https://blog.csdn.net/technologyleader/article/details/82225205
http://blog.sina.com.cn/s/blog_a6fb6cc90101guc5.html
qDebug:调试信息
qWarning:警告信息
qCritical:严重错误
qFatal:致命错误
PS:release版本下会出现没有文件与行信息,需要在.pro中引入
DEFINES += QT_MESSAGELOGCONTEXT
PS:中文乱码问题处理:
输入时如下:
QString str = QStringLiteral("中文");
qDebug(str.toStdString().data());
写入文件时如下:
QFile file("d:/log.txt");
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream text_stream(&file);
text_stream.setCodec("utf-8");
text_stream << message << "\r\n";
file.flush();
file.close();
.pro
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
main.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 << "\r\n"; 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 }
mainwindow.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
mainwindow.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
相关文章
- Qt-QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connect...
- QT-Qt图片按钮类
- QT-Qt中MediaPlay的使用,播放视频或MP4
- QT-Qt显示GIF图片
- QT-在Qt Widgets Application中怎么显示printf的输出内容?
- qt 向窗口发送消息,键盘输入事件
- s3c2410上搭建QT/Embedded4.8.5开发环境(二)--安装arm平台qt库qt-everywhere-opensource-src-4.8.5
- s3c2410上搭建QT/Embedded4.8.5开发环境(七)--命令行开始第一个Qt程序
- Qt开源作品21-日志重定向输出类
- Qt高级——Qt日志信息处理
- Qt日志系统
- Qt中QJsonValue类
- Qt QtConcurrent多线程 - map、mapped和mappedReduced
- Qt多线程1:QThread
- QT配置opencv_qt调用opencv
- QT中槽的使用
- 2、QT基础——创建Qt项目
- Qt如何实现界面的阴影效果?
- Qt 自定义日志类
- Qt按钮左边图片右边文字
- Qt中实现悬浮窗口
- Qt学习一门:直接使用QT具