本文介绍了在adb logcat输出中查看从Android上的Qt应用进行日志记录的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NB我的 一个QtCreator用户.我使用qmake,make和androiddeployqt在构建脚本中构建android应用,然后通过adb install将它们部署到设备上.

NB I am not a QtCreator user. I build android apps in build scripts with qmake, make and androiddeployqt, and deploy them to the device with adb install.

我希望能够在abd logcat输出中看到qDebug,qInfo等的输出,以及qml conole.log的任何输出以及来自QML引擎的任何其他颤动.但是,在适用于Android的Qt应用程序的原始版本中,任何此类消息似乎都显得毫无意义(或者至少我不知道它们的去向).

I'd like to be able to see the output of qDebug, qInfo etc, and also any qml conole.log output and any other chatter from the QML engine, in abd logcat output. But in a vanilla build of a Qt app for Android, any such messages just seem to be blackholed (or at least I have no idea where they're going).

结合以下各项,我取得了一些成功:

I have had some success by the combination of:

  • Redirecting logging to stderr using qInstallMessageHandler (as shown here).

使用代码此处. (更新:随后发现这种方法相当不令人满意,因为它依赖于侦听管道的另一个线程,并且崩溃的应用可能会在崩溃之前立即丢失日志记录.)

Redirecting stderr to Android logging using the code here. (Update: subsequently discovered this approach is rather unsatisfactory as it relies on another thread listening on a pipe, and crashing apps can lose the logging immediately preceeding the crash).

但是,这一切似乎有些笨拙,而且过于复杂.当然有更好更好的方法了吗? (例如,在Windows上使用Qt,您可以简单地使用CONFIG += console进行构建,以获取显示日志记录的控制台窗口,但该选项特定于Windows.)

But this all seems a bit clunky and and overcomplicated. Surely there's a better and simpler way? (For example, with Qt on Windows you can simply build with CONFIG += console to get a console window with logging displayed, but that option is Windows specific).

Qt版本.

Google搜索此输出重定向问题引起了对adb shell setprop log.redirect-stdio true的大量提及,但是据我所知,它对Qt应用程序的stout/stderr没有影响.

Googling this output redirection issue turns up quite a lot of mention of adb shell setprop log.redirect-stdio true, however so far as I can tell it has no effect on Qt apps' stout/stderr.

推荐答案

实际上,结合问题中链接的两个解决方案的各个方面至少可以将其简化为一个功能:

Actually, combining aspects of the two solutions linked in the question at least reduces it to one function:

const char*const applicationName="myapp";

#ifdef ANDROIDQUIRKS  // Set in my myapp.pro file for android builds
#include <android/log.h>

void myMessageHandler(
  QtMsgType type,
  const QMessageLogContext& context,
  const QString& msg
) {
  QString report=msg;
  if (context.file && !QString(context.file).isEmpty()) {
    report+=" in file ";
    report+=QString(context.file);
    report+=" line ";
    report+=QString::number(context.line);
  }
  if (context.function && !QString(context.function).isEmpty()) {
    report+=+" function ";
    report+=QString(context.function);
  }
  const char*const local=report.toLocal8Bit().constData();
  switch (type) {
  case QtDebugMsg:
    __android_log_write(ANDROID_LOG_DEBUG,applicationName,local);
    break;
  case QtInfoMsg:
    __android_log_write(ANDROID_LOG_INFO,applicationName,local);
    break;
  case QtWarningMsg:
    __android_log_write(ANDROID_LOG_WARN,applicationName,local);
    break;
  case QtCriticalMsg:
    __android_log_write(ANDROID_LOG_ERROR,applicationName,local);
    break;
  case QtFatalMsg:
  default:
    __android_log_write(ANDROID_LOG_FATAL,applicationName,local);
    abort();
  }
}
#endif

...

int main(int argc,char* argv[]) {

  QGuiApplication app(argc,argv);
#ifdef ANDROIDQUIRKS
  qInstallMessageHandler(myMessageHandler);
#endif
  app.setApplicationName(applicationName);
  ...

这可以从C ++ 获得qInfo() <<消息,从QML获得 console.log消息.它没有得到的是stdout/stderr输出,但是我可以忍受那些可以始终使用Qt日志记录的新代码.

This gets things like qInfo() << messages from C++ and console.log messages from QML. What it doesn't get is stdout/stderr output, but I can live with that for new code which can use Qt logging throughout.

在logcat中,QML代码Component.onCompleted: console.log("QML completed")生成

In logcat, the QML code Component.onCompleted: console.log("QML completed") results in

08-02 12:27:53.378  1199  1220 D myapp : QML completed in file qrc:///main.qml line 7 function onCompleted

,在C ++中,代码qInfo() << "QQuickViewer setup completed";产生

and in C++ the code qInfo() << "QQuickViewer setup completed"; produces

08-02 12:27:53.562  1199  1220 I myapp : QQuickViewer setup completed

甚至在实例化QGuiApplication之前安装消息处理程序似乎同样有效,但是它不会生成任何其他输出(我注意到iOS上的Qt在启动时会在xcode控制台上喷出各种各样的东西,例如,性能警告链接到 https://wiki.qt.io/V4 ,想知道Android是否做了同样的事情;但是是否发出安装我的处理程序之前必须存在的所有内容.

Installing the message handler even before QGuiApplication is instantiated seems to work equally well, but it doesn't generate any additional output (I note Qt on iOS spews all sorts of stuff on startup to the xcode console e.g a performance warning linking to https://wiki.qt.io/V4 and wondered if Android did the same; but if it's emitting anything it must be before my handler is installed).

这篇关于在adb logcat输出中查看从Android上的Qt应用进行日志记录的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:38
查看更多