From d394ca7f27197cfbfc28eb9a08eb0db261dd9d3d Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Tue, 17 Jan 2012 16:20:45 +0100 Subject: QtDebug: Include file, line, function information Record the file, line, and function where a qDebug, qWarning, qCritical or qFatal call happens, and make this information available in a custom message handler. The patch uses the C preprocessor to replace qDebug, qWarning, ... with a line that also records the current file, line, and function. Custom message handlers can access this information via a new QMessageLogContext argument. Change-Id: I0a9b89c1d137e41775932d3b1a35da4ebf12d18d Reviewed-by: David Faure --- doc/src/snippets/code/src_corelib_global_qglobal.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'doc/src/snippets/code') diff --git a/doc/src/snippets/code/src_corelib_global_qglobal.cpp b/doc/src/snippets/code/src_corelib_global_qglobal.cpp index a45a136255..21bea7aef8 100644 --- a/doc/src/snippets/code/src_corelib_global_qglobal.cpp +++ b/doc/src/snippets/code/src_corelib_global_qglobal.cpp @@ -246,27 +246,27 @@ const TInputType &myMin(const TInputType &value1, const TInputType &value2) #include #include -void myMessageOutput(QtMsgType type, const char *msg) +void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const char *msg) { switch (type) { case QtDebugMsg: - fprintf(stderr, "Debug: %s\n", msg); + fprintf(stderr, "Debug: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function); break; case QtWarningMsg: - fprintf(stderr, "Warning: %s\n", msg); + fprintf(stderr, "Warning: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function); break; case QtCriticalMsg: - fprintf(stderr, "Critical: %s\n", msg); + fprintf(stderr, "Critical: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function); break; case QtFatalMsg: - fprintf(stderr, "Fatal: %s\n", msg); + fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function); abort(); } } int main(int argc, char **argv) { - qInstallMsgHandler(myMessageOutput); + qInstallMessageHandler(myMessageOutput); QApplication app(argc, argv); ... return app.exec(); @@ -532,6 +532,11 @@ class MyClass : public QObject CApaApplication *myApplicationFactory(); //! [47] + +//! [49] +void myMessageHandler(QtMsgType, const QMessageLogContext &, const char *); +//! [49] + //! [qlikely] // the condition inside the "if" will be successful most of the times for (int i = 1; i <= 365; i++) { -- cgit v1.2.3