【问题标题】:Can logcat be used to log NDK code in Android? Or what are logging options from NDK?logcat 可以用来在 Android 中记录 NDK 代码吗?或者 NDK 的日志记录选项是什么?
【发布时间】:2014-09-28 17:52:41
【问题描述】:

如何从 Android (NDK) 的本机代码中写入日志?有哪些可用选项? 例如,可以从 NDK 内部使用 logcat 来写入日志吗?或者由于它在android中的更高级别,它无法从NDK访问?

目前我只知道用 C 代码编写时间: millis = System.currentTimeMillis();

并且具有将这次写入的功能以及将任何消息写入自定义日志文件。

【问题讨论】:

标签: android android-ndk logcat android-logcat


【解决方案1】:

您可以使用 Android 日志记录

#include <android/log.h>

#define APPNAME "MyApp"

__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "My Log");

另外,请确保您还在 Android.mk 文件中链接到日志库:

LOCAL_LDLIBS := -llog

已经在Any simple way to log in Android NDK code?讨论过

【讨论】:

    【解决方案2】:

    如果您使用的是使用 CMake 的较新的 Android Studio 版本 (2.2+),那么当您生成支持 C++ 的新项目时,您会发现以下内容自动添加到您的 CMakeLists.txt 文件中:

    find_library( # Sets the name of the path variable.
              log-lib
    
              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )
    

    target_link_libraries( # Specifies the target library.
                       your-lib1
                       your-lib2
                       ...
    
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
    

    【讨论】:

    • 我遇到的唯一最新答案,谢谢你!小提示:将每个“目标库”放在单独的“目标链接库”块中。
    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    相关资源
    最近更新 更多