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