Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging values of variables in Android native ndk

I set up logging with C++ in Android NDK.

I can print a message to logcat like this:

__android_log_write(ANDROID_LOG_INFO, "tag here", "message here");

Now let's say I have an integer called testint. How can I print the value of this int?

Something like this prints the address, but I want the value. I haven't found anything in C++ on how to do this. Thanks for any help!

__android_log_print(ANDROID_LOG_INFO, "sometag", "%p", *test);
like image 722
dorien Avatar asked Aug 28 '12 12:08

dorien


2 Answers

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
like image 52
Adam Avatar answered Oct 27 '22 06:10

Adam


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);
like image 53
Benoit Duffez Avatar answered Oct 27 '22 05:10

Benoit Duffez