QT on Android: output qt Debug information to Logcat

Source: Internet
Author: User

Copyright Foruok, if you need to reprint please specify the source (Http://blog.csdn.net/foruok) .

If you run the Qt on Android app on your target Android device, you may want to see the log information for the program output. There are ADB tools in the Android SDK, and when you are connected to a target device, you can use ADB logcat to view the log of the app output on your Android device.

Let's introduce the use of ADB tools. In general I often use the following commands:

    • ADB logcat, view all logs that are output on your phone or other device
    • ADB logcat-v time, let the log with the timing information
    • ADB logcat-v time-s tag, displays only the log information for the specified label, and displays the log time. For example, adb logcat-v time-s qnote, only display the log information labeled Qnote, if you want to filter multiple labels at the same time, you can separate the tags in English with commas, such as ADB logcat-v time-s qnote,test.
In order to import the log into the log system of the Android system (a circular memory log system), I wrote an auxiliary function to make it easy for everyone to use.

First-qDebug2Logcat.h file:

#ifndef qdebug2logcat_h#define qdebug2logcat_h#ifdef androidvoid installlogcatmessagehandler (const char *TAG); #else # Define Installlogcatmessagehandler (TAG) #endif #endif//Qdebug2logcat_h

Very simple, I declare a function installlogcatmessagehandler, if not defined Android macro, it is an empty macro, do nothing; otherwise, install a message filter, take over the QT output message, forwarded to the ANDROID log system.

See Source file QDebug2Logcat.cpp:

#if defined (ANDROID) #include "qDebug2Logcat.h" #include <android/log.h> #include <QDebug> #include < qbytearray>static const char *g_tag = 0;static void Messageoutput2logcat (qtmsgtype type,    const Qmessagelogcontext &context,    const QString &msg) {    int prio = android_log_verbose;    Qbytearray localmsg = Msg.tolocal8bit ();    Switch (type) {case    qtdebugmsg:        prio = android_log_debug;        break;    Case QTWARNINGMSG:        prio = Android_log_warn;        break;    Case QTCRITICALMSG:        prio = android_log_info;        break;    Case QTFATALMSG:        prio = android_log_fatal;        Abort ();    }    __android_log_write (Prio, G_tag, Localmsg.data ());} void Installlogcatmessagehandler (const char *tag) {    G_tag = (TAG = = 0?) "Qdebug": TAG);    Qinstallmessagehandler (MESSAGEOUTPUT2LOGCAT);} #endif

Implementation is also very simple, call Qinstallmessagehandler to set Messageoutput2logcat as the default message processor for the QT application. The MESSAGEOUTPUT2LOGCAT function maps the debug message level of Qt to the log level of Android and calls the __android_log_write () function to write log information to the Android log system.

You can use these two files directly and add them to your project. Then include the QDebug2Logcat.h header file before the main () function, and add the following code in the first line of the main () function body:

Installlogcatmessagehandler ("Yourlogtag");

All right, everything's ready.

Copyright Foruok, if you need to reprint please specify the source (Http://blog.csdn.net/foruok) .

I translate the great god BogDan Vatra's Qt on Android series article:

    • Qt on Android episode 1 (translation)
    • Qt on Android episode 2 (translation)
    • Qt on Android episode 3 (translation)
    • Qt on Android episode 4 (translation)

My series on the Qt on Android article:

    • under the Windows QT For Android Development
    • QT for Android deployment process Analysis
    • QT for Android compilation Pure C project
    • Windows under Qt for Android build Android C Language executable
    • Qt on Android: Text-to-Speech description Hello World whole process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.