Qt利用qInstallMessageHandler输出日志QtLogOut

相关资料:

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

 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

相关文章: