Cómo obtener mensajes "printf" escritos en la aplicación NDK?
Si estoy definiendo dicha función en el archivo java
/**
* Adds two integers, returning their sum
*/
public native int add( int v1, int v2 );
Así que necesito codificar en el archivo 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);
}
Entonces, ¿desde dónde imprimirá este mensaje printf? En logcate no lo entiendo?
¿Cómo puedo depurar cualquier aplicación NDK poniendo mensajes de registro?
3 answers
Use __android_log_print()
en su lugar. Tienes que incluir header <android/log.h>
Ejemplo de Ejemplo. __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n this is log messge \n");
También puede usar especificador de formato como printf -
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "Need to print : %d %s",int_var, str_var);
Asegúrese también de enlazar con la biblioteca de registro, en su Android.mk archivo:
LOCAL_LDLIBS := -llog
Ohh.. olvidar .. La salida se mostrará en Logcat
con etiqueta LOG_TAG
Fácil aproximación
Agregue las siguientes líneas a su archivo de encabezado común.
#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
Ahora solo llama LOGD("Hello world") or LOGE("Number = %d", any_int)
like printf in c
.
No olvide incluir el archivo de cabecera común.
Eliminar el registro
Si define LOGD(...)
vacío, todos los registros desaparecerán. Solo comente después de LOGD(...)
.
#define LOGD(...) // __android_log..... rest of the code
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-03 09:54:52
Hay dos opciones:
1) reemplaza printf con _ _ android _ log _ print. Puede hacer esto fácilmente con definir al principio del código:
#define printf(...) __android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__);
Por supuesto, esto requerirá cambiar todo el código fuente que tenga printf.
2) redirigir stdout y stderr a Android logcat (no estoy seguro de si esto funcionará en dispositivos no arraigados): http://developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-23 06:12:58
No es necesario rootear el dispositivo, acroding a http://developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd , a continuación puede funcionar perfectamente.
$ adb shell
$ su
$ stop
$ setprop log.redirect-stdio true
$ start
Hecho!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-20 03:00:52