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