absl::Status InitializeLogging()

in kmsp11/util/logging.cc [96:144]


absl::Status InitializeLogging(std::string_view output_directory,
                               std::string_view output_filename_suffix) {
  absl::WriterMutexLock lock(&logging_lock);

  if (logging_initialized) {
    return NewInternalError("logging is already initialized", SOURCE_LOCATION);
  }

  if (output_directory.empty()) {
    google::SetStderrLogging(google::GLOG_INFO);
    // Disable discrete log files for all levels.
    for (google::LogSeverity severity :
         {google::GLOG_INFO, google::GLOG_WARNING, google::GLOG_ERROR,
          google::GLOG_FATAL}) {
      google::SetLogDestination(severity, "");
      google::SetLogSymlink(severity, "");
    }
  } else {
    // FATAL logs crash the program; emit these to standard error as well.
    google::SetStderrLogging(google::GLOG_FATAL);

    if (!output_filename_suffix.empty()) {
      google::SetLogDestination(
          google::GLOG_INFO, absl::StrCat(output_directory, "/libkmsp11.log-",
                                          output_filename_suffix, "-")
                                 .c_str());
    } else {
      google::SetLogDestination(
          google::GLOG_INFO,
          absl::StrCat(output_directory, "/libkmsp11.log-").c_str());
    }

    // Disable symlink creation, which causes removal issues on FreeBSD.
    // https://www.mail-archive.com/freebsd-bugs@freebsd.org/msg79713.html
    google::SetLogSymlink(google::GLOG_INFO, "");

    // Disable discrete log files for all levels but INFO -- they all still
    // get logged to the INFO logfile.
    for (google::LogSeverity severity :
         {google::GLOG_WARNING, google::GLOG_ERROR, google::GLOG_FATAL}) {
      google::SetLogDestination(severity, "");
      google::SetLogSymlink(severity, "");
    }
  }

  google::InitGoogleLogging("libkmsp11");
  logging_initialized = true;
  return absl::OkStatus();
}