如何获得" printf的"写NDK应用程序的消息?如何获得、应用程序、消息、printf

2023-09-04 23:49:36 作者:″_触动心扉

如果我在Java文件中定义这样的功能

if i am defining such function in java file

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

所以我需要code在C文件

so i need to code in c file

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我不明白吗?

then from where this printf will print it message ? In logcate i dont get it?

我如何可以通过将日志消息调试任何NDK的应用程序?

How can i debug any NDK application by putting log messages?

推荐答案

使用 __ android_log_print()代替。你必须包含头文件<安卓/ log.h>

use __android_log_print() instead. You have to include header <android/log.h>

样品实施例。 __ android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,\ n这个是日志的messge \ N);

您也可以使用格式说明如printf -

You can also use format specifier like printf -

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

请确保您还链接对日志库,在你的Andr​​oid.mk文件:

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

  LOCAL_LDLIBS := -llog

哦......忘了..输出会显示在的logcat 用标签来 LOG_TAG

简易方法

添加以下行到你的公共头文件。

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

现在只需拨打 LOGD(世界,你好)或LOGE(数=%D,ANY_INT)的printf在C

不要忘记,包括公共头文件。

Don't forget to include the common header file.

删除记录

如果你定义 LOGD(...)空,所有日志将不复存在。在 LOGD只是评论(...)

If you define LOGD(...) empty, all logs will be gone. Just comment after LOGD(...).

的#define LOGD(...)// __android_log .....的code休息