Android-cuttlefish cvd tool
error_type.h
Go to the documentation of this file.
1//
2// Copyright (C) 2022 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#pragma once
17
18#include <unistd.h>
19
20#include <ostream>
21#include <sstream>
22#include <string>
23#include <utility>
24#include <vector>
25
26#include "tl/expected.hpp"
27
29
30namespace cuttlefish {
31
32class StackTraceError;
33
35 public:
36 StackTraceEntry(std::string file, size_t line, std::string pretty_function,
37 std::string function);
38
39 StackTraceEntry(std::string file, size_t line, std::string pretty_function,
40 std::string function, std::string expression);
41
42 StackTraceEntry(const StackTraceEntry& other);
43
47
48 template <typename T>
49 StackTraceEntry& operator<<(T&& message_ext) & {
50 message_ << std::forward<T>(message_ext);
51 return *this;
52 }
53 template <typename T>
54 StackTraceEntry operator<<(T&& message_ext) && {
55 message_ << std::forward<T>(message_ext);
56 return std::move(*this);
57 }
58
59 operator StackTraceError() &&;
60 template <typename T>
61 operator tl::expected<T, StackTraceError>() &&;
62
63 bool HasMessage() const;
64 const std::string& Expression() const;
65 const std::string& File() const;
66 const std::string& Function() const;
67 const std::string& PrettyFunction() const;
68 size_t Line() const;
69 std::string Message() const;
70
71 private:
72 std::string file_;
73 size_t line_;
74 std::string pretty_function_;
75 std::string function_;
76 std::string expression_;
77 std::stringstream message_;
78};
79
80std::string ResultErrorFormat(bool color);
81
82#define CF_STACK_TRACE_ENTRY(expression) \
83 StackTraceEntry(__FILE__, __LINE__, __PRETTY_FUNCTION__, __func__, expression)
84
86 public:
89 const std::vector<StackTraceEntry>& Stack() const;
90
91 std::string Message() const;
92
93 std::string Trace() const;
94
95 std::string FormatForEnv(bool color = ShouldColorStdout()) const;
96
97 template <typename T>
98 operator tl::expected<T, StackTraceError>() && {
99 return tl::unexpected(std::move(*this));
100 }
101
102 private:
103 std::vector<StackTraceEntry> stack_;
104};
105
106inline StackTraceEntry::operator StackTraceError() && {
107 return StackTraceError().PushEntry(std::move(*this));
108}
109
110template <typename T>
111inline StackTraceEntry::operator tl::expected<T, StackTraceError>() && {
112 return tl::unexpected(std::move(*this));
113}
114
115std::ostream& operator<<(std::ostream&, const StackTraceError&);
116
117} // namespace cuttlefish
Definition: error_type.h:34
std::string pretty_function_
Definition: error_type.h:74
size_t Line() const
Definition: error_type.cc:361
const std::string & File() const
Definition: error_type.cc:356
StackTraceEntry & operator=(const StackTraceEntry &other)
Definition: error_type.cc:344
StackTraceEntry operator<<(T &&message_ext) &&
Definition: error_type.h:54
const std::string & Expression() const
Definition: error_type.cc:355
size_t line_
Definition: error_type.h:73
std::stringstream message_
Definition: error_type.h:77
std::string file_
Definition: error_type.h:72
bool HasMessage() const
Definition: error_type.cc:354
StackTraceEntry & operator=(StackTraceEntry &&)=default
StackTraceEntry(std::string file, size_t line, std::string pretty_function, std::string function)
Definition: error_type.cc:319
std::string Message() const
Definition: error_type.cc:362
const std::string & PrettyFunction() const
Definition: error_type.cc:358
std::string expression_
Definition: error_type.h:76
std::string function_
Definition: error_type.h:75
const std::string & Function() const
Definition: error_type.cc:357
StackTraceEntry(StackTraceEntry &&)=default
StackTraceEntry & operator<<(T &&message_ext) &
Definition: error_type.h:49
Definition: error_type.h:85
std::string Message() const
Definition: error_type.cc:377
std::vector< StackTraceEntry > stack_
Definition: error_type.h:103
const std::vector< StackTraceEntry > & Stack() const
Definition: error_type.cc:373
std::string Trace() const
Definition: error_type.cc:381
std::string FormatForEnv(bool color=ShouldColorStdout()) const
Definition: error_type.cc:385
StackTraceError & PushEntry(StackTraceEntry entry) &
Definition: error_type.cc:364
#define color
Definition: dict.c:54
Definition: alloc_driver.h:20
std::string ResultErrorFormat(bool color)
Definition: error_type.cc:389
bool ShouldColorStdout()
Definition: should_color.cc:40
std::ostream & operator<<(std::ostream &out, DeviceType device_type)
Definition: device_type.cpp:72
unexpected(E) -> unexpected< E >