• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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 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),
42       mach_err_(mach_err) {
43 }
44 
~MachLogMessage()45 MachLogMessage::~MachLogMessage() {
46   stream() << ": "
47            << mach_error_string(mach_err_)
48            << FormatMachErrorNumber(mach_err_);
49 }
50 
51 #if BUILDFLAG(USE_BLINK)
52 
BootstrapLogMessage(const char * file_path,int line,LogSeverity severity,kern_return_t bootstrap_err)53 BootstrapLogMessage::BootstrapLogMessage(const char* file_path,
54                                          int line,
55                                          LogSeverity severity,
56                                          kern_return_t bootstrap_err)
57     : LogMessage(file_path, line, severity),
58       bootstrap_err_(bootstrap_err) {
59 }
60 
~BootstrapLogMessage()61 BootstrapLogMessage::~BootstrapLogMessage() {
62   stream() << ": "
63            << bootstrap_strerror(bootstrap_err_);
64 
65   switch (bootstrap_err_) {
66     case BOOTSTRAP_SUCCESS:
67     case BOOTSTRAP_NOT_PRIVILEGED:
68     case BOOTSTRAP_NAME_IN_USE:
69     case BOOTSTRAP_UNKNOWN_SERVICE:
70     case BOOTSTRAP_SERVICE_ACTIVE:
71     case BOOTSTRAP_BAD_COUNT:
72     case BOOTSTRAP_NO_MEMORY:
73     case BOOTSTRAP_NO_CHILDREN: {
74       // Show known bootstrap errors in decimal because that's how they're
75       // defined in <servers/bootstrap.h>.
76       stream() << " (" << bootstrap_err_ << ")";
77       break;
78     }
79 
80     default: {
81       // bootstrap_strerror passes unknown errors to mach_error_string, so
82       // format them as they would be if they were handled by
83       // MachErrorMessage.
84       stream() << FormatMachErrorNumber(bootstrap_err_);
85       break;
86     }
87   }
88 }
89 
90 #endif  // BUILDFLAG(USE_BLINK)
91 
92 }  // namespace logging
93