Wednesday, 18 June 2014

solutions for control android NDK logs


#define LOGD2(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG1, __VA_ARGS__))

this is the commonly used method to simple the log function. but we cannot control the log globally.(actually we can control it by the log level.)

2. by this solution I can stop all logs globally. because I believe that even control by log level, it still cause some system resource.

void  xxxxx(){

//#define XXXX 0 //by commenting this all logs will gone.
#ifdef XXXX
#define LOG_TAG1 "jni header"
#define LOGD2(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG1, __VA_ARGS__))
#define LOGD2(...) (xxxxx())


"__VA_ARGS__" worth to mention, it is a parameter in #define only for deal with ... parameters.

However, to use this solution, you need copy above code into all your c++ files.

3. by this function, I only need include it and use this function directly.

inline int log(int prio, const char *tag,  const char *fmt, ...){
if(DEBUG_FLAG == 1){
va_list args;
va_start(args, fmt);
__android_log_vprint(prio, tag, fmt,args);

"va_list args;" is a special variable for ... parameters in a function, and vprint is the correspondent function for it.

No comments:

Post a comment