Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print/log address of a variable in NDK

I'm not very strong in C, but I'm working with the NDK right now and I need help logging address of a variable in hex. I've been using __android_log_print to print generic log messages, but how do I tell C to convert address of a variable to a char array?

like image 868
Phonon Avatar asked Aug 11 '11 18:08

Phonon


2 Answers

I never worked with android NDK, but I'm assuming __android_log_print works with printf format characters. In that case you may use %p.

Let's say we have a variable int a = 10;. To print its address:

printf("%p\n", &a); //This will print in hexadecimal

EDIT:

Accepted answer:

__android_log_print(SOME_PRIO, "sometag", "%p", &a);
like image 56
Fred Avatar answered Oct 05 '22 22:10

Fred


I work with Android NDK many times and I have tried before use native logcat.

__android_log_print(6, "deneme", "text"); -> this is error message
typedef enum android_LogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT,    /* only for SetMinPriority() */
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT,     /* only for SetMinPriority(); must be last */
} android_LogPriority;

and than include header in c source files

#include <jni.h>
#include <android/log.h>

than makefile look like

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := deneme-jni
LOCAL_SRC_FILES := deneme.c
LOCAL_LDLIBS    := -llog  

include $(BUILD_SHARED_LIBRARY)
like image 28
nurisezgin Avatar answered Oct 05 '22 22:10

nurisezgin