QT on Android: output qt Debug information to Logcat

Source: Internet
Author: User

All rights reserved Foruok. Please specify the source (Http://blog.csdn.net/foruok) If you wish to reprint.

Assuming that you performed the Qt on Android app on the target Android device, you might 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, displaying the log time at the same time. For example, adb logcat-v time-s qnote. Just display the log information labeled Qnote; If you want to filter multiple labels at the same time, you can separate the labels with commas in English. such as ADB logcat-v time-s qnote,test.

In order to import logs 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 easy, I declare a function installlogcatmessagehandler, assuming that the ANDROID macro is undefined. It is an empty macro and does nothing; otherwise, a message filter is installed. The message that takes over the Qt output is 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

The implementation is also very easy, calling Qinstallmessagehandler to set Messageoutput2logcat as the default message handler 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 directly use these two files and add them to your project.

Then include the QDebug2Logcat.h header file before the main () function, and add the following code to the first line of the main () function body:

Installlogcatmessagehandler ("Yourlogtag");

All right, everything's ready.


All rights reserved Foruok. Please specify the source (Http://blog.csdn.net/foruok) If you wish to reprint.

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:

    • Introduction to the development of Qt 5.2 for Android under Windows
    • Qt for Android deployment process Analysis
    • Qt for Android compiled pure Cproject
    • Windows under Qt for Android compiled Android C language executable program
    • Qt on Android: text-specific explanation Hello World whole process

QT on Android: output qt Debug information to Logcat

Related Article

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.