Logging values of variables in Android native ndk
Asked Answered
S

4

55

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);
Sciomachy answered 28/8, 2012 at 12:34 Comment(0)
P
53

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);
Phebe answered 28/8, 2012 at 12:39 Comment(0)
C
77

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
Cosmopolite answered 21/11, 2013 at 2:10 Comment(3)
how can i print c++ string using this?Helbonna
same way just call the defined macro: LOGI("This is a String");Scorify
Oh man! This is awesome! I used these macros 4 years ago and here I'm again! Thanks ;)Moreno
P
53

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);
Phebe answered 28/8, 2012 at 12:39 Comment(0)
O
16

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);
}
Osanna answered 28/8, 2012 at 12:46 Comment(7)
best to implement the above...niceBevel
Thanks for this solution @Osanna but I'm receiving the following error implementing it exactly as shown above: A/libc(18350): Fatal signal 7 (SIGBUS) at 0x00000000 (code=128), thread 18410 (WebViewCoreThre). Furthermore, the parameters are not correctly printed while whether they are not pointing to the right memory address. Do you have any idea about that ? Thanks very muchIndemonstrable
In fact, I had the mistake to use __android_log_print instead of __android_log_vprint. With this last one the values are well displayed in the console, but I always get the error above after few seconds and the app is killed.Indemonstrable
@Indemonstrable the error you've pasted means you've tried to access a NULL memory location -- you're possibly using a pointer that has not been properly initialized. It's got too many possibilities to diagnose via Stack Overflow's comments feature. I would suggest you try to find exactly which line the error happens on (by adding extra log statements to see how far your code gets). If you still can't figure it out, you should post a question along with the error and the area of code the crash happens in.Osanna
The error happens exactly on the line ` __android_log_vprint(ANDROID_LOG_INFO, sTag, fmt, ap);` when I try to print double or float (with integer I don't get the issue). The variable passed to logInfo is initialized as it double var = 7.7; for instance. When I'm calling __android_log_vprint directly (without using logInfo) then I don't get the issue anymore.Indemonstrable
I don't know the cause of what you're seeing (which is another good reason to post it as a new question; you'll get attention from more than just me). If you just want a quick fix though, your description makes it sound like @Adam's macro based suggestion (answered on this page) would get you going.Osanna
Thanks very much. I was so convinced that my non systematic issue was due to the logPrint function which I was just integrated. So, I'm not look for another possibility. Thanks to your previous comment to add extra log statements. So I found that another error case occurred causing a function to return abnormally. Sorry for the inconvenience and thanks again.Indemonstrable
T
11

__android_log_print() takes a format string and a variable argument list. The format specifier you're looking for to print out a signed integer is "%d". So something like this is what you want:

int foo = 42;
__android_log_print(ANDROID_LOG_INFO, "SomeTag", "foo is %d", foo);

For more information on format strings, you can see the sprintf manual.

Tonneson answered 29/8, 2012 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.