1 // Copyright 2014 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 PARTITION_ALLOC_PARTITION_ALLOC_BASE_APPLE_MACH_LOGGING_H_
6 #define PARTITION_ALLOC_PARTITION_ALLOC_BASE_APPLE_MACH_LOGGING_H_
7
8 #include <mach/mach.h>
9
10 #include "build/build_config.h"
11 #include "partition_alloc/partition_alloc_base/component_export.h"
12 #include "partition_alloc/partition_alloc_base/debug/debugging_buildflags.h"
13 #include "partition_alloc/partition_alloc_base/logging.h"
14
15 // Use the PA_MACH_LOG family of macros along with a mach_error_t
16 // (kern_return_t) containing a Mach error. The error value will be decoded so
17 // that logged messages explain the error.
18 //
19 // Examples:
20 //
21 // kern_return_t kr = mach_timebase_info(&info);
22 // if (kr != KERN_SUCCESS) {
23 // PA_MACH_LOG(ERROR, kr) << "mach_timebase_info";
24 // }
25 //
26 // kr = vm_deallocate(task, address, size);
27 // PA_MACH_DCHECK(kr == KERN_SUCCESS, kr) << "vm_deallocate";
28
29 namespace partition_alloc::internal::logging {
30
PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)31 class PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) MachLogMessage
32 : public partition_alloc::internal::logging::LogMessage {
33 public:
34 MachLogMessage(const char* file_path,
35 int line,
36 LogSeverity severity,
37 mach_error_t mach_err);
38
39 MachLogMessage(const MachLogMessage&) = delete;
40 MachLogMessage& operator=(const MachLogMessage&) = delete;
41
42 ~MachLogMessage() override;
43
44 private:
45 mach_error_t mach_err_;
46 };
47
48 } // namespace partition_alloc::internal::logging
49
50 #if BUILDFLAG(PA_DCHECK_IS_ON)
51 #define PA_MACH_DVLOG_IS_ON(verbose_level) PA_VLOG_IS_ON(verbose_level)
52 #else
53 #define PA_MACH_DVLOG_IS_ON(verbose_level) 0
54 #endif
55
56 #define PA_MACH_LOG_STREAM(severity, mach_err) \
57 PA_COMPACT_GOOGLE_PLOG_EX_##severity(MachLogMessage, mach_err).stream()
58 #define PA_MACH_VLOG_STREAM(verbose_level, mach_err) \
59 ::partition_alloc::internal::logging::MachLogMessage( \
60 __FILE__, __LINE__, -verbose_level, mach_err) \
61 .stream()
62
63 #define PA_MACH_LOG(severity, mach_err) \
64 PA_LAZY_STREAM(PA_MACH_LOG_STREAM(severity, mach_err), PA_LOG_IS_ON(severity))
65 #define PA_MACH_LOG_IF(severity, condition, mach_err) \
66 PA_LAZY_STREAM(PA_MACH_LOG_STREAM(severity, mach_err), \
67 PA_LOG_IS_ON(severity) && (condition))
68
69 #define PA_MACH_VLOG(verbose_level, mach_err) \
70 PA_LAZY_STREAM(PA_MACH_VLOG_STREAM(verbose_level, mach_err), \
71 PA_VLOG_IS_ON(verbose_level))
72 #define PA_MACH_VLOG_IF(verbose_level, condition, mach_err) \
73 PA_LAZY_STREAM(PA_MACH_VLOG_STREAM(verbose_level, mach_err), \
74 PA_VLOG_IS_ON(verbose_level) && (condition))
75
76 #define PA_MACH_CHECK(condition, mach_err) \
77 PA_LAZY_STREAM(PA_MACH_LOG_STREAM(FATAL, mach_err), !(condition)) \
78 << "Check failed: " #condition << ". "
79
80 #define PA_MACH_DLOG(severity, mach_err) \
81 PA_LAZY_STREAM(PA_MACH_LOG_STREAM(severity, mach_err), \
82 PA_DLOG_IS_ON(severity))
83 #define PA_MACH_DLOG_IF(severity, condition, mach_err) \
84 PA_LAZY_STREAM(PA_MACH_LOG_STREAM(severity, mach_err), \
85 PA_DLOG_IS_ON(severity) && (condition))
86
87 #define PA_MACH_DVLOG(verbose_level, mach_err) \
88 PA_LAZY_STREAM(PA_MACH_VLOG_STREAM(verbose_level, mach_err), \
89 PA_MACH_DVLOG_IS_ON(verbose_level))
90 #define PA_MACH_DVLOG_IF(verbose_level, condition, mach_err) \
91 PA_LAZY_STREAM(PA_MACH_VLOG_STREAM(verbose_level, mach_err), \
92 PA_MACH_DVLOG_IS_ON(verbose_level) && (condition))
93
94 #define PA_MACH_DCHECK(condition, mach_err) \
95 PA_LAZY_STREAM(PA_MACH_LOG_STREAM(FATAL, mach_err), \
96 BUILDFLAG(PA_DCHECK_IS_ON) && !(condition)) \
97 << "Check failed: " #condition << ". "
98
99 #endif // PARTITION_ALLOC_PARTITION_ALLOC_BASE_APPLE_MACH_LOGGING_H_
100