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.
5 #include "base/mac/mach_logging.h"
7 #include <servers/bootstrap.h>
12 #include "base/strings/stringprintf.h"
16 std::string FormatMachErrorNumber(mach_error_t mach_err) {
17 // For the os/kern subsystem, give the error number in decimal as in
18 // <mach/kern_return.h>. Otherwise, give it in hexadecimal to make it easier
19 // to visualize the various bits. See <mach/error.h>.
20 if (mach_err >= 0 && mach_err < KERN_RETURN_MAX) {
21 return base::StringPrintf(" (%d)", mach_err);
23 return base::StringPrintf(" (0x%08x)", mach_err);
30 MachLogMessage::MachLogMessage(const char* file_path,
33 mach_error_t mach_err)
34 : LogMessage(file_path, line, severity),
38 MachLogMessage::~MachLogMessage() {
40 << mach_error_string(mach_err_)
41 << FormatMachErrorNumber(mach_err_);
44 BootstrapLogMessage::BootstrapLogMessage(const char* file_path,
47 kern_return_t bootstrap_err)
48 : LogMessage(file_path, line, severity),
49 bootstrap_err_(bootstrap_err) {
52 BootstrapLogMessage::~BootstrapLogMessage() {
54 << bootstrap_strerror(bootstrap_err_);
56 switch (bootstrap_err_) {
57 case BOOTSTRAP_SUCCESS:
58 case BOOTSTRAP_NOT_PRIVILEGED:
59 case BOOTSTRAP_NAME_IN_USE:
60 case BOOTSTRAP_UNKNOWN_SERVICE:
61 case BOOTSTRAP_SERVICE_ACTIVE:
62 case BOOTSTRAP_BAD_COUNT:
63 case BOOTSTRAP_NO_MEMORY:
64 case BOOTSTRAP_NO_CHILDREN: {
65 // Show known bootstrap errors in decimal because that's how they're
66 // defined in <servers/bootstrap.h>.
67 stream() << " (" << bootstrap_err_ << ")";
72 // bootstrap_strerror passes unknown errors to mach_error_string, so
73 // format them as they would be if they were handled by
75 stream() << FormatMachErrorNumber(bootstrap_err_);
81 } // namespace logging