Android 如何获取用 NDK 应用程序编写的“printf”消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10274920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get "printf" messages written in NDK application?
提问by Jeegar Patel
if i am defining such function in java file
如果我在java文件中定义这样的函数
/**
* Adds two integers, returning their sum
*/
public native int add( int v1, int v2 );
so i need to code in c file
所以我需要在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);
}
then from where this printf will print it message ? In logcate i dont get it?
那么这个 printf 将从哪里打印消息?在logcate我不明白吗?
How can i debug any NDK application by putting log messages?
如何通过放置日志消息来调试任何 NDK 应用程序?
回答by Shaiful
use __android_log_print()
instead. You have to include header <android/log.h>
使用__android_log_print()
来代替。你必须包含标题<android/log.h>
Sample Example. __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n this is log messge \n");
示例。 __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n this is log messge \n");
You can also use format specifier like printf -
您还可以使用格式说明符,如 printf -
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "Need to print : %d %s",int_var, str_var);
Make sure you also link against the logging library, in your Android.mk file:
确保您还链接到 Android.mk 文件中的日志记录库:
LOCAL_LDLIBS := -llog
Ohh.. forgot .. The output will be shown in Logcat
with tag LOG_TAG
哦..忘了..输出将显示在Logcat
标签中LOG_TAG
Easy Approach
简单的方法
Add the following lines to your common header file.
将以下行添加到您的公共头文件中。
#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
Now just call LOGD("Hello world") or LOGE("Number = %d", any_int)
like printf in c
.
现在只需调用LOGD("Hello world") or LOGE("Number = %d", any_int)
像printf in c
.
Don't forget to include the common header file.
不要忘记包含公共头文件。
Remove the logging
删除日志
If you define LOGD(...)
empty, all logs will be gone. Just comment after LOGD(...)
.
如果您定义为LOGD(...)
空,则所有日志都将消失。之后才评论LOGD(...)
。
#define LOGD(...) // __android_log..... rest of the code
#define LOGD(...) // __android_log..... rest of the code
回答by Mārti?? Mo?eiko
There is two options:
有两种选择:
1) replace printf with __android_log_print. You can do this easily with define at beginning of code:
1) 用 __android_log_print 替换 printf。您可以通过在代码开头定义轻松完成此操作:
#define printf(...) __android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__);
Of course this will require changing all source code that has printf.
当然,这需要更改所有具有 printf 的源代码。
2) redirect stdout and stderr to Android logcat (not sure if this will work on non-rooted device): http://developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd
2)将stdout和stderr重定向到Android logcat(不确定这是否适用于非root设备):http: //developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd
回答by user2513099
not need to root device, acroding to http://developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd, below can work prefectly.
不需要 root 设备,根据http://developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd,下面可以完美工作。
$ adb shell
$ su
$ stop
$ setprop log.redirect-stdio true
$ start
done!
完毕!