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