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,136
  • 30
  • 106
  • 167
  • possible duplicate of [How to see printf lines in gdb?](http://stackoverflow.com/questions/5459324/how-to-see-printf-lines-in-gdb) – richq Jun 21 '11 at 18:34
  • Possible duplicate of [Is "std::cout" usable in Android-ndk](https://stackoverflow.com/questions/8870174/is-stdcout-usable-in-android-ndk) – Ciro Santilli OurBigBook.com Nov 06 '17 at 09:40
  • My question was from one year before @CiroSantilli刘晓波死六四事件法轮功 – Marcos Vasconcelos Nov 06 '17 at 13:39
  • Hi Marcos, the current consensus is to close by "quality": http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha Since "quality" is not measurable, I just go by upvotes. ;-) Likely it comes down to which question hit the best newbie Google keywords on the title. – Ciro Santilli OurBigBook.com Nov 06 '17 at 15:10
  • Not willing to improve the question quality though? The other question is about the physical space where the log appears to, I'm asking how to see in logcat. – Marcos Vasconcelos Nov 06 '17 at 15:18

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,209
  • 3
  • 42
  • 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,129
  • 23
  • 18
gfrigon
  • 2,237
  • 2
  • 23
  • 32