1 // Copyright 2012 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_MAC_LOGGING_H_ 6 #define BASE_MAC_MAC_LOGGING_H_ 7 8 #include <string> 9 10 #include "base/base_export.h" 11 #include "base/logging.h" 12 #include "build/build_config.h" 13 14 #if BUILDFLAG(IS_IOS) 15 #include <MacTypes.h> 16 #else 17 #include <libkern/OSTypes.h> 18 #endif 19 20 // Use the OSSTATUS_LOG family to log messages related to errors in Mac OS X 21 // system routines that report status via an OSStatus or OSErr value. It is 22 // similar to the PLOG family which operates on errno, but because there is no 23 // global (or thread-local) OSStatus or OSErr value, the specific error must 24 // be supplied as an argument to the OSSTATUS_LOG macro. The message logged 25 // will contain the symbolic constant name corresponding to the status value, 26 // along with the value itself. 27 // 28 // OSErr is just an older 16-bit form of the newer 32-bit OSStatus. Despite 29 // the name, OSSTATUS_LOG can be used equally well for OSStatus and OSErr. 30 31 namespace logging { 32 33 // Returns a UTF8 description from an OS X Status error. 34 BASE_EXPORT std::string DescriptionFromOSStatus(OSStatus err); 35 36 class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage { 37 public: 38 OSStatusLogMessage(const char* file_path, 39 int line, 40 LogSeverity severity, 41 OSStatus status); 42 43 OSStatusLogMessage(const OSStatusLogMessage&) = delete; 44 OSStatusLogMessage& operator=(const OSStatusLogMessage&) = delete; 45 46 ~OSStatusLogMessage() override; 47 48 private: 49 OSStatus status_; 50 }; 51 52 } // namespace logging 53 54 #if DCHECK_IS_ON() 55 #define MAC_DVLOG_IS_ON(verbose_level) VLOG_IS_ON(verbose_level) 56 #else 57 #define MAC_DVLOG_IS_ON(verbose_level) 0 58 #endif 59 60 #define OSSTATUS_LOG_STREAM(severity, status) \ 61 COMPACT_GOOGLE_LOG_EX_ ## severity(OSStatusLogMessage, status).stream() 62 #define OSSTATUS_VLOG_STREAM(verbose_level, status) \ 63 logging::OSStatusLogMessage(__FILE__, __LINE__, \ 64 -verbose_level, status).stream() 65 66 #define OSSTATUS_LOG(severity, status) \ 67 LAZY_STREAM(OSSTATUS_LOG_STREAM(severity, status), LOG_IS_ON(severity)) 68 #define OSSTATUS_LOG_IF(severity, condition, status) \ 69 LAZY_STREAM(OSSTATUS_LOG_STREAM(severity, status), \ 70 LOG_IS_ON(severity) && (condition)) 71 72 #define OSSTATUS_VLOG(verbose_level, status) \ 73 LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \ 74 VLOG_IS_ON(verbose_level)) 75 #define OSSTATUS_VLOG_IF(verbose_level, condition, status) \ 76 LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \ 77 VLOG_IS_ON(verbose_level) && (condition)) 78 79 #define OSSTATUS_CHECK(condition, status) \ 80 LAZY_STREAM(OSSTATUS_LOG_STREAM(FATAL, status), !(condition)) \ 81 << "Check failed: " # condition << ". " 82 83 #define OSSTATUS_DLOG(severity, status) \ 84 LAZY_STREAM(OSSTATUS_LOG_STREAM(severity, status), DLOG_IS_ON(severity)) 85 #define OSSTATUS_DLOG_IF(severity, condition, status) \ 86 LAZY_STREAM(OSSTATUS_LOG_STREAM(severity, status), \ 87 DLOG_IS_ON(severity) && (condition)) 88 89 #define OSSTATUS_DVLOG(verbose_level, status) \ 90 LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \ 91 MAC_DVLOG_IS_ON(verbose_level)) 92 #define OSSTATUS_DVLOG_IF(verbose_level, condition, status) \ 93 LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \ 94 MAC_DVLOG_IS_ON(verbose_level) && (condition)) 95 96 #define OSSTATUS_DCHECK(condition, status) \ 97 LAZY_STREAM(OSSTATUS_LOG_STREAM(FATAL, status), \ 98 DCHECK_IS_ON() && !(condition)) \ 99 << "Check failed: " #condition << ". " 100 101 #endif // BASE_MAC_MAC_LOGGING_H_ 102