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 "partition_alloc/partition_alloc_base/apple/mach_logging.h"
6
7 #include <iomanip>
8 #include <string>
9
10 #include "partition_alloc/partition_alloc_base/strings/stringprintf.h"
11
12 namespace {
13
FormatMachErrorNumber(mach_error_t mach_err)14 std::string FormatMachErrorNumber(mach_error_t mach_err) {
15 // For the os/kern subsystem, give the error number in decimal as in
16 // <mach/kern_return.h>. Otherwise, give it in hexadecimal to make it easier
17 // to visualize the various bits. See <mach/error.h>.
18 if (mach_err >= 0 && mach_err < KERN_RETURN_MAX) {
19 return partition_alloc::internal::base::TruncatingStringPrintf(" (%d)",
20 mach_err);
21 }
22 return partition_alloc::internal::base::TruncatingStringPrintf(" (0x%08x)",
23 mach_err);
24 }
25
26 } // namespace
27
28 namespace partition_alloc::internal::logging {
29
MachLogMessage(const char * file_path,int line,LogSeverity severity,mach_error_t mach_err)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), mach_err_(mach_err) {}
35
~MachLogMessage()36 MachLogMessage::~MachLogMessage() {
37 stream() << ": " << mach_error_string(mach_err_)
38 << FormatMachErrorNumber(mach_err_).c_str();
39 }
40
41 } // namespace partition_alloc::internal::logging
42