diff options
author | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-09 13:34:44 +0000 |
---|---|---|
committer | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-09 13:34:44 +0000 |
commit | 7e1de9858cfe4dc01fb7881097258d458918b3d1 (patch) | |
tree | a630042f388c96bd9e89e8e6e02cd666e3677f23 | |
parent | cd636729b2ffc613f90fa22c605d15d1fe2ba70e (diff) | |
download | chromium_src-7e1de9858cfe4dc01fb7881097258d458918b3d1.zip chromium_src-7e1de9858cfe4dc01fb7881097258d458918b3d1.tar.gz chromium_src-7e1de9858cfe4dc01fb7881097258d458918b3d1.tar.bz2 |
Add mach_logging to Chrome.
R=rsesek@chromium.org
Review URL: https://codereview.chromium.org/272603003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269263 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/base.gypi | 2 | ||||
-rw-r--r-- | base/mac/mac_logging.h | 4 | ||||
-rw-r--r-- | base/mac/mach_logging.cc | 81 | ||||
-rw-r--r-- | base/mac/mach_logging.h | 149 |
4 files changed, 234 insertions, 2 deletions
diff --git a/base/base.gypi b/base/base.gypi index fbe46b7..3c244ad 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -274,6 +274,8 @@ 'mac/mac_logging.cc', 'mac/mac_util.h', 'mac/mac_util.mm', + 'mac/mach_logging.cc', + 'mac/mach_logging.h', 'mac/objc_property_releaser.h', 'mac/objc_property_releaser.mm', 'mac/os_crash_dumps.cc', diff --git a/base/mac/mac_logging.h b/base/mac/mac_logging.h index ecd9b6b..02f205d 100644 --- a/base/mac/mac_logging.h +++ b/base/mac/mac_logging.h @@ -73,10 +73,10 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage { DLOG_IS_ON(severity) && (condition)) #define OSSTATUS_DVLOG(verbose_level, status) \ - LAZY_STREAM(OSSTATUS_VPLOG_STREAM(verbose_level, status), \ + LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \ DVLOG_IS_ON(verbose_level)) #define OSSTATUS_DVLOG_IF(verbose_level, condition, status) \ - LAZY_STREAM(OSSTATUS_VPLOG_STREAM(verbose_level, status) \ + LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status) \ DVLOG_IS_ON(verbose_level) && (condition)) #define OSSTATUS_DCHECK(condition, status) \ diff --git a/base/mac/mach_logging.cc b/base/mac/mach_logging.cc new file mode 100644 index 0000000..0ab6416 --- /dev/null +++ b/base/mac/mach_logging.cc @@ -0,0 +1,81 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/mac/mach_logging.h" + +#include <servers/bootstrap.h> + +#include <iomanip> +#include <string> + +#include "base/strings/stringprintf.h" + +namespace { + +std::string FormatMachErrorNumber(mach_error_t mach_err) { + // For the os/kern subsystem, give the error number in decimal as in + // <mach/kern_return.h>. Otherwise, give it in hexadecimal to make it easier + // to visualize the various bits. See <mach/error.h>. + if (mach_err >= 0 && mach_err < KERN_RETURN_MAX) { + return base::StringPrintf(" (%d)", mach_err); + } + return base::StringPrintf(" (0x%08x)", mach_err); +} + +} // namespace + +namespace logging { + +MachLogMessage::MachLogMessage(const char* file_path, + int line, + LogSeverity severity, + mach_error_t mach_err) + : LogMessage(file_path, line, severity), + mach_err_(mach_err) { +} + +MachLogMessage::~MachLogMessage() { + stream() << ": " + << mach_error_string(mach_err_) + << FormatMachErrorNumber(mach_err_); +} + +BootstrapLogMessage::BootstrapLogMessage(const char* file_path, + int line, + LogSeverity severity, + kern_return_t bootstrap_err) + : LogMessage(file_path, line, severity), + bootstrap_err_(bootstrap_err) { +} + +BootstrapLogMessage::~BootstrapLogMessage() { + stream() << ": " + << bootstrap_strerror(bootstrap_err_); + + switch (bootstrap_err_) { + case BOOTSTRAP_SUCCESS: + case BOOTSTRAP_NOT_PRIVILEGED: + case BOOTSTRAP_NAME_IN_USE: + case BOOTSTRAP_UNKNOWN_SERVICE: + case BOOTSTRAP_SERVICE_ACTIVE: + case BOOTSTRAP_BAD_COUNT: + case BOOTSTRAP_NO_MEMORY: + case BOOTSTRAP_NO_CHILDREN: { + // Show known bootstrap errors in decimal because that's how they're + // defined in <servers/bootstrap.h>. + stream() << " (" << bootstrap_err_ << ")"; + break; + } + + default: { + // bootstrap_strerror passes unknown errors to mach_error_string, so + // format them as they would be if they were handled by + // MachErrorMessage. + stream() << FormatMachErrorNumber(bootstrap_err_); + break; + } + } +} + +} // namespace logging diff --git a/base/mac/mach_logging.h b/base/mac/mach_logging.h new file mode 100644 index 0000000..589dde2 --- /dev/null +++ b/base/mac/mach_logging.h @@ -0,0 +1,149 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_MAC_MACH_LOGGING_H_ +#define BASE_MAC_MACH_LOGGING_H_ + +#include <mach/mach.h> + +#include "base/basictypes.h" +#include "base/logging.h" + +// Use the MACH_LOG family of macros along with a mach_error_t (kern_return_t) +// containing a Mach error. The error value will be decoded so that logged +// messages explain the error. +// +// Use the BOOTSTRAP_LOG family of macros specifically for errors that occur +// while interoperating with the bootstrap subsystem. These errors will first +// be looked up as bootstrap error messages. If no match is found, they will +// be treated as generic Mach errors, as in MACH_LOG. +// +// Examples: +// +// kern_return_t kr = mach_timebase_info(&info); +// if (kr != KERN_SUCCESS) { +// MACH_LOG(ERROR, kr) << "mach_timebase_info"; +// } +// +// kr = mach_vm_deallocate(task, address, size); +// MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_vm_deallocate"; + +namespace logging { + +class MachLogMessage : public logging::LogMessage { + public: + MachLogMessage(const char* file_path, + int line, + LogSeverity severity, + mach_error_t mach_err); + ~MachLogMessage(); + + private: + mach_error_t mach_err_; + + DISALLOW_COPY_AND_ASSIGN(MachLogMessage); +}; + +class BootstrapLogMessage : public logging::LogMessage { + public: + BootstrapLogMessage(const char* file_path, + int line, + LogSeverity severity, + kern_return_t bootstrap_err); + ~BootstrapLogMessage(); + + private: + kern_return_t bootstrap_err_; + + DISALLOW_COPY_AND_ASSIGN(BootstrapLogMessage); +}; + +} // namespace logging + +#define MACH_LOG_STREAM(severity, mach_err) \ + COMPACT_GOOGLE_LOG_EX_ ## severity(MachLogMessage, mach_err).stream() +#define MACH_VLOG_STREAM(verbose_level, mach_err) \ + logging::MachLogMessage(__FILE__, __LINE__, \ + -verbose_level, mach_err).stream() + +#define MACH_LOG(severity, mach_err) \ + LAZY_STREAM(MACH_LOG_STREAM(severity, mach_err), LOG_IS_ON(severity)) +#define MACH_LOG_IF(severity, condition, mach_err) \ + LAZY_STREAM(MACH_LOG_STREAM(severity, mach_err), \ + LOG_IS_ON(severity) && (condition)) + +#define MACH_VLOG(verbose_level, mach_err) \ + LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \ + VLOG_IS_ON(verbose_level)) +#define MACH_VLOG_IF(verbose_level, condition, mach_err) \ + LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \ + VLOG_IS_ON(verbose_level) && (condition)) + +#define MACH_CHECK(condition, mach_err) \ + LAZY_STREAM(MACH_LOG_STREAM(FATAL, mach_err), !(condition)) \ + << "Check failed: " # condition << ". " + +#define MACH_DLOG(severity, mach_err) \ + LAZY_STREAM(MACH_LOG_STREAM(severity, mach_err), DLOG_IS_ON(severity)) +#define MACH_DLOG_IF(severity, condition, mach_err) \ + LAZY_STREAM(MACH_LOG_STREAM(severity, mach_err), \ + DLOG_IS_ON(severity) && (condition)) + +#define MACH_DVLOG(verbose_level, mach_err) \ + LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \ + DVLOG_IS_ON(verbose_level)) +#define MACH_DVLOG_IF(verbose_level, condition, mach_err) \ + LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err) \ + DVLOG_IS_ON(verbose_level) && (condition)) + +#define MACH_DCHECK(condition, mach_err) \ + LAZY_STREAM(MACH_LOG_STREAM(FATAL, mach_err), \ + DCHECK_IS_ON && !(condition)) \ + << "Check failed: " # condition << ". " + +#define BOOTSTRAP_LOG_STREAM(severity, bootstrap_err) \ + COMPACT_GOOGLE_LOG_EX_ ## severity(BootstrapLogMessage, \ + bootstrap_err).stream() +#define BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err) \ + logging::BootstrapLogMessage(__FILE__, __LINE__, \ + -verbose_level, bootstrap_err).stream() + +#define BOOTSTRAP_LOG(severity, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_LOG_STREAM(severity, \ + bootstrap_err), LOG_IS_ON(severity)) +#define BOOTSTRAP_LOG_IF(severity, condition, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_LOG_STREAM(severity, bootstrap_err), \ + LOG_IS_ON(severity) && (condition)) + +#define BOOTSTRAP_VLOG(verbose_level, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \ + VLOG_IS_ON(verbose_level)) +#define BOOTSTRAP_VLOG_IF(verbose_level, condition, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \ + VLOG_IS_ON(verbose_level) && (condition)) + +#define BOOTSTRAP_CHECK(condition, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_LOG_STREAM(FATAL, bootstrap_err), !(condition)) \ + << "Check failed: " # condition << ". " + +#define BOOTSTRAP_DLOG(severity, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_LOG_STREAM(severity, bootstrap_err), \ + DLOG_IS_ON(severity)) +#define BOOTSTRAP_DLOG_IF(severity, condition, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_LOG_STREAM(severity, bootstrap_err), \ + DLOG_IS_ON(severity) && (condition)) + +#define BOOTSTRAP_DVLOG(verbose_level, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \ + DVLOG_IS_ON(verbose_level)) +#define BOOTSTRAP_DVLOG_IF(verbose_level, condition, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err) \ + DVLOG_IS_ON(verbose_level) && (condition)) + +#define BOOTSTRAP_DCHECK(condition, bootstrap_err) \ + LAZY_STREAM(BOOTSTRAP_LOG_STREAM(FATAL, bootstrap_err), \ + DCHECK_IS_ON && !(condition)) \ + << "Check failed: " # condition << ". " + +#endif // BASE_MAC_MACH_LOGGING_H_ |