• 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 
11 #include "base/location.h"
12 #include "base/process/process.h"
13 #include "base/strings/string_piece.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   // zx_status_t error values are negative, so log the numeric version as
26   // decimal rather than hex. This is also useful to match zircon/errors.h for
27   // grepping.
28   stream() << ": " << zx_status_get_string(zx_status_) << " (" << zx_status_
29            << ")";
30 }
31 
32 }  // namespace logging
33 
34 namespace base {
35 
36 namespace internal {
37 
FidlConnectionErrorMessage(const base::StringPiece & protocol_name,const base::StringPiece & status_string)38 std::string FidlConnectionErrorMessage(const base::StringPiece& protocol_name,
39                                        const base::StringPiece& status_string) {
40   return base::StringPrintf("Failed to connect to %s: %s", protocol_name.data(),
41                             status_string.data());
42 }
43 
FidlMethodResultErrorMessage(const base::StringPiece & formatted_error,const base::StringPiece & method_name)44 std::string FidlMethodResultErrorMessage(
45     const base::StringPiece& formatted_error,
46     const base::StringPiece& method_name) {
47   return base::StringPrintf("Error calling %s: %s", method_name.data(),
48                             formatted_error.data());
49 }
50 
51 }  // namespace internal
52 
LogFidlErrorAndExitProcess(const Location & from_here,StringPiece protocol_name)53 fit::function<void(zx_status_t)> LogFidlErrorAndExitProcess(
54     const Location& from_here,
55     StringPiece protocol_name) {
56   return [from_here, protocol_name](zx_status_t status) {
57     {
58       logging::ZxLogMessage(from_here.file_name(), from_here.line_number(),
59                             logging::LOGGING_ERROR, status)
60               .stream()
61           << protocol_name << " disconnected unexpectedly, exiting";
62     }
63     base::Process::TerminateCurrentProcessImmediately(1);
64   };
65 }
66 
FidlMethodResultErrorMessage(const fit::result<fidl::OneWayError> & result,const base::StringPiece & method_name)67 std::string FidlMethodResultErrorMessage(
68     const fit::result<fidl::OneWayError>& result,
69     const base::StringPiece& method_name) {
70   CHECK(result.is_error());
71   return internal::FidlMethodResultErrorMessage(
72       result.error_value().FormatDescription(), method_name);
73 }
74 
FidlBindingClosureWarningLogger(base::StringPiece protocol_name)75 fit::function<void(fidl::UnbindInfo)> FidlBindingClosureWarningLogger(
76     base::StringPiece protocol_name) {
77   return [protocol_name](fidl::UnbindInfo info) {
78     ZX_LOG(WARNING, info.status()) << protocol_name << " unbound";
79   };
80 }
81 
82 }  // namespace base
83