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 #include "base/apple/mach_logging.h"
6
7 #include <iomanip>
8 #include <string>
9
10 #include "base/strings/stringprintf.h"
11 #include "build/build_config.h"
12
13 #if BUILDFLAG(USE_BLINK)
14 #if BUILDFLAG(IS_IOS)
15 #include "base/ios/sim_header_shims.h"
16 #else
17 #include <servers/bootstrap.h>
18 #endif // BUILDFLAG(IS_IOS)
19 #endif // BUILDFLAG(USE_BLINK)
20
21 namespace {
22
FormatMachErrorNumber(mach_error_t mach_err)23 std::string FormatMachErrorNumber(mach_error_t mach_err) {
24 // For the os/kern subsystem, give the error number in decimal as in
25 // <mach/kern_return.h>. Otherwise, give it in hexadecimal to make it easier
26 // to visualize the various bits. See <mach/error.h>.
27 if (mach_err >= 0 && mach_err < KERN_RETURN_MAX) {
28 return base::StringPrintf(" (%d)", mach_err);
29 }
30 return base::StringPrintf(" (0x%08x)", mach_err);
31 }
32
33 } // namespace
34
35 namespace logging {
36
MachLogMessage(const char * file_path,int line,LogSeverity severity,mach_error_t mach_err)37 MachLogMessage::MachLogMessage(const char* file_path,
38 int line,
39 LogSeverity severity,
40 mach_error_t mach_err)
41 : LogMessage(file_path, line, severity), mach_err_(mach_err) {}
42
~MachLogMessage()43 MachLogMessage::~MachLogMessage() {
44 stream() << ": " << mach_error_string(mach_err_)
45 << FormatMachErrorNumber(mach_err_);
46 }
47
48 #if BUILDFLAG(USE_BLINK)
49
BootstrapLogMessage(const char * file_path,int line,LogSeverity severity,kern_return_t bootstrap_err)50 BootstrapLogMessage::BootstrapLogMessage(const char* file_path,
51 int line,
52 LogSeverity severity,
53 kern_return_t bootstrap_err)
54 : LogMessage(file_path, line, severity), bootstrap_err_(bootstrap_err) {}
55
~BootstrapLogMessage()56 BootstrapLogMessage::~BootstrapLogMessage() {
57 stream() << ": " << bootstrap_strerror(bootstrap_err_);
58
59 switch (bootstrap_err_) {
60 case BOOTSTRAP_SUCCESS:
61 case BOOTSTRAP_NOT_PRIVILEGED:
62 case BOOTSTRAP_NAME_IN_USE:
63 case BOOTSTRAP_UNKNOWN_SERVICE:
64 case BOOTSTRAP_SERVICE_ACTIVE:
65 case BOOTSTRAP_BAD_COUNT:
66 case BOOTSTRAP_NO_MEMORY:
67 case BOOTSTRAP_NO_CHILDREN: {
68 // Show known bootstrap errors in decimal because that's how they're
69 // defined in <servers/bootstrap.h>.
70 stream() << " (" << bootstrap_err_ << ")";
71 break;
72 }
73
74 default: {
75 // bootstrap_strerror passes unknown errors to mach_error_string, so
76 // format them as they would be if they were handled by
77 // MachErrorMessage.
78 stream() << FormatMachErrorNumber(bootstrap_err_);
79 break;
80 }
81 }
82 }
83
84 #endif // BUILDFLAG(USE_BLINK)
85
86 } // namespace logging
87