Roll gyp r1967:1970.
[chromium-blink-merge.git] / base / mac / mach_logging.cc
blobc5ff85e662f8303a9f5cdf06fe5349f5fb471c98
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 <iomanip>
8 #include <string>
10 #include "base/strings/stringprintf.h"
12 #if !defined(OS_IOS)
13 #include <servers/bootstrap.h>
14 #endif // !OS_IOS
16 namespace {
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);
25 return base::StringPrintf(" (0x%08x)", mach_err);
28 } // namespace
30 namespace logging {
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) {
40 MachLogMessage::~MachLogMessage() {
41 stream() << ": "
42 << mach_error_string(mach_err_)
43 << FormatMachErrorNumber(mach_err_);
46 #if !defined(OS_IOS)
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) {
56 BootstrapLogMessage::~BootstrapLogMessage() {
57 stream() << ": "
58 << bootstrap_strerror(bootstrap_err_);
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;
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;
85 #endif // !OS_IOS
87 } // namespace logging