根据manual为了登录文件,我需要在 main
中打开它:
QScopedPointer<QFile> m_logFile;
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
m_logFile.reset(new QFile("logFile.txt"));
m_logFile.data()->open(QFile::Append | QFile::Text);
qInstallMessageHandler(messageHandler);
MainWindow w;
w.show();
return a.exec();
}
然后我就可以登录了:
qDebug(logDebug()) << "Hello Debug";
但是如何使用像 printf
这样的文本格式以更方便的方式组织日志记录:
printf("Hello Debug %d, my_int);
最佳答案
除了类似流的 API,您还可以简单地使用 qDebug
像 printf
。
qDebug("Hello Debug %d", my_int);
类似于日志category :
qCDebug(category, "Hello Debug %d", my_int);
Tip
The following message format produces nice links to the lines printed the trace in the QtCreator's Application Output pane.
QT_MESSAGE_PATTERN=[%{time process}] %{threadid} file:/%{file}:%{line}: %{message} %{if-critical}%{backtrace}%{endif}
关于c++ - 带文本格式的 QT 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49635726/