gpt4 book ai didi

android - 如何获取 NDK 应用程序中写入的 "printf"消息?

转载 作者:IT王子 更新时间:2023-10-29 00:00:51 25 4
gpt4 key购买 nike

如果我在 java 文件中定义这样的函数

  /** 
* Adds two integers, returning their sum
*/
public native int add( int v1, int v2 );

所以我需要在c文件中编码

JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
(JNIEnv * env, jobject obj, jint value1, jint value2) {

printf("\n this is log messge \n");

return (value1 + value2);
}

那么这个 printf 将从哪里打印它的消息?在logcate中我不明白吗?

如何通过放置日志消息来调试任何 NDK 应用程序?

最佳答案

使用 __android_log_print()反而。您必须包含标题 <android/log.h>

示例。 __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n this is log messge \n");

你也可以使用像 printf 这样的格式说明符 -

__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "Need to print : %d %s",int_var, str_var);

确保您还在 Android.mk 文件中链接到日志库:

  LOCAL_LDLIBS := -llog

哦..忘记了..输出将显示在Logcat中带标签 LOG_TAG

简单的方法

将以下行添加到您的通用头文件中。

#include <android/log.h>

#define LOG_TAG "your-log-tag"

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
// If you want you can add other log definition for info, warning etc

现在只需调用 LOGD("Hello world") or LOGE("Number = %d", any_int)喜欢 printf in c .

不要忘记包含通用头文件。

删除日志

如果您定义 LOGD(...)为空,所有日志都将消失。在 LOGD(...) 之后发表评论.

#define LOGD(...) // __android_log..... rest of the code

关于android - 如何获取 NDK 应用程序中写入的 "printf"消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10274920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com