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