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