Putting Log in Native Code

Hi All,

This post is to put logs in the Native code  which will show in DDMS.

Following is the detail for log function for native code.

Lib Name:- liblog.so

HeaderFile:-    “android/log.h”

Function:-  int   __android_log_write(int prio, const char *tag, const char *text);

For Log Priority you can use following Value.

    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,
Tag :- It is up to you which tag you want to use for filtering in DDMS.
Text:- A simple text which you use in printf format.
Now a very small example  how we can you this Log function
1) First make some changes in Android.mk file so that you can link to liblog.so.
LOCAL_LDLIBS := -llog
2) After this I just include the header file as
#include <android/log.h>

3) Define Macros as

#define  LOG_TAG    "YS" #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
 
All is done now you can use the LOGI() macro for putting logs.
Advertisements
This entry was posted in Debugging Native Libary. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s