Monday, January 16, 2012

Log Function for Debugging Native Code in Android

Follow these steps to enable logs in the native code.

1) Include the "log.h" in the native code . Preferably in the beginning of the file.

     
          #include "android/log.h"       

2) Write the "my_log" function in the source file


void my_log(int level, const char *format,...)
{
    va_list arglist;

   va_start(arglist, format);
     __android_log_vprint(level, "file_name.cpp", format, arglist);
   va_end(arglist);

   return;
}

3) call "my_log()" from your function.
 
ex:
   int my_function()
   {
       .........
       .........
       my_log(ANDROID_LOG_DEBUG,(const char *)"my_function has been called") ;
       .........
       .........
   }


Note :
a) "android/log.h" doesn't define LOGE, LOGD, etc.. but only a few functions
like __android_log_print() that you are free to wrap around your own
logging/debugging macros. 


b) LOGD/LOGE are used internally by the Android sources (in <cutils/log.h>),
but are not exposed by the native code for fear of conflict with other logging
systems.

No comments:

Post a Comment