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