logging_chromeos.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // Define _GNU_SOURCE to ensure that <errno.h> defines
  5. // program_invocation_short_name which is used in GetProgramName(). Keep this at
  6. // the top of the file since some system headers might include <errno.h> and the
  7. // header could be skipped on subsequent includes.
  8. #if !defined(_GNU_SOURCE)
  9. #define _GNU_SOURCE
  10. #endif
  11. #include "base/logging.h"
  12. #include <errno.h>
  13. #include <iomanip>
  14. #include "base/process/process_handle.h"
  15. #include "base/strings/string_piece.h"
  16. #include "base/threading/platform_thread.h"
  17. namespace logging {
  18. namespace {
  19. const char* GetProgramName() {
  20. return program_invocation_short_name ? program_invocation_short_name : "";
  21. }
  22. } // namespace
  23. // InitWithSyslogPrefix generates log header for Chrome OS in syslog compatible
  24. // format. The timezone will always be UTC regardless of devices' timezone.
  25. // `<rfc3339_timestamp> <tickcount> <log_level>`
  26. // `<program_name>[<pid>:<thread_id>]: `
  27. // `[<filename>(<line_number>)] <message>`
  28. //
  29. // e.g.
  30. // 2020-06-27T23:55:25.094701Z 1234 VERBOSE1 chrome[3816:3877]:
  31. // [drm_device_handle.cc(90)] Succeeded authenticating /dev/dri/card0 in 0 ms
  32. // with 1 attempt(s)
  33. void LogMessage::InitWithSyslogPrefix(base::StringPiece filename,
  34. int line,
  35. uint64_t tick_count,
  36. const char* log_severity_name_c_str,
  37. const char* log_prefix,
  38. bool enable_process_id,
  39. bool enable_thread_id,
  40. bool enable_timestamp,
  41. bool enable_tickcount) {
  42. if (log_prefix)
  43. stream_ << log_prefix << ':';
  44. if (enable_timestamp) {
  45. timeval tv{};
  46. gettimeofday(&tv, nullptr);
  47. time_t t = tv.tv_sec;
  48. struct tm utc_time {};
  49. gmtime_r(&t, &utc_time);
  50. stream_ << std::setfill('0') // Set fill to 0
  51. << std::setw(4) << 1900 + utc_time.tm_year << "-" // year
  52. << std::setw(2) << 1 + utc_time.tm_mon << "-" // month
  53. << std::setw(2) << utc_time.tm_mday // date
  54. << 'T' << std::setw(2) << utc_time.tm_hour << ":" // hour
  55. << std::setw(2) << utc_time.tm_min << ":" // minute
  56. << std::setw(2) << utc_time.tm_sec << "." // second
  57. << std::setw(6) << tv.tv_usec // millisecond
  58. << "Z "; // timezone UTC
  59. }
  60. if (enable_tickcount)
  61. stream_ << tick_count << ' ';
  62. if (severity_ >= 0) {
  63. stream_ << log_severity_name_c_str;
  64. } else {
  65. stream_ << "VERBOSE" << -severity_;
  66. }
  67. stream_ << ' ' << GetProgramName();
  68. if (enable_process_id || enable_thread_id) {
  69. stream_ << "[";
  70. if (enable_process_id) {
  71. stream_ << base::GetUniqueIdForProcess();
  72. }
  73. if (enable_thread_id) {
  74. stream_ << ':' << base::PlatformThread::CurrentId();
  75. }
  76. stream_ << "]";
  77. }
  78. stream_ << ": ";
  79. stream_ << "[" << filename << "(" << line << ")] ";
  80. }
  81. } // namespace logging