从Android JNI程序调用的日志API是什么?

时间:2021-09-14 18:54:05

I would like to debug a JNI C application by inserting log messages to logcat. What is the C API that does this?

我想通过向logcat插入日志消息来调试JNI C应用程序。做这个的C API是什么?

3 个解决方案

#1


87  

Like this:

是这样的:

#include <android/log.h>


__android_log_write(ANDROID_LOG_ERROR, "Tag", "Error here");//Or ANDROID_LOG_INFO, ...  

Add it to your makefile like this:

将它添加到您的makefile中:

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 

#2


6  

Following is the code snippet that you should include in your native code.

下面是您应该在本机代码中包含的代码片段。

#include <android/log.h>


__android_log_write(ANDROID_LOG_ERROR, "Tag", "Error msg");//Or ANDROID_LOG_INFO, ...  

In order to use the above API, we need to link the corresponding library.

为了使用上述API,我们需要链接相应的库。

We can link a shared library in Android in 3 ways. In below 3 cases, the lines mentioned should be added in Android.mk

我们可以用三种方式连接Android上的共享库。在以下3种情况下,应在Android.mk中添加上述行

So here are the three ways.

这里有三种方法。

1. LOCAL_LDLIBS way
LOCAL_LDLIBS := -llog

For some reason if 1 doesnt work(it did not work for me), You can try below 2 ways

由于某种原因,如果1不起作用(它对我不起作用),你可以尝试以下两种方法

2. LOCAL_LDFLAGS way
LOCAL_LDFLAGS := -llog

3. LOCAL_SHARED_LIBRARIES way
LOCAL_SHARED_LIBRARIES += liblog

#3


1  

syslog

syslog

This POSIX function also outputs to logcat.

这个POSIX函数也输出到logcat。

It has the advantage of being more portable across non Android systems than __android_log_write and it automatically adds the app package to the log.

它比__android_log_write在非Android系统上具有更强的可移植性,并自动将应用程序包添加到日志中。

Tested with this example app: https://github.com/**/android-cheat/tree/a080f5c370c1f06e74a8300fb4a2e93369861047/gradle/NdkSyslog the NDK source is:

使用这个示例应用程序进行测试:https://github.com/**/android- cheat/tree/a080f5c370c370c1f06e74a84a8300fb4b42e93369861047 / ndksyslog NDK来源为:

#include <jni.h>
#include <string>
#include <syslog.h>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_**_android_1cheat_ndksyslog_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    syslog(LOG_CRIT, "hello syslog");
    return env->NewStringUTF("Check adb logcat");
}

And logcat now contains:

和logcat现在包含:

01-14 15:39:07.582  3633  3633 E com.**.android_cheat.ndksyslog: hello syslog  

Tested on Android O, HiKey 960.

在Android O上测试,HiKey 960。

#1


87  

Like this:

是这样的:

#include <android/log.h>


__android_log_write(ANDROID_LOG_ERROR, "Tag", "Error here");//Or ANDROID_LOG_INFO, ...  

Add it to your makefile like this:

将它添加到您的makefile中:

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 

#2


6  

Following is the code snippet that you should include in your native code.

下面是您应该在本机代码中包含的代码片段。

#include <android/log.h>


__android_log_write(ANDROID_LOG_ERROR, "Tag", "Error msg");//Or ANDROID_LOG_INFO, ...  

In order to use the above API, we need to link the corresponding library.

为了使用上述API,我们需要链接相应的库。

We can link a shared library in Android in 3 ways. In below 3 cases, the lines mentioned should be added in Android.mk

我们可以用三种方式连接Android上的共享库。在以下3种情况下,应在Android.mk中添加上述行

So here are the three ways.

这里有三种方法。

1. LOCAL_LDLIBS way
LOCAL_LDLIBS := -llog

For some reason if 1 doesnt work(it did not work for me), You can try below 2 ways

由于某种原因,如果1不起作用(它对我不起作用),你可以尝试以下两种方法

2. LOCAL_LDFLAGS way
LOCAL_LDFLAGS := -llog

3. LOCAL_SHARED_LIBRARIES way
LOCAL_SHARED_LIBRARIES += liblog

#3


1  

syslog

syslog

This POSIX function also outputs to logcat.

这个POSIX函数也输出到logcat。

It has the advantage of being more portable across non Android systems than __android_log_write and it automatically adds the app package to the log.

它比__android_log_write在非Android系统上具有更强的可移植性,并自动将应用程序包添加到日志中。

Tested with this example app: https://github.com/**/android-cheat/tree/a080f5c370c1f06e74a8300fb4a2e93369861047/gradle/NdkSyslog the NDK source is:

使用这个示例应用程序进行测试:https://github.com/**/android- cheat/tree/a080f5c370c370c1f06e74a84a8300fb4b42e93369861047 / ndksyslog NDK来源为:

#include <jni.h>
#include <string>
#include <syslog.h>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_**_android_1cheat_ndksyslog_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    syslog(LOG_CRIT, "hello syslog");
    return env->NewStringUTF("Check adb logcat");
}

And logcat now contains:

和logcat现在包含:

01-14 15:39:07.582  3633  3633 E com.**.android_cheat.ndksyslog: hello syslog  

Tested on Android O, HiKey 960.

在Android O上测试,HiKey 960。