Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / base / mac / mach_logging.cc
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 <servers/bootstrap.h>
8
9 #include <iomanip>
10 #include <string>
11
12 #include "base/strings/stringprintf.h"
13
14 namespace {
15
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);
22   }
23   return base::StringPrintf(" (0x%08x)", mach_err);
24 }
25
26 }  // namespace
27
28 namespace logging {
29
30 MachLogMessage::MachLogMessage(const char* file_path,
31                                int line,
32                                LogSeverity severity,
33                                mach_error_t mach_err)
34     : LogMessage(file_path, line, severity),
35       mach_err_(mach_err) {
36 }
37
38 MachLogMessage::~MachLogMessage() {
39   stream() << ": "
40            << mach_error_string(mach_err_)
41            << FormatMachErrorNumber(mach_err_);
42 }
43
44 BootstrapLogMessage::BootstrapLogMessage(const char* file_path,
45                                          int line,
46                                          LogSeverity severity,
47                                          kern_return_t bootstrap_err)
48     : LogMessage(file_path, line, severity),
49       bootstrap_err_(bootstrap_err) {
50 }
51
52 BootstrapLogMessage::~BootstrapLogMessage() {
53   stream() << ": "
54            << bootstrap_strerror(bootstrap_err_);
55
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_ << ")";
68       break;
69     }
70
71     default: {
72       // bootstrap_strerror passes unknown errors to mach_error_string, so
73       // format them as they would be if they were handled by
74       // MachErrorMessage.
75       stream() << FormatMachErrorNumber(bootstrap_err_);
76       break;
77     }
78   }
79 }
80
81 }  // namespace logging