当前位置:   article > 正文

Android liblog

android liblog

Androi系统提供了一套完整的API供其他程序调用输出log,这套API分为Java 层和 native 层,不过两个API最终都是通过file system将log写入kernel 层的logger device.

ALOGX 系列

以native层为例,如果我们要开发’.cpp’或’.c’程序,那么可以call下列API之以写出不同level的log

  1. #define LOG_TAG "HeloWorld"
  2. ALOGV("hello world,level verbose");
  3. ALOGD("hello world,level debug");
  4. ALOGI("hello world,level info");
  5. ALOGE("hello world,level error");
  6. ALOGW("hello world,level warning");

这里通常都需要定义一个LOG_TAG, 作为一个完整log的一部分,可以唯一的定位一个module. ALOGX()系列API的实现通过宏定位到共同的一组函数.

  1. #ifndef ALOGE
  2. #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
  3. #endif
  4. #ifndef ALOG
  5. #define ALOG(priority, tag, ...) \
  6. LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
  7. #endif
  8. #ifndef LOG_PRI
  9. #define LOG_PRI(priority, tag, ...) \
  10. android_printLog(priority, tag, __VA_ARGS__)
  11. #endif
  12. #define android_printLog(prio, tag, fmt...) \
  13. __android_log_print(prio, tag, fmt)
  14. int __android_log_print(int prio, const char *tag, const char *fmt, ...)
  15. {
  16. va_list ap;
  17. char buf[LOG_BUF_SIZE];
  18. va_start(ap, fmt);
  19. vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
  20. va_end(ap);
  21. return __android_log_write(prio, tag, buf);
  22. }

__android_log_print()通过va_list变量把format形式字符串生成最终的字符串,然后调用__android_log_write(),这里的参数tag就是之前定义的 LOG_TAG. 而prio是一个整数值,中logcat讲到过,最后通过logcat抓出来后,会将整形log level转换为字符型.

  1. int __android_log_write(int prio, const char *tag, const char *msg)
  2. {
  3. struct iovec vec[3];
  4. log_id_t log_id = LOG_ID_MAIN;
  5. if (!tag)
  6. tag = "";
  7. /* XXX: This needs to go! */
  8. if (!strcmp(tag, "HTC_RIL") ||
  9. !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
  10. !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
  11. !strcmp(tag, "AT") ||
  12. !strcmp(tag, "GSM") ||
  13. !strcmp(tag, "STK") ||
  14. !strcmp(tag, "CDMA") ||
  15. !strcmp(tag, "PHONE") ||
  16. !strcmp(tag, "SMS"))
  17. log_id = LOG_ID_RADIO;
  18. vec[0].iov_base = (unsigned char *) &prio;
  19. vec[0].iov_len = 1;
  20. vec[1].iov_base = (void *) tag;
  21. vec[1].iov_len = strlen(tag) + 1;
  22. vec[2].iov_base = (void *) msg;
  23. vec[2].iov_len = strlen(msg) + 1;
  24. return write_to_log(log_id, vec, 3);
  25. }

Android log 系统目前有四种类型的log:main,system,radio,events. 后三种一般都是系统的一些特殊的log,除此之外,自己开发的程序,log都默认写到main中. 所以程序最开始把 log_id 设为 LOG_ID_MAIN. 不过程序接下来会判断tag参数,如果tag符合radio log的规则的话,则将log_id改为 LOG_ID_RADIO. 接着把传入的三个参数放到一个iovec变量中. 并调用write_to_log()

  1. struct iovec {
  2. const void* iov_base;
  3. size_t iov_len;
  4. };
  5. static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;

write_to_log()是一个指针函数,这里的实现用了一点小伎俩. 最开始这个指针就被赋值为__write_to_log_init, 所以,在第一次调用该函数的时候,调用的就是 __write_to_log_init()

  1. static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
  2. {
  3. #ifdef HAVE_PTHREADS
  4. pthread_mutex_lock(&log_init_lock);
  5. #endif
  6. if (write_to_log == __write_to_log_init) {
  7. log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
  8. log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
  9. log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
  10. log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
  11. write_to_log = __write_to_log_kernel;
  12. if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
  13. log_fds[LOG_ID_EVENTS] < 0) {
  14. log_close(log_fds[LOG_ID_MAIN]);
  15. log_close(log_fds[LOG_ID_RADIO]);
  16. log_close(log_fds[LOG_ID_EVENTS]);
  17. log_fds[LOG_ID_MAIN] = -1;
  18. log_fds[LOG_ID_RADIO] = -1;
  19. log_fds[LOG_ID_EVENTS] = -1;
  20. write_to_log = __write_to_log_null;
  21. }
  22. if (log_fds[LOG_ID_SYSTEM] < 0) {
  23. log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
  24. }
  25. }
  26. #ifdef HAVE_PTHREADS
  27. pthread_mutex_unlock(&log_init_lock);
  28. #endif
  29. return write_to_log(log_id, vec, nr);
  30. }

之所以要这样做,是因为在系统开启后第一次写通过ALOGX函数写log的时候,kernel 层的logger device还未被打开,所以要将这些device都打开,然后,将write_to_log改成__write_to_log_kernel. 在函数的最后,接着再调用一次write_to_log(),这次调用的就是__write_log_log_kernel 了.

  1. static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
  2. {
  3. ssize_t ret;
  4. int log_fd;
  5. if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
  6. log_fd = log_fds[(int)log_id];
  7. } else {
  8. return EBADF;
  9. }
  10. do {
  11. ret = log_writev(log_fd, vec, nr);
  12. } while (ret < 0 && errno == EINTR);
  13. return ret;
  14. }

函数将log_id转为log_fd后,就直接调用 log_writev()函数

#define log_writev(filedes, vector, count) writev(filedes, vector, count)

log_writev()就被映射到具体的driver层的writev()函数.这样,一条log就被写入到了kernel层的device中.

SLOGX

SLOGX()API族用于生成system log,log被写到system这个logger device中,SLOGX的实现跟main log基本相同,只是默认的log id是system而不是main

  1. #define SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
  2. int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
  3. {
  4. va_list ap;
  5. char buf[LOG_BUF_SIZE];
  6. va_start(ap, fmt);
  7. vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
  8. va_end(ap);
  9. return __android_log_buf_write(bufID, prio, tag, buf);
  10. }
  11. int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
  12. {
  13. struct iovec vec[3];
  14. if (!tag)
  15. tag = "";
  16. /* XXX: This needs to go! */
  17. if (!strcmp(tag, "HTC_RIL") ||
  18. !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
  19. !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
  20. !strcmp(tag, "AT") ||
  21. !strcmp(tag, "GSM") ||
  22. !strcmp(tag, "STK") ||
  23. !strcmp(tag, "CDMA") ||
  24. !strcmp(tag, "PHONE") ||
  25. !strcmp(tag, "SMS"))
  26. bufID = LOG_ID_RADIO;
  27. vec[0].iov_base = (unsigned char *) &prio;
  28. vec[0].iov_len = 1;
  29. vec[1].iov_base = (void *) tag;
  30. vec[1].iov_len = strlen(tag) + 1;
  31. vec[2].iov_base = (void *) msg;
  32. vec[2].iov_len = strlen(msg) + 1;
  33. return write_to_log(bufID, vec, 3);
  34. }
Events Log

Events log是一种特殊的log,经常被用来记录系统的一些参数:例如电池的当前状态,剩余电量……等等

Java层写event log的api为 writeEvent(),不过系统有四种该函数的实现.

  1. public static native int writeEvent(int tag, int value);
  2. public static native int writeEvent(int tag, long value);
  3. public static native int writeEvent(int tag, String str);
  4. public static native int writeEvent(int tag, Object... list);

系统会根据模块中call api时传入的参数对应调用不同的writeEvent()函数.这些函数分别对应到不同的JNI实现.首先看下参数为int/long时的JNI实现.

  1. static jint android_util_EventLog_writeEvent_Integer(JNIEnv* env, jobject clazz,
  2. jint tag, jint value)
  3. {
  4. return android_btWriteLog(tag, EVENT_TYPE_INT, &value, sizeof(value));
  5. }
  6. static jint android_util_EventLog_writeEvent_Long(JNIEnv* env, jobject clazz,
  7. jint tag, jlong value)
  8. {
  9. return android_btWriteLog(tag, EVENT_TYPE_LONG, &value, sizeof(value));
  10. }

这两个函数调用了一个共同的函数 android_btWriteLog()

  1. #define android_btWriteLog(tag, type, payload, len) __android_log_btwrite(tag, type, payload, len)
  2. int __android_log_btwrite(int32_t tag, char type, const void *payload,
  3. size_t len)
  4. {
  5. struct iovec vec[3];
  6. vec[0].iov_base = &tag;
  7. vec[0].iov_len = sizeof(tag);
  8. vec[1].iov_base = &type;
  9. vec[1].iov_len = sizeof(type);
  10. vec[2].iov_base = (void*)payload;
  11. vec[2].iov_len = len;
  12. return write_to_log(LOG_ID_EVENTS, vec, 3);
  13. }

是不是很熟悉?没错,最后跟main log流程一样,都调用write_to_log(0函数. 下面是参数为string时的JNI实现:

  1. static jint android_util_EventLog_writeEvent_String(JNIEnv* env, jobject clazz,
  2. jint tag, jstring value) {
  3. uint8_t buf[MAX_EVENT_PAYLOAD];
  4. const char *str = value != NULL ? env->GetStringUTFChars(value, NULL) : "NULL";
  5. jint len = strlen(str);
  6. const int max = sizeof(buf) - sizeof(len) - 2; // Type byte, final newline
  7. if (len > max) len = max;
  8. buf[0] = EVENT_TYPE_STRING;
  9. memcpy(&buf[1], &len, sizeof(len));
  10. memcpy(&buf[1 + sizeof(len)], str, len);
  11. buf[1 + sizeof(len) + len] = '\n';
  12. if (value != NULL) env->ReleaseStringUTFChars(value, str);
  13. return android_bWriteLog(tag, buf, 2 + sizeof(len) + len);
  14. }

该函数把type,string长度,string都放到了同一个buffer中,然后call android_bWriteLog()

  1. int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
  2. {
  3. struct iovec vec[2];
  4. vec[0].iov_base = &tag;
  5. vec[0].iov_len = sizeof(tag);
  6. vec[1].iov_base = (void*)payload;
  7. vec[1].iov_len = len;
  8. return write_to_log(LOG_ID_EVENTS, vec, 2);
  9. }

该函数与__android_log_bwrite的不同是后者是把type(int/long)跟payload分开的,而该函数放到了一起.

writeEvent的第四种形式:写入的是int/long/string的组合体,则会循环遍历该组合,转换成格式化字符串放到同一个buffer中.

  1. static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz,
  2. jint tag, jobjectArray value) {
  3. uint8_t buf[MAX_EVENT_PAYLOAD];
  4. const size_t max = sizeof(buf) - 1; // leave room for final newline
  5. size_t pos = 2; // Save room for type tag & array count
  6. jsize copied = 0, num = env->GetArrayLength(value);
  7. for (; copied < num && copied < 255; ++copied) {
  8. jobject item = env->GetObjectArrayElement(value, copied);
  9. if (item == NULL || env->IsInstanceOf(item, gStringClass)) {
  10. if (pos + 1 + sizeof(jint) > max) break;
  11. const char *str = item != NULL ? env->GetStringUTFChars((jstring) item, NULL) : "NULL";
  12. jint len = strlen(str);
  13. if (pos + 1 + sizeof(len) + len > max) len = max - pos - 1 - sizeof(len);
  14. buf[pos++] = EVENT_TYPE_STRING;
  15. memcpy(&buf[pos], &len, sizeof(len));
  16. memcpy(&buf[pos + sizeof(len)], str, len);
  17. pos += sizeof(len) + len;
  18. if (item != NULL) env->ReleaseStringUTFChars((jstring) item, str);
  19. } else if (env->IsInstanceOf(item, gIntegerClass)) {
  20. jint intVal = env->GetIntField(item, gIntegerValueID);
  21. if (pos + 1 + sizeof(intVal) > max) break;
  22. buf[pos++] = EVENT_TYPE_INT;
  23. memcpy(&buf[pos], &intVal, sizeof(intVal));
  24. pos += sizeof(intVal);
  25. } else if (env->IsInstanceOf(item, gLongClass)) {
  26. jlong longVal = env->GetLongField(item, gLongValueID);
  27. if (pos + 1 + sizeof(longVal) > max) break;
  28. buf[pos++] = EVENT_TYPE_LONG;
  29. memcpy(&buf[pos], &longVal, sizeof(longVal));
  30. pos += sizeof(longVal);
  31. } else {
  32. jniThrowException(env,
  33. "java/lang/IllegalArgumentException",
  34. "Invalid payload item type");
  35. return -1;
  36. }
  37. env->DeleteLocalRef(item);
  38. }
  39. buf[0] = EVENT_TYPE_LIST;
  40. buf[1] = copied;
  41. buf[pos++] = '\n';
  42. return android_bWriteLog(tag, buf, pos);
  43. }

最后同样是调用函数 android_bWriteLog()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/684735
推荐阅读
相关标签
  

闽ICP备14008679号