使用__android_log_print()
来代替。 你必须包含头文件
示例示例。 __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 #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
现在只需要printf in c
调用LOGD("Hello world") or LOGE("Number = %d", any_int)
不要忘记包含公共头文件。
删除日志logging
如果您将LOGD(...)
定义为空,则所有日志都将消失。 只需在LOGD(...)
之后发表评论。
#define LOGD(...) // __android_log..... rest of the code
有两个选项:
1)用__android_log_printreplaceprintf。 你可以在代码开头定义:
#define printf(...) __android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__);
当然,这将需要更改所有具有printf的源代码。
2)将stdout和stderrredirect到Android logcat(不知道这是否会在非root用户的设备上工作): http : //developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd
不需要根设备,可以 ,下面就可以正常工作了。
$ adb shell $ su $ stop $ setprop log.redirect-stdio true $ start
完成了!
以上就是android开发分享如何获得用NDK应用程序编写的“printf”消息?相关内容,想了解更多android开发(异常处理)及android游戏开发关注(编程笔记)。