[Android] C/C++ logcat

来源:互联网 发布:印度红色走廊知乎 编辑:程序博客网 时间:2024/06/04 20:03

 

第一步:在对应的mk文件中加入:LOCAL_LDLIBS := -llog

 

第二步:在要使用LOG的cpp文件中加入:
#include  <android/log.h>


#define LOGV(...)    __android_log_print(ANDROID_LOG_VERBOSE, "ProjectName", __VA_ARGS__)
#define LOGD(...)   __android_log_print(ANDROID_LOG_DEBUG , "ProjectName", __VA_ARGS__)
#define LOGI(...)      __android_log_print(ANDROID_LOG_INFO  , "ProjectName", __VA_ARGS__)
#define LOGW(...)   __android_log_print(ANDROID_LOG_WARN  , "ProjectName", __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , "ProjectName", __VA_ARGS__)

比如:

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "jll", __VA_ARGS__)

 

直接调用LOGD("调试信息“),就可以在logcat中看到这条信息

========================================================================================

在实际Android开发调试过程中,我经常使用jll_alog(1, "Debugging");进行跟踪产品问题:

==================================
 Varargs Macros In Android Native
==================================

/*** JLL.S2016mmddxx: Support for Jll Android Logcat ***/
#include <android/log.h>
#include <cutils/properties.h>
#ifdef __cplusplus
extern "C" {
#endif
#define jll_alog(bit, fmt, ...)                                                           \
            do {                                                                          \
                int i4JllAlogPropVal = property_get_int32("persist.jll_alog", 0);         \
                if (i4JllAlogPropVal && (i4JllAlogPropVal & (0x1 << (bit))))              \
                    __android_log_print(ANDROID_LOG_INFO, "JLL", "%s@%d,%s | " fmt,       \
                                        __FUNCTION__, __LINE__, __FILE__, ##__VA_ARGS__); \
            } while (0)
#ifdef __cplusplus
}
#endif
/*** JLL.E2016mmddxx: Support for Jll Android Logcat ***/

 


---------------------------
Android.mk:

### JLL.S2016mmddxx: Support for Jll Android Logcat ###
LOCAL_C_INCLUDES +=/../../../../../system/core/include
LOCAL_SHARED_LIBRARIES += libcutils # Support for property_get_int32(...)
LOCAL_SHARED_LIBRARIES += liblog    # Support for __android_log_print(...)
### JLL.E2016mmddxx: Support for Jll Android Logcat ###

 

0 0