Can logcat be used to log NDK code in Android? Or what are logging options from NDK?
Asked Answered
O

2

18

How would one write logs from inside Native code in Android (NDK)? What are the available options? For example, can logcat be used from inside of NDK to write logs? Or since its more upper level in android, it can not be accessible from NDK?

At the moment I am just aware of writing times from C code with: millis = System.currentTimeMillis();

And with function that would write this time plus any messages to a custom log file.

Optime answered 5/8, 2014 at 22:39 Comment(4)
possible duplicate of Any simple or easy way to debug Android NDK code?Mattress
u can also refer-android-harvest.blogspot.com/2011/06/…Chun
related #12159816Rancorous
Possible duplicate of What is the Log API to call from an Android JNI program?Rancorous
A
43

You can use the Android logging

#include <android/log.h>

#define APPNAME "MyApp"

__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "My Log");

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

LOCAL_LDLIBS := -llog

It has already been discussed at Any simple way to log in Android NDK code?

Adlai answered 5/8, 2014 at 22:52 Comment(0)
S
23

If you are using the newer Android Studio versions (2.2+) that use CMake, then you will find the following automatically added to your CMakeLists.txt file when you generate a new project with C++ support:

find_library( # Sets the name of the path variable.
          log-lib

          # Specifies the name of the NDK library that
          # you want CMake to locate.
          log )

and

target_link_libraries( # Specifies the target library.
                   your-lib1
                   your-lib2
                   ...

                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )
Specify answered 2/2, 2017 at 11:41 Comment(1)
The only up to date answer I came across, thank you man!A little hint: place each of your 'target lib' in a separate 'target_link_libraries' blocks.Crank

© 2022 - 2024 — McMap. All rights reserved.