mach_logging.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2014 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. #ifndef BASE_MAC_MACH_LOGGING_H_
  5. #define BASE_MAC_MACH_LOGGING_H_
  6. #include <mach/mach.h>
  7. #include "base/base_export.h"
  8. #include "base/logging.h"
  9. #include "base/macros.h"
  10. #include "build/build_config.h"
  11. // Use the MACH_LOG family of macros along with a mach_error_t (kern_return_t)
  12. // containing a Mach error. The error value will be decoded so that logged
  13. // messages explain the error.
  14. //
  15. // Use the BOOTSTRAP_LOG family of macros specifically for errors that occur
  16. // while interoperating with the bootstrap subsystem. These errors will first
  17. // be looked up as bootstrap error messages. If no match is found, they will
  18. // be treated as generic Mach errors, as in MACH_LOG.
  19. //
  20. // Examples:
  21. //
  22. // kern_return_t kr = mach_timebase_info(&info);
  23. // if (kr != KERN_SUCCESS) {
  24. // MACH_LOG(ERROR, kr) << "mach_timebase_info";
  25. // }
  26. //
  27. // kr = vm_deallocate(task, address, size);
  28. // MACH_DCHECK(kr == KERN_SUCCESS, kr) << "vm_deallocate";
  29. namespace logging {
  30. class BASE_EXPORT MachLogMessage : public logging::LogMessage {
  31. public:
  32. MachLogMessage(const char* file_path,
  33. int line,
  34. LogSeverity severity,
  35. mach_error_t mach_err);
  36. ~MachLogMessage() override;
  37. private:
  38. mach_error_t mach_err_;
  39. DISALLOW_COPY_AND_ASSIGN(MachLogMessage);
  40. };
  41. } // namespace logging
  42. #if defined(NDEBUG)
  43. #define MACH_DVLOG_IS_ON(verbose_level) 0
  44. #else
  45. #define MACH_DVLOG_IS_ON(verbose_level) VLOG_IS_ON(verbose_level)
  46. #endif
  47. #define MACH_LOG_STREAM(severity, mach_err) \
  48. COMPACT_GOOGLE_LOG_EX_ ## severity(MachLogMessage, mach_err).stream()
  49. #define MACH_VLOG_STREAM(verbose_level, mach_err) \
  50. logging::MachLogMessage(__FILE__, __LINE__, \
  51. -verbose_level, mach_err).stream()
  52. #define MACH_LOG(severity, mach_err) \
  53. LAZY_STREAM(MACH_LOG_STREAM(severity, mach_err), LOG_IS_ON(severity))
  54. #define MACH_LOG_IF(severity, condition, mach_err) \
  55. LAZY_STREAM(MACH_LOG_STREAM(severity, mach_err), \
  56. LOG_IS_ON(severity) && (condition))
  57. #define MACH_VLOG(verbose_level, mach_err) \
  58. LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \
  59. VLOG_IS_ON(verbose_level))
  60. #define MACH_VLOG_IF(verbose_level, condition, mach_err) \
  61. LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \
  62. VLOG_IS_ON(verbose_level) && (condition))
  63. #define MACH_CHECK(condition, mach_err) \
  64. LAZY_STREAM(MACH_LOG_STREAM(FATAL, mach_err), !(condition)) \
  65. << "Check failed: " # condition << ". "
  66. #define MACH_DLOG(severity, mach_err) \
  67. LAZY_STREAM(MACH_LOG_STREAM(severity, mach_err), DLOG_IS_ON(severity))
  68. #define MACH_DLOG_IF(severity, condition, mach_err) \
  69. LAZY_STREAM(MACH_LOG_STREAM(severity, mach_err), \
  70. DLOG_IS_ON(severity) && (condition))
  71. #define MACH_DVLOG(verbose_level, mach_err) \
  72. LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \
  73. MACH_DVLOG_IS_ON(verbose_level))
  74. #define MACH_DVLOG_IF(verbose_level, condition, mach_err) \
  75. LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \
  76. MACH_DVLOG_IS_ON(verbose_level) && (condition))
  77. #define MACH_DCHECK(condition, mach_err) \
  78. LAZY_STREAM(MACH_LOG_STREAM(FATAL, mach_err), \
  79. DCHECK_IS_ON() && !(condition)) \
  80. << "Check failed: " #condition << ". "
  81. #if !defined(OS_IOS)
  82. namespace logging {
  83. class BASE_EXPORT BootstrapLogMessage : public logging::LogMessage {
  84. public:
  85. BootstrapLogMessage(const char* file_path,
  86. int line,
  87. LogSeverity severity,
  88. kern_return_t bootstrap_err);
  89. ~BootstrapLogMessage() override;
  90. private:
  91. kern_return_t bootstrap_err_;
  92. DISALLOW_COPY_AND_ASSIGN(BootstrapLogMessage);
  93. };
  94. } // namespace logging
  95. #define BOOTSTRAP_DVLOG_IS_ON MACH_DVLOG_IS_ON
  96. #define BOOTSTRAP_LOG_STREAM(severity, bootstrap_err) \
  97. COMPACT_GOOGLE_LOG_EX_ ## severity(BootstrapLogMessage, \
  98. bootstrap_err).stream()
  99. #define BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err) \
  100. logging::BootstrapLogMessage(__FILE__, __LINE__, \
  101. -verbose_level, bootstrap_err).stream()
  102. #define BOOTSTRAP_LOG(severity, bootstrap_err) \
  103. LAZY_STREAM(BOOTSTRAP_LOG_STREAM(severity, \
  104. bootstrap_err), LOG_IS_ON(severity))
  105. #define BOOTSTRAP_LOG_IF(severity, condition, bootstrap_err) \
  106. LAZY_STREAM(BOOTSTRAP_LOG_STREAM(severity, bootstrap_err), \
  107. LOG_IS_ON(severity) && (condition))
  108. #define BOOTSTRAP_VLOG(verbose_level, bootstrap_err) \
  109. LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \
  110. VLOG_IS_ON(verbose_level))
  111. #define BOOTSTRAP_VLOG_IF(verbose_level, condition, bootstrap_err) \
  112. LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \
  113. VLOG_IS_ON(verbose_level) && (condition))
  114. #define BOOTSTRAP_CHECK(condition, bootstrap_err) \
  115. LAZY_STREAM(BOOTSTRAP_LOG_STREAM(FATAL, bootstrap_err), !(condition)) \
  116. << "Check failed: " # condition << ". "
  117. #define BOOTSTRAP_DLOG(severity, bootstrap_err) \
  118. LAZY_STREAM(BOOTSTRAP_LOG_STREAM(severity, bootstrap_err), \
  119. DLOG_IS_ON(severity))
  120. #define BOOTSTRAP_DLOG_IF(severity, condition, bootstrap_err) \
  121. LAZY_STREAM(BOOTSTRAP_LOG_STREAM(severity, bootstrap_err), \
  122. DLOG_IS_ON(severity) && (condition))
  123. #define BOOTSTRAP_DVLOG(verbose_level, bootstrap_err) \
  124. LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \
  125. BOOTSTRAP_DVLOG_IS_ON(verbose_level))
  126. #define BOOTSTRAP_DVLOG_IF(verbose_level, condition, bootstrap_err) \
  127. LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \
  128. BOOTSTRAP_DVLOG_IS_ON(verbose_level) && (condition))
  129. #define BOOTSTRAP_DCHECK(condition, bootstrap_err) \
  130. LAZY_STREAM(BOOTSTRAP_LOG_STREAM(FATAL, bootstrap_err), \
  131. DCHECK_IS_ON() && !(condition)) \
  132. << "Check failed: " #condition << ". "
  133. #endif // !OS_IOS
  134. #endif // BASE_MAC_MACH_LOGGING_H_