Friday, December 02, 2011

Android NDK logging

Just wanted to share the snipped that often used in the different Android native projects.
The following are some useful macros for logging with NDK.

#include<android/log.h>

#define LOGD(LOG_TAG, ...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGV(LOG_TAG, ...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define LOGE(LOG_TAG, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

You can define more as needed just take a look at the android/log.h header file for all available priority values.
Then I would usually define log TAG:
#define LOG_TAG "MyModule"

and use the macros like this:
LOGD(LOG_TAG, "my val: %s", val);


2 comments:

Anonymous said...

don't forget to link the native Android log library in Android.mk

append the line

LOCAL_LDLIBS := -llog

to Android.mk and the log output should show in logcat

Alex said...

This is correct, just forgot to mention it.