mach_logging.cc 2.5 KB

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