android系列-init 初始化日志

1.FirstStageMain

//android10\system\core\init\first_stage_init.cpp

int FirstStageMain(int argc, char** argv) {
    
    // Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually
    // talk to the outside world...
    InitKernelLogging(argv);//初始化日志
}

2.InitKernelLogging 

//android10\system\core\init\util.cpp

void InitKernelLogging(char** argv) {
    SetFatalRebootTarget();
    android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
}

3.KernelLogger 

//android10\system\core\base\logging.cpp

void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
                  const char* tag, const char*, unsigned int, const char* msg) {
    static int klog_fd = OpenKmsg();
}

4.OpenKmsg 

//android10\system\core\base\logging.cpp

#if defined(__linux__)
static int OpenKmsg() {
#if defined(__ANDROID__)
  // pick up 'file w /dev/kmsg' environment from daemon's init rc file
  //在 Android 中用于获取名为 "ANDROID_FILE__dev_kmsg" 的环境变量的值的函数
  const auto val = getenv("ANDROID_FILE__dev_kmsg");
  if (val != nullptr) {
    int fd;
    if (android::base::ParseInt(val, &fd, 0)) {
      auto flags = fcntl(fd, F_GETFL);
      if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd;
    }
  }
#endif

  //打开/dev/kmsg文件
  return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
}
#endif

你可能感兴趣的:(Android,android)