• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Returns a UTF8 description from an OS X Status error.
33 BASE_EXPORT std::string DescriptionFromOSStatus(OSStatus err);
34 
35 class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage {
36  public:
37   OSStatusLogMessage(const char* file_path,
38                      int line,
39                      LogSeverity severity,
40                      OSStatus status);
41   ~OSStatusLogMessage();
42 
43  private:
44   OSStatus status_;
45 
46   DISALLOW_COPY_AND_ASSIGN(OSStatusLogMessage);
47 };
48 
49 }  // namespace logging
50 
51 #if defined(NDEBUG)
52 #define MAC_DVLOG_IS_ON(verbose_level) 0
53 #else
54 #define MAC_DVLOG_IS_ON(verbose_level) VLOG_IS_ON(verbose_level)
55 #endif
56 
57 #define OSSTATUS_LOG_STREAM(severity, status) \
58     COMPACT_GOOGLE_LOG_EX_ ## severity(OSStatusLogMessage, status).stream()
59 #define OSSTATUS_VLOG_STREAM(verbose_level, status) \
60     logging::OSStatusLogMessage(__FILE__, __LINE__, \
61                                 -verbose_level, status).stream()
62 
63 #define OSSTATUS_LOG(severity, status) \
64     LAZY_STREAM(OSSTATUS_LOG_STREAM(severity, status), LOG_IS_ON(severity))
65 #define OSSTATUS_LOG_IF(severity, condition, status) \
66     LAZY_STREAM(OSSTATUS_LOG_STREAM(severity, status), \
67                 LOG_IS_ON(severity) && (condition))
68 
69 #define OSSTATUS_VLOG(verbose_level, status) \
70     LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \
71                 VLOG_IS_ON(verbose_level))
72 #define OSSTATUS_VLOG_IF(verbose_level, condition, status) \
73     LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \
74                 VLOG_IS_ON(verbose_level) && (condition))
75 
76 #define OSSTATUS_CHECK(condition, status) \
77     LAZY_STREAM(OSSTATUS_LOG_STREAM(FATAL, status), !(condition)) \
78     << "Check failed: " # condition << ". "
79 
80 #define OSSTATUS_DLOG(severity, status) \
81     LAZY_STREAM(OSSTATUS_LOG_STREAM(severity, status), DLOG_IS_ON(severity))
82 #define OSSTATUS_DLOG_IF(severity, condition, status) \
83     LAZY_STREAM(OSSTATUS_LOG_STREAM(severity, status), \
84                 DLOG_IS_ON(severity) && (condition))
85 
86 #define OSSTATUS_DVLOG(verbose_level, status) \
87     LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \
88                 MAC_DVLOG_IS_ON(verbose_level))
89 #define OSSTATUS_DVLOG_IF(verbose_level, condition, status) \
90     LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \
91                 MAC_DVLOG_IS_ON(verbose_level) && (condition))
92 
93 #define OSSTATUS_DCHECK(condition, status)        \
94   LAZY_STREAM(OSSTATUS_LOG_STREAM(FATAL, status), \
95               DCHECK_IS_ON() && !(condition))     \
96       << "Check failed: " #condition << ". "
97 
98 #endif  // BASE_MAC_MAC_LOGGING_H_
99