• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INCLUDE_PERFETTO_BASE_STATUS_H_
18 #define INCLUDE_PERFETTO_BASE_STATUS_H_
19 
20 #include <optional>
21 #include <string>
22 #include <string_view>
23 #include <vector>
24 
25 #include "perfetto/base/compiler.h"
26 #include "perfetto/base/export.h"
27 #include "perfetto/base/logging.h"
28 
29 namespace perfetto {
30 namespace base {
31 
32 // Represents either the success or the failure message of a function.
33 // This can used as the return type of functions which would usually return an
34 // bool for success or int for errno but also wants to add some string context
35 // (ususally for logging).
36 //
37 // Similar to absl::Status, an optional "payload" can also be included with more
38 // context about the error. This allows passing additional metadata about the
39 // error (e.g. location of errors, potential mitigations etc).
40 class PERFETTO_EXPORT_COMPONENT Status {
41  public:
Status()42   Status() : ok_(true) {}
Status(std::string msg)43   explicit Status(std::string msg) : ok_(false), message_(std::move(msg)) {
44     PERFETTO_CHECK(!message_.empty());
45   }
46 
47   // Copy operations.
48   Status(const Status&) = default;
49   Status& operator=(const Status&) = default;
50 
51   // Move operations. The moved-from state is valid but unspecified.
52   Status(Status&&) noexcept = default;
53   Status& operator=(Status&&) = default;
54 
ok()55   bool ok() const { return ok_; }
56 
57   // When ok() is false this returns the error message. Returns the empty string
58   // otherwise.
message()59   const std::string& message() const { return message_; }
c_message()60   const char* c_message() const { return message_.c_str(); }
61 
62   //////////////////////////////////////////////////////////////////////////////
63   // Payload Management APIs
64   //////////////////////////////////////////////////////////////////////////////
65 
66   // Payloads can be attached to error statuses to provide additional context.
67   //
68   // Payloads are (key, value) pairs, where the key is a string acting as a
69   // unique "type URL" and the value is an opaque string. The "type URL" should
70   // be unique, follow the format of a URL and, ideally, documentation on how to
71   // interpret its associated data should be available.
72   //
73   // To attach a payload to a status object, call `Status::SetPayload()`.
74   // Similarly, to extract the payload from a status, call
75   // `Status::GetPayload()`.
76   //
77   // Note: the payload APIs are only meaningful to call when the status is an
78   // error. Otherwise, all methods are noops.
79 
80   // Gets the payload for the given |type_url| if one exists.
81   //
82   // Will always return std::nullopt if |ok()|.
83   std::optional<std::string_view> GetPayload(std::string_view type_url) const;
84 
85   // Sets the payload for the given key. The key should
86   //
87   // Will always do nothing if |ok()|.
88   void SetPayload(std::string_view type_url, std::string value);
89 
90   // Erases the payload for the given string and returns true if the payload
91   // existed and was erased.
92   //
93   // Will always do nothing if |ok()|.
94   bool ErasePayload(std::string_view type_url);
95 
96  private:
97   struct Payload {
98     std::string type_url;
99     std::string payload;
100   };
101 
102   bool ok_ = false;
103   std::string message_;
104   std::vector<Payload> payloads_;
105 };
106 
107 // Returns a status object which represents the Ok status.
OkStatus()108 inline Status OkStatus() {
109   return Status();
110 }
111 
112 Status ErrStatus(const char* format, ...) PERFETTO_PRINTF_FORMAT(1, 2);
113 
114 }  // namespace base
115 }  // namespace perfetto
116 
117 #endif  // INCLUDE_PERFETTO_BASE_STATUS_H_
118