mac_logging.h 3.6 KB

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