54

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);
Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
dorien
  • 5,265
  • 10
  • 57
  • 116

4 Answers4

75

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
Adam
  • 25,966
  • 23
  • 76
  • 87
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);
Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
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);
}
mah
  • 39,056
  • 9
  • 76
  • 93
  • best to implement the above...nice – Houston Feb 08 '13 at 06:58
  • 1
    Thanks for this solution @mah 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 much – Lisarien Mar 19 '14 at 10:16
  • 1
    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. – Lisarien Mar 19 '14 at 10:57
  • 1
    @Lisarien 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. – mah Mar 19 '14 at 11:05
  • 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. – Lisarien Mar 19 '14 at 13:01
  • 1
    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. – mah Mar 19 '14 at 13:17
  • 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. – Lisarien Mar 19 '14 at 13:55
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.

kelnos
  • 874
  • 5
  • 11