一、要想使用ALOGD ALOGE等,需要添加头文件#include
OCAL_SHARED_LIBRARIES := \
libcutils \
liblog
二、我们这里就拿ALOGD来分析,跟踪一下源码。
1.1 、system/core/libcutils/include/cutils/log.h
1.2 、system/core/liblog/include/log/log.h
1.3 、system/core/liblog/include/log/log_main.h
1.4 、system/core/liblog/include/log/log_main.h
#define ALOG(priority, tag, ...) LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
1.5 、system/core/liblog/include/log/log_main.h
#define LOG_PRI(priority, tag, ...) android_printLog(priority, tag, __VA_ARGS__)
1.6 、system/core/liblog/include/log/log_main.h
#define android_printLog(prio, tag, ...) \
__android_log_print(prio, tag, __VA_ARGS__)
1.7 、system/core/liblog/include/android/log.h
三、使用ndk编译的时候如何使用ALOGI ALOGE呢?只能是自己去封装了。
#ifndef GIADA_DEBUG
#define GIADA_DEBUG
#include
//#include
#include
#define LOG_TAG NULL
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_MLOG_LEVEL 4
#define MLOG_ERR 0
#define MLOG_WARN 1
#define MLOG_INFO 2
#define MLOG_DEBUG 3
#define MLOG_PRINT 4
void set_debug_level(int l);
void giada_log_func(int level, const char *file, const char *function, const char *format, ...);
#define giada_log(level, format, ...) do{ \
giada_log_func(level, __FILE__, __FUNCTION__, format, ## __VA_ARGS__); } while(0)
#if 0
#define giada_debug(format,...) printf("%s, Line: %d:" format "\n", __FUNCTION__, __LINE__, ## __VA_ARGS__)
#define giada_err(format,...) printf("%s, Line: %d:" format "\n", __FUNCTION__, __LINE__, ## __VA_ARGS__)
#define giada_warn(format, ...) do{ \
giada_log(MLOG_WARN, format, ## __VA_ARGS__); }while(0)
#define DBG(str, ...) do { if(!silent) printf("[DBG]%s:%d: " str, __func__, __LINE__, __VA_ARGS__); } while(0)
#define INFO(str, ...) do { printf("[INFO]%s:%d: " str, __func__, __LINE__, __VA_ARGS__); } while (0)
#define WARN(str, ...) do { printf("[WARN]%s:%d: " str, __func__, __LINE__, __VA_ARGS__); } while (0)
#define ERR(str, ...) do { fprintf(stderr, "[ERR]%s:%d: " str, __func__, __LINE__, __VA_ARGS__); } while (0)
#define giada_info(format, ...) do{ \
giada_log(MLOG_INFO, format, ## __VA_ARGS__); }while(0)
#else
#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define giada_debug(...) ALOGD(__VA_ARGS__)
#define giada_info(...) ALOGI(__VA_ARGS__)
#define giadar_warn(...) ALOGW(__VA_ARGS__)
#define giadar_err(...) ALOGE(__VA_ARGS__)
//ALOGE("%s, Line: %d:" format "\n", __FUNCTION__, __LINE__, ## __VA_ARGS__)
#define DBG(str, ...) ALOGD(str, ## __VA_ARGS__)
#define INFO(str, ...) ALOGI(str, ## __VA_ARGS__)
#define WARN(str, ...) ALOGW(str, ## __VA_ARGS__)
#define ERR(str, ...) ALOGE(str, ## __VA_ARGS__)
#endif
#define giada_print(format, ...) do{ \
giada_log(MLOG_PRINT, format, ## __VA_ARGS__); }while(0)
#ifdef __cplusplus
}
#endif
#endif
四、参考文章
https://www.cnblogs.com/zhuangquan/p/17101681.htmlhttps://www.cnblogs.com/zhuangquan/p/17101681.html