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