• 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_TRACE_PROCESSOR_STATUS_H_
18 #define INCLUDE_PERFETTO_TRACE_PROCESSOR_STATUS_H_
19 
20 #include <stdarg.h>
21 #include <string>
22 
23 #include "perfetto/base/export.h"
24 
25 namespace perfetto {
26 namespace trace_processor {
27 
28 // Status and related methods are inside util for consistency with embedders of
29 // trace processor.
30 namespace util {
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 class PERFETTO_EXPORT Status {
37  public:
Status()38   Status() : ok_(true) {}
Status(std::string error)39   explicit Status(std::string error) : ok_(false), message_(std::move(error)) {}
40 
41   // Copy operations.
42   Status(const Status&) = default;
43   Status& operator=(const Status&) = default;
44 
45   // Move operations. The moved-from state is valid but unspecified.
46   Status(Status&&) noexcept = default;
47   Status& operator=(Status&&) = default;
48 
ok()49   bool ok() const { return ok_; }
50 
51   // Only valid to call when this message has an Err status (i.e. ok() returned
52   // false or operator bool() returned true).
message()53   const std::string& message() const { return message_; }
54 
55   // Only valid to call when this message has an Err status (i.e. ok() returned
56   // false or operator bool() returned true).
c_message()57   const char* c_message() const { return message_.c_str(); }
58 
59  private:
60   bool ok_ = false;
61   std::string message_;
62 };
63 
64 // Returns a status object which represents the Ok status.
OkStatus()65 inline Status OkStatus() {
66   return Status();
67 }
68 
69 // Returns a status object which represents an error with the given message
70 // formatted using printf.
ErrStatus(const char * format,...)71 __attribute__((__format__(__printf__, 1, 2))) inline Status ErrStatus(
72     const char* format,
73     ...) {
74   va_list ap;
75   va_start(ap, format);
76 
77   char buffer[1024];
78   vsnprintf(buffer, sizeof(buffer), format, ap);
79   return Status(std::string(buffer));
80 }
81 
82 }  // namespace util
83 }  // namespace trace_processor
84 }  // namespace perfetto
85 
86 #endif  // INCLUDE_PERFETTO_TRACE_PROCESSOR_STATUS_H_
87