Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize Qt Message Handler on Android

I'm using qSetMessagePattern() to customize the log formatting of a Qt application. On Windows, qDebug() outputs exactly what I expect, but on Android it adds some information before. For example

qSetMessagePattern("[D] %{message}");
qDebug("Debug test");

Windows output:

[D] Debug test

Android output:

D/Qt  (30070): ..\project\filename.cpp:521 (void Class::method()): [D] Debug test

I've tried making a custom handler (and installing it with qInstallMessageHandler()), but I don't know how the message handler should output debug information on Android

The question: How to make Android message handler to stop prepending that information, or how to write a custom message handler for Android.

like image 937
Smasho Avatar asked Sep 16 '25 16:09

Smasho


1 Answers

Found the code of the default message handler for android, and formatting is hardcoded, so I write a similar version:

#include <android/log.h>

static void android_message_handler(QtMsgType type,
                                  const QMessageLogContext &context,
                                  const QString &message)
{
    android_LogPriority priority = ANDROID_LOG_DEBUG;
    switch (type) {
    case QtDebugMsg: priority = ANDROID_LOG_DEBUG; break;
    case QtWarningMsg: priority = ANDROID_LOG_WARN; break;
    case QtCriticalMsg: priority = ANDROID_LOG_ERROR; break;
    case QtFatalMsg: priority = ANDROID_LOG_FATAL; break;
    };

    __android_log_print(priority, "Qt", "%s", qPrintable(message));
}

And installed it with:

qInstallMessageHandler(android_message_handler);
like image 70
Smasho Avatar answered Sep 18 '25 11:09

Smasho