1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_FUCHSIA_FUCHSIA_LOGGING_H_
#define BASE_FUCHSIA_FUCHSIA_LOGGING_H_
#include <lib/fidl/cpp/wire/connect_service.h>
#include <lib/fidl/cpp/wire/traits.h>
#include <lib/fit/function.h>
#include <lib/zx/result.h>
#include <zircon/types.h>
#include <string>
#include <string_view>
#include "base/base_export.h"
#include "base/check.h"
#include "base/logging.h"
// Use the ZX_LOG family of macros along with a zx_status_t containing a Zircon
// error. The error value will be decoded so that logged messages explain the
// error.
namespace logging {
class BASE_EXPORT ZxLogMessage : public logging::LogMessage {
public:
ZxLogMessage(const char* file_path,
int line,
LogSeverity severity,
zx_status_t zx_status);
ZxLogMessage(const ZxLogMessage&) = delete;
ZxLogMessage& operator=(const ZxLogMessage&) = delete;
~ZxLogMessage() override;
protected:
void AppendError();
private:
zx_status_t zx_status_;
};
class BASE_EXPORT ZxLogMessageFatal final : public ZxLogMessage {
public:
using ZxLogMessage::ZxLogMessage;
[[noreturn]] ~ZxLogMessageFatal() override;
};
} // namespace logging
#define ZX_LOG_STREAM(severity, zx_status) \
COMPACT_GOOGLE_LOG_EX_##severity(ZxLogMessage, zx_status).stream()
#define ZX_LOG(severity, zx_status) \
LAZY_STREAM(ZX_LOG_STREAM(severity, zx_status), LOG_IS_ON(severity))
#define ZX_LOG_IF(severity, condition, zx_status) \
LAZY_STREAM(ZX_LOG_STREAM(severity, zx_status), \
LOG_IS_ON(severity) && (condition))
#define ZX_CHECK(condition, zx_status) \
LAZY_STREAM(ZX_LOG_STREAM(FATAL, zx_status), !(condition)) \
<< "Check failed: " #condition << ". "
#define ZX_DLOG(severity, zx_status) \
LAZY_STREAM(ZX_LOG_STREAM(severity, zx_status), DLOG_IS_ON(severity))
#if DCHECK_IS_ON()
#define ZX_DLOG_IF(severity, condition, zx_status) \
LAZY_STREAM(ZX_LOG_STREAM(severity, zx_status), \
DLOG_IS_ON(severity) && (condition))
#else // DCHECK_IS_ON()
#define ZX_DLOG_IF(severity, condition, zx_status) EAT_STREAM_PARAMETERS
#endif // DCHECK_IS_ON()
#define ZX_DCHECK(condition, zx_status) \
LAZY_STREAM(ZX_LOG_STREAM(DFATAL, zx_status), \
DCHECK_IS_ON() && !(condition)) \
<< "Check failed: " #condition << ". "
namespace base {
namespace internal {
BASE_EXPORT std::string FidlMethodResultErrorMessage(
std::string_view formatted_error,
std::string_view method_name);
BASE_EXPORT std::string FidlConnectionErrorMessage(
std::string_view protocol_name,
std::string_view status_string);
} // namespace internal
class Location;
// Returns a function suitable for use as error-handler for a FIDL binding or
// helper (e.g. ScenicSession) required by the process to function. Typically
// it is unhelpful to simply crash on such failures, so the returned handler
// will instead log an ERROR and exit the process.
// The Location and protocol name string must be kept valid by the caller, for
// as long as the returned fit::function<> remains live.
BASE_EXPORT fit::function<void(zx_status_t)> LogFidlErrorAndExitProcess(
const Location& from_here,
std::string_view protocol_name);
template <typename Protocol>
BASE_EXPORT std::string FidlConnectionErrorMessage(
const zx::result<fidl::ClientEnd<Protocol>>& result) {
CHECK(result.is_error());
return internal::FidlConnectionErrorMessage(
fidl::DiscoverableProtocolName<Protocol>, result.status_string());
}
template <typename FidlMethod>
BASE_EXPORT std::string FidlMethodResultErrorMessage(
const fidl::Result<FidlMethod>& result,
std::string_view method_name) {
CHECK(result.is_error());
return internal::FidlMethodResultErrorMessage(
result.error_value().FormatDescription(), method_name);
}
BASE_EXPORT std::string FidlMethodResultErrorMessage(
const fit::result<fidl::OneWayError>& result,
std::string_view method_name);
BASE_EXPORT fit::function<void(fidl::UnbindInfo)>
FidlBindingClosureWarningLogger(std::string_view protocol_name);
template <typename Protocol>
BASE_EXPORT fit::function<void(fidl::UnbindInfo)>
FidlBindingClosureWarningLogger() {
return FidlBindingClosureWarningLogger(
fidl::DiscoverableProtocolName<Protocol>);
}
} // namespace base
#endif // BASE_FUCHSIA_FUCHSIA_LOGGING_H_
|