# C++ Android NDK logging (with SDL)

This topic is 374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone,

I'm having some issues adapting my C++/SDL framework to use on Android.  I'm using the NDK and installing directly onto my tablet via the command line and normally it works fine but sometimes of course it does nothing and it's a pain to debug.  So I thought about adding some simple logging functions so at least I can get some basic idea of what happens.  But it seems creating logs in Android is tricky also.  So a couple of questions.

1) Is it correct that you can only use a predefined set of Android logs (i.e. an info, debug, verbose, etc.. log)?  So it's impossible to make your own log-file just from your application and dump all output there to analyse?  Or did I miss something?

2) Since I was using SDL, I tried using their log functions but it seems to dump direct to one of the main logs (containing basically everything) so it was not much help.  SDL also has functions to generate internal files for game save data, etc..  So I hoped I could adapt this to make my own log except these files are private and cannot be read, at least not in the default mode.  Is there any way around this that anyone knows, where I can download an internal private file to my laptop to analyse?

3) Is there an easier solution I've missed?

Thanks in advance :-)

##### Share on other sites
53 minutes ago, sergamer1 said:

1) Is it correct that you can only use a predefined set of Android logs (i.e. an info, debug, verbose, etc.. log)?  So it's impossible to make your own log-file just from your application and dump all output there to analyse?  Or did I miss something?

Android development is a pain, no doubt.

There isn't a standard output you can log to. Logcat is the general method to get logs, but I would recommend using the tag for your app name so you can easily filter. I would also recommend just living with Logcat unless you're willing to put in the time for one of the below.

Other ways, at least until Google further locks Android down:

• Log to a file in /data/local/tmp. This won't work in recent Android versions.
• Write a logging app on your PC that connects to a socket opened by your app and log to the socket. You'll need to setup adb port forwarding and make sure your app has INTERNET permissions. Lots of work just to see logs.
• Create an in-app/game console that you can log messages to. Drawback is having to see them on the device.
58 minutes ago, sergamer1 said:

2) Since I was using SDL, I tried using their log functions but it seems to dump direct to one of the main logs (containing basically everything) so it was not much help.  SDL also has functions to generate internal files for game save data, etc..  So I hoped I could adapt this to make my own log except these files are private and cannot be read, at least not in the default mode.  Is there any way around this that anyone knows, where I can download an internal private file to my laptop to analyse?

Those files will be in the app's data directory, assuming the writes worked. You can access it if you root your device, but you won't be able to see them on a standard commercial device. Aside from adding some file transfer mechanism (see 2nd option in bullet list above), the only other option I can think of is to redirect SDL output to logcat.

1 hour ago, sergamer1 said:

3) Is there an easier solution I've missed?

With all the above I'm assuming you don't want to root your device, so in that case the easy solution is Logcat. Unfortunately it's the best option for Android logging, especially as Google continues to lock-down Android even further. I recommend reading up on the options and parameters so at least you can filter the output. Otherwise you'll need to do extra work for logging files to the app data directory and transferring them back to your PC.

I don't know SDL well enough, but I'd also recommend hooking into the logging system and redirecting output to Logcat.

##### Share on other sites
6 hours ago, khawk said:

There isn't a standard output you can log to. Logcat is the general method to get logs, but I would recommend using the tag for your app name so you can easily filter. I would also recommend just living with Logcat unless you're willing to put in the time for one of the below.

Ahhh, ok.  I've been trying with Logcat and the problem I said was it just tells you everything you didn't want to know what is happening.  But if there's a way to tag and filter the messages, then I think this solution alone would be good enough for my purposes.  Thanks for the suggestion :-)

Is it simple enough to describe here what to do?  Or are you talking about really basic stuff, like adding a string/tag directly to the output and then grepping the file for the tag?  :-)

6 hours ago, khawk said:

With all the above I'm assuming you don't want to root your device, so in that case the easy solution is Logcat.

No, definitely not.  The tagging solution (whatever form it takes) will be simple and workable enough for me (assuming I can get it to work).

6 hours ago, khawk said:

I don't know SDL well enough, but I'd also recommend hooking into the logging system and redirecting output to Logcat.

I saw one or two examples on stackoverflow where using the Android log directly via NDK (so avoiding SDL completely) seemed relatively straightforward.  I'll give it a go if SDL gives me any (more) trouble with this.

Thanks :-)

##### Share on other sites
2 hours ago, sergamer1 said:

Is it simple enough to describe here what to do?  Or are you talking about really basic stuff, like adding a string/tag directly to the output and then grepping the file for the tag?  :-)

Yes, pretty simple. Here's a more complete answer for anyone else who might be wondering this.

Logcat is basically like stdout, but since a mobile app doesn't have a console/terminal it can open, logcat redirects output through the adb/USB connection.

The second parameter to __android_log_print() is the "tag". Typically you would use your app name, but I've also used it for individuals modules or components within the app.

Then when you run adb logcat, use the "-s" parameter. For example:

#include <android/log.h>

...

__android_log_print(ANDROID_LOG_DEBUG, "MyAppName", "This is log output");

Then on command line:

adb logcat -s "MyAppName"

Quite a few other parameters you can play with to get what you want too.. like filtering the log level.

One thing I would recommend for anyone is to create macros around __android_log_print(). This is a common form:

#include <android/log.h>

#define  LOG_TAG    "MyApp"

#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__)

Then you can use printf() style output:

int hitpoints = 100;

while (hitpoints > 0)
{
LOGI("Hitpoints: %d", hitpoints);
hitpoints--;
}

LOGD("We killed it.");

The Hitpoints lines will be with the INFO filter, and the last line will be with the DEBUG filter, both of which you can filter with the logcat command line parameters.

##### Share on other sites
3 hours ago, khawk said:

Yes, pretty simple.

Yes, it worked first time!  Brilliant! :-).  Thank you for your help with that.

3 hours ago, khawk said:

One thing I would recommend for anyone is to create macros around __android_log_print().

Yes, I already had some logging macros exactly like this but wrapped around the SDL_log functions instead so it was pretty trivial to exchange them for the Android log functions.  I rather suspect the SDL_log functions are probably wrappers around this android function also in some way but wasn't clear getting them to work as I wanted.  Anyway, thanks again :-)

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631707
• Total Posts
3001836
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!