C/C++ printfs - Where's it appears in a Android native code?

后端 未结 2 1568
夕颜
夕颜 2020-12-08 20:19

Since It\'s pretty hard to debug native android code, I\'m going to the \"printf trace\" approach.

So, my question is, in a native code, wheres the standards \"print

相关标签:
2条回答
  • 2020-12-08 21:02

    There are shorter macros available in order to log to logcat.

    #define LOG_TAG "my_log_tag"
    #include <cutils/log.h>
    
    ALOGD("Format this %d", some_int);
    

    In Android.mk, add the liblog library to LOCAL_SHARED_LIBRARIES when building in 'mydroid' (full android system build). In case of ndk build LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog can be used.

    include $(CLEAR_VARS)
    LOCAL_MODULE    := foo
    LOCAL_SRC_FILES := foo.c
    # if mydroid
    LOCAL_SHARED_LIBRARIES := liblog
    # in ndk, use LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog instead
    include $(BUILD_EXECUTABLE)
    

    There are various other macros defined for all levels of logging. From cutils/log.h:

    #define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
    #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
    ...
    #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
    
    0 讨论(0)
  • 2020-12-08 21:10

    Log to logcat.

    1) To invoke the logger in native code include the header and call _android_log_write(..).

    #include <android/log.h>
    
    __android_log_write(ANDROID_LOG_INFO, "tag here", "message here");
    

    2) In your Android.mk file include the log lib like this.

    LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 
    
    0 讨论(0)
提交回复
热议问题