1 | /* |
---|---|
2 | * SPDX-License-Identifier: Apache-2.0 |
3 | */ |
4 | |
5 | #include "status.h" |
6 | #include <assert.h> |
7 | #include "onnx/string_utils.h" |
8 | |
9 | namespace ONNX_NAMESPACE { |
10 | namespace Common { |
11 | |
12 | Status::Status(StatusCategory category, int code, const std::string& msg) { |
13 | assert(static_cast<int>(StatusCode::OK) != code); |
14 | state_.reset(new State(category, code, msg)); |
15 | } |
16 | |
17 | Status::Status(StatusCategory category, int code) : Status(category, code, EmptyString()) {} |
18 | |
19 | bool Status::IsOK() const noexcept { |
20 | return (state_ == NULL); |
21 | } |
22 | |
23 | StatusCategory Status::Category() const noexcept { |
24 | return IsOK() ? StatusCategory::NONE : state_->category; |
25 | } |
26 | |
27 | int Status::Code() const noexcept { |
28 | return IsOK() ? static_cast<int>(StatusCode::OK) : state_->code; |
29 | } |
30 | |
31 | const std::string& Status::ErrorMessage() const { |
32 | return IsOK() ? EmptyString() : state_->msg; |
33 | } |
34 | |
35 | std::string Status::ToString() const { |
36 | if (state_ == nullptr) { |
37 | return std::string("OK"); |
38 | } |
39 | |
40 | std::string result; |
41 | |
42 | if (StatusCategory::CHECKER == state_->category) { |
43 | result += "[CheckerError]"; |
44 | } else if (StatusCategory::OPTIMIZER == state_->category) { |
45 | result += "[OptimizerError]"; |
46 | } |
47 | |
48 | result += " : "; |
49 | result += ONNX_NAMESPACE::to_string(Code()); |
50 | std::string msg; |
51 | |
52 | switch (static_cast<StatusCode>(Code())) { |
53 | case INVALID_ARGUMENT: |
54 | msg = "INVALID_ARGUMENT"; |
55 | break; |
56 | case INVALID_PROTOBUF: |
57 | msg = "INVALID_PROTOBUF"; |
58 | break; |
59 | case FAIL: |
60 | msg = "FAIL"; |
61 | break; |
62 | default: |
63 | msg = "GENERAL ERROR"; |
64 | break; |
65 | } |
66 | result += " : "; |
67 | result += msg; |
68 | result += " : "; |
69 | result += state_->msg; |
70 | |
71 | return result; |
72 | } |
73 | |
74 | const Status& Status::OK() noexcept { |
75 | static Status s_ok; |
76 | return s_ok; |
77 | } |
78 | |
79 | const std::string& Status::EmptyString() { |
80 | static std::string empty_str; |
81 | return empty_str; |
82 | } |
83 | |
84 | } // namespace Common |
85 | } // namespace ONNX_NAMESPACE |
86 |