Logging values of variables in Android native ndk Logging values of variables in Android native ndk android android

Logging values of variables in Android native ndk


Here's the most concise way I've seen:

#include <android/log.h>#define  LOG_TAG    "someTag"#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)#define  LOGW(...)  __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)...// Now you can log very simply like this:int foo = 42;LOGD( "This is a number from JNI: %d", foo );

Also, make sure you link to the log library in your Android.mk:

LOCAL_LDLIBS    := -llog


You could use __android_log_print which uses a sprintf-like syntax that formats your data into a string.

__android_log_print(ANDROID_LOG_INFO, "sometag", "test int = %d", testInt);


Take advantage of the variadic log print function you have available. For my own code, I provide a LogInfo() function to make it simple. Of course there are several options available to you here.

void LogInfo(const char *sTag, const char *fmt, ...){  va_list ap;  va_start(ap, fmt);  __android_log_vprint(ANDROID_LOG_INFO, sTag, fmt, ap);  va_end(ap);}