首先我们看下Android NDK中关于log的定义
针对宏定义
__android_log_print
int __android_log_print(
int prio,
const char *tag,
const char *fmt,
...
)
Writes a formatted string to the log, with priority prio and tag tag.
针对va_list
__android_log_vprint
int __android_log_vprint(
int prio,
const char *tag,
const char *fmt,
va_list ap
)
Equivalent to __android_log_print, but taking a va_list.
(If __android_log_print is like printf, this is like vprintf.)
实际的使用方案
针对宏定义
log.h
/* arm linux androideabi gcc */
#if defined(ANDROID) || defined(__ANDROID__)
#include
#define TAG "swack_lib"
#define LOGD(...) \
{ \
__android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__); \
}
#define LOGE(...) \
{ \
__android_log_print( \
ANDROID_LOG_ERROR, TAG, "LOGE (%s:%i) ", __func__, __LINE__); \
__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__); \
/*exit(1);*/ \
}
#endif
针对va_list
log.h
#ifndef LOG_H_
#define LOG_H_
#include
#define LOGD(...) \
{ \
test_logd(__VA_ARGS__); \
}
#define LOGE(...) \
{ \
test_loge(__func__, __LINE__, __VA_ARGS__); \
}
void test_logd(const char* format, ...);
void test_loge(const char* func,
const uint32_t line,
const char* format,
...);
#endif // LOG_H_
log.c
#include "mixo_pal.h"
#include
#define TAG "swack_lib"
void test_logd(const char* format, ...)
{
va_list arg;
va_start(arg, format);
__android_log_vprint(ANDROID_LOG_DEBUG, TAG, format, arg);
va_end(arg);
}
void test_loge(const char* func,
const uint32_t line,
const char* format,
...)
{
va_list arg;
va_start(arg, format);
__android_log_print(ANDROID_LOG_ERROR, TAG, "LOGE (%s:%i) ", func, line);
__android_log_vprint(ANDROID_LOG_ERROR, TAG, format, arg);
va_end(arg);
}