Firefly开源社区

android C/C++ native编程中logcat的使用

105

积分

0

威望

0

贡献

技术小白

积分
105
发表于 2017-7-12 10:22:33     
本帖最后由 lxp 于 2017-7-22 10:18 编辑

一.ALOG
参考:system/core/include/cutils/log.h
以ALOGV为例:
  1. #ifndef ALOGV
  2. #define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
  3. #if LOG_NDEBUG
  4. #define ALOGV(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0)
  5. #else
  6. #define ALOGV(...) __ALOGV(__VA_ARGS__)
  7. #endif
  8. #endif
复制代码
  1. /*
  2. * Basic log message macro.
  3. *
  4. * Example:
  5. * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
  6. *
  7. * The second argument may be NULL or "" to indicate the "global" tag.
  8. */
  9. #ifndef ALOG
  10. #define ALOG(priority, tag, ...) \
  11. LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
  12. #endif
复制代码
  1. /*
  2. * Log macro that allows you to specify a number for the priority.
  3. */
  4. #ifndef LOG_PRI
  5. #define LOG_PRI(priority, tag, ...) \
  6. android_printLog(priority, tag, __VA_ARGS__)
  7. #endif
复制代码
  1. #define android_printLog(prio, tag, fmt...) \
  2. __android_log_print(prio, tag, fmt)

  3. #define android_vprintLog(prio, cond, tag, fmt...) \
  4. __android_log_vprint(prio, tag, fmt)
复制代码


__android_log_print(prio, tag, fmt)在 liblog 中的 logd_write.c 中实现了.
再来看一下LOG_NDEBUG
  1. #ifndef LOG_NDEBUG
  2. #ifdef NDEBUG
  3. #define LOG_NDEBUG 1
  4. #else
  5. #define LOG_NDEBUG 0
  6. #endif
  7. #endif
复制代码


LOG_NDEBUG受到NDEBUG的影响
因此,想在logcat中利用ALOG打印log,分以下2步
1.在c,c++层添加
  1. #include <cutils/log.h>

  2. #undef NDEBUG    //取消NDEBUG的定义
复制代码

2.定义LOG_TAG

  1. /*
  2.   * This is the local tag used for the following simplified
  3.   * logging macros.  You can change this preprocessor definition
  4.   * before using the other macros to change the tag.
  5.   */
  6. #ifndef LOG_TAG
  7. #define LOG_TAG NULL
  8. #endif

复制代码
在文件中定义TAG
#define LOG_TAG "ProjectName"
这样就可以在logcat中查看Tag为ProjectName的日志

3.在需要调用Log的地方执行:ALOGV,ALOGD,ALOGI,ALOGW,ALOGE。

二.直接调用__android_log_print
1.在Android.mk文件中加入LOCAL_LDLIBS := -llog 或 LOCAL_SHARED_LIBRARIES := \ libutils \ liblog \ libcutils
2.在CPP文件中加入
  1. #include <android/log.h>

  2. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "ProjectName", __VA_ARGS__);
复制代码

ANDROID_LOG_DEBUG是LOG的等级中的debug
其他等级分别是
Verbose,Debug,Info,Warn,Error

"ProjectName"是Log中的Tag

__VA_ARGS__是要打印的内容

3.直接在代码中调用LOGD("XXXXX"),就可以在logcat中查看调试信息
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表