Saving your logs to a file
A common need for a developer is to have logs. In some situations, you cannot have access to the console output, or you have to study the application state afterwards. In both cases, the log has to be outputted to a file.
Qt provides a practical way of redirecting your logs (qDebug
, qInfo
, qWarning
, and so on) to any device that is convenient for you: QtMessageHandler
. To use it, you have to register a function that will save the logs to the desired output.
For example, in your main.cpp
, add the following function:
#include <QFile> #include <QTextStream> void messageHander(QtMsgType type, const QMessageLogContext& context, const QString& message) { QString levelText; switch (type) { case QtDebugMsg: levelText = "Debug"; break; case QtInfoMsg: levelText = "Info"; break; case QtWarningMsg...