23

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?

Marcos Vasconcelos
  • 18,038
  • 29
  • 106
  • 167

2 Answers2

42

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 
Ryan Reeves
  • 10,099
  • 3
  • 41
  • 26
2

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__))
Rom
  • 4,053
  • 22
  • 18
gfrigon
  • 2,227
  • 2
  • 21
  • 32