Reputation: 18251
According manual in order to log into file I need to open it in 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();
}
Then I can log to it:
qDebug(logDebug()) << "Hello Debug";
But how to organize logging in more convenient way with text formatting like printf
:
printf("Hello Debug %d, my_int);
Upvotes: 1
Views: 1845
Reputation: 3911
Additionally to stream-like API you can simply use qDebug
like printf
.
qDebug("Hello Debug %d", my_int);
Similar with logging 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}
Upvotes: 3
Reputation: 13238
You can use QString::asprintf()
or, preferably QString::arg()
to produce QString
using printf
-like format syntax and then use it with qDebug()
.
Upvotes: 0
Reputation: 12831
you can simply write the integer object.
qDebug(logDebug()) << "Hello Debug "<< my_int;
or
qDebug(logDebug()) << "Hello Debug"<< " "<<my_int;
Upvotes: 0