Any simple way to log in Android NDK code?
Asked Answered
G

7

93

I'm looking for a way to easily debug C code in an Android NDK application using Eclipse. I've read ways to debug the app using gdb or something similar but what I want is a way to push messages to Eclipse somehow.

I'm looking for a solution that's as simple as using a print function in C and seeing it in the DDMS Log or anything similar. Does anyone have any experience doing this?

Geoponics answered 7/1, 2011 at 19:11 Comment(0)
U
154

You can use the Android logging facilities:

#include <android/log.h>

#define APPNAME "MyApp"

__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of 1 + 1 is %d", 1+1);

Make sure you also link against the logging library, in your Android.mk file:

  LOCAL_LDLIBS := -llog
Unilateral answered 7/1, 2011 at 20:3 Comment(1)
If using the new Gradle NDK integration in Android Studio (Gradle experimental), you need to add this line ldLibs.addAll(['android','log']) to your android.ndk options .Stupefacient
E
42

No one has posted info about different log levels so far. The answer is an attempt to make the logging "picture" full.

#include <android/log.h>

#define TAG "MY_TAG"

#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,    TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,     TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,     TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,    TAG, __VA_ARGS__)

Usage:

char err[] = "wrong";
LOGE("Something went %s", err);

Link Android log library as below.

Android.mk:

LOCAL_LDLIBS := -llog

CMakeLists.txt:

find_library( log-lib log )
target_link_libraries( ${log-lib} )

Further reading: Logging

Endorse answered 10/11, 2018 at 14:26 Comment(1)
When using CMake you need to pass in the name of your target library you are linking the logging library to as the first parameter. So target_link_libraries( your-library-name ${log-lib} ) then it worked for me. Thanks for this answer.Kinney
S
14

The easiest way is probably to redirect printf() statements to the system log (based on the "Viewing stdout and stderr" section of the official ADB reference manual.

Type these 3 commands on a command line:

adb shell stop
adb shell setprop log.redirect-stdio true
adb shell start

Then you can view the output of your "printf()" statements by looking at the "LogCat" window of Eclipse Debugger, or by typing this on a command line:

adb logcat

Just be aware that since the data is buffered before transferring from the emulator or device, you should definitely flush the stdout buffer, eg:

printf("Hello, I am %d years old!\n", 30);
fflush(stdout);

You should then see a log message starting with "I/stdout:"

Segment answered 11/8, 2011 at 6:32 Comment(1)
Note that this solution breaks JUnit tests. See: #3463350Squander
T
5

You can also a little util

#include <android/log.h>

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-lib", __VA_ARGS__))

Usage:

std::string hello = "Hello from C++";
int a = 1;
LOGI("int %d, string: %s", a, hello.c_str());
Tinkling answered 5/11, 2018 at 12:36 Comment(1)
How to see these logs in linux terminalParapodium
L
2

An alternative solution (using a debugger) is explained here:

How can I effectively debug C code that's wrapped with JNI in Eclipse? (Android Dev)

Liftoff answered 9/2, 2011 at 8:5 Comment(0)
E
2

ADT 20 includes an NDK plugin that provides support for building and debugging NDK projects in Eclipse. This document describes how to install and use the NDK plugin. Instructions are pretty straightforward and consist of only a few steps.

This is the simplest solution I found and it worked for me.

Note: If you are using ADT bundle you only need to install C development tools with install new software (see the screenshot) and you can go to "Using the NDK plugin" part immediately.

c dev tools install

Edit: It seems there is an issue with CDT in eclipse juno http://code.google.com/p/android/issues/detail?id=33788 causing eclipse's debugger to be unable to find breakpoints. Workaround I used is to start app in debug mode (not debug as native app but 'regular' debug) and then in command line I went to my project root and typed ndk-gdb (this creates gdb.setup file in obj/local/armeabi folder). After that breakpoints worked as usual.

In comments related to the issue on the link above they suggest some other workarounds but I didn't try them since they seemed to require more effort than this.

Elburt answered 3/5, 2013 at 11:19 Comment(0)
R
0

For anyone who's struggling with

LOCAL_LDLIBS := -llog

not working, just add it after each

include $(CLEAR_VARS)

Not sure why, but it worked for me.

Cheers!

Rupertruperta answered 9/10, 2020 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.