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

Exception or error:

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 “printf(“something”)” appears when running a Android application?

How to solve:

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 

###

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__))

Leave a Reply

Your email address will not be published. Required fields are marked *