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 #include "base/fuchsia/fuchsia_logging.h"
6
7 #include <zircon/status.h>
8
9 #include <iomanip>
10 #include <string_view>
11
12 #include "base/location.h"
13 #include "base/process/process.h"
14 #include "base/strings/stringprintf.h"
15
16 namespace logging {
17
ZxLogMessage(const char * file_path,int line,LogSeverity severity,zx_status_t zx_status)18 ZxLogMessage::ZxLogMessage(const char* file_path,
19 int line,
20 LogSeverity severity,
21 zx_status_t zx_status)
22 : LogMessage(file_path, line, severity), zx_status_(zx_status) {}
23
~ZxLogMessage()24 ZxLogMessage::~ZxLogMessage() {
25 AppendError();
26 }
27
AppendError()28 void ZxLogMessage::AppendError() {
29 // zx_status_t error values are negative, so log the numeric version as
30 // decimal rather than hex. This is also useful to match zircon/errors.h for
31 // grepping.
32 stream() << ": " << zx_status_get_string(zx_status_) << " (" << zx_status_
33 << ")";
34 }
35
~ZxLogMessageFatal()36 ZxLogMessageFatal::~ZxLogMessageFatal() {
37 AppendError();
38 Flush();
39 base::ImmediateCrash();
40 }
41
42 } // namespace logging
43
44 namespace base {
45
46 namespace internal {
47
FidlConnectionErrorMessage(std::string_view protocol_name,std::string_view status_string)48 std::string FidlConnectionErrorMessage(std::string_view protocol_name,
49 std::string_view status_string) {
50 return base::StringPrintf("Failed to connect to %s: %s", protocol_name.data(),
51 status_string.data());
52 }
53
FidlMethodResultErrorMessage(std::string_view formatted_error,std::string_view method_name)54 std::string FidlMethodResultErrorMessage(std::string_view formatted_error,
55 std::string_view method_name) {
56 return base::StringPrintf("Error calling %s: %s", method_name.data(),
57 formatted_error.data());
58 }
59
60 } // namespace internal
61
LogFidlErrorAndExitProcess(const Location & from_here,std::string_view protocol_name)62 fit::function<void(zx_status_t)> LogFidlErrorAndExitProcess(
63 const Location& from_here,
64 std::string_view protocol_name) {
65 return [from_here, protocol_name](zx_status_t status) {
66 {
67 logging::ZxLogMessage(from_here.file_name(), from_here.line_number(),
68 logging::LOGGING_ERROR, status)
69 .stream()
70 << protocol_name << " disconnected unexpectedly, exiting";
71 }
72 base::Process::TerminateCurrentProcessImmediately(1);
73 };
74 }
75
FidlMethodResultErrorMessage(const fit::result<fidl::OneWayError> & result,std::string_view method_name)76 std::string FidlMethodResultErrorMessage(
77 const fit::result<fidl::OneWayError>& result,
78 std::string_view method_name) {
79 CHECK(result.is_error());
80 return internal::FidlMethodResultErrorMessage(
81 result.error_value().FormatDescription(), method_name);
82 }
83
FidlBindingClosureWarningLogger(std::string_view protocol_name)84 fit::function<void(fidl::UnbindInfo)> FidlBindingClosureWarningLogger(
85 std::string_view protocol_name) {
86 return [protocol_name](fidl::UnbindInfo info) {
87 ZX_LOG(WARNING, info.status()) << protocol_name << " unbound";
88 };
89 }
90
91 } // namespace base
92