c++ - 带文本格式的 QT 日志记录

标签 c++ qt

根据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,您还可以简单地使用 qDebugprintf

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/

相关文章:

c++ - QT 中的 moc 不会编译我的 c++ 头文件。说它太不同了

c++ - 尝试学习 boost::intrusive Q2

c++ - qt 背景样式表不起作用

c++ - 对象数组 C++ (Embarcadero) 的问题

c++ - 与要定义的规则序列相关的 Makefile 行为

c++ - 在执行期间将大型多维数组读取和写入二进制文件并返回数组?

c++ - QTcpServer 在 QRunnable 中工作时没有收到任何数据

c++ - 初始化列表中的 QString 导致访问冲突。这里出了什么问题?

c++ - 等待多个对象发出信号

c++ - QSqlDatabase : QMYSQL driver not loaded on Ubuntu 15. 04 64位