|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/status.h" |
| 21 | + |
| 22 | +#include <cassert> |
| 23 | +#include <cstdlib> |
| 24 | +#include <iostream> |
| 25 | +#include <sstream> |
| 26 | + |
| 27 | +namespace iceberg { |
| 28 | + |
| 29 | +Status::Status(StatusCode code, const std::string& msg) |
| 30 | + : Status::Status(code, msg, nullptr) {} |
| 31 | + |
| 32 | +Status::Status(StatusCode code, std::string msg, std::shared_ptr<StatusDetail> detail) { |
| 33 | + state_ = new State{code, std::move(msg), std::move(detail)}; |
| 34 | +} |
| 35 | + |
| 36 | +void Status::CopyFrom(const Status& s) { |
| 37 | + if (ICEBERG_PREDICT_FALSE(state_ != nullptr)) { |
| 38 | + DeleteState(); |
| 39 | + } |
| 40 | + if (s.state_ == nullptr) { |
| 41 | + state_ = nullptr; |
| 42 | + } else { |
| 43 | + state_ = new State(*s.state_); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +std::string Status::CodeAsString() const { |
| 48 | + if (state_ == nullptr) { |
| 49 | + return "OK"; |
| 50 | + } |
| 51 | + return CodeAsString(code()); |
| 52 | +} |
| 53 | + |
| 54 | +std::string Status::CodeAsString(StatusCode code) { |
| 55 | + const char* type; |
| 56 | + switch (code) { |
| 57 | + case StatusCode::OK: |
| 58 | + type = "OK"; |
| 59 | + break; |
| 60 | + case StatusCode::IOError: |
| 61 | + type = "IOError"; |
| 62 | + break; |
| 63 | + case StatusCode::NotImplemented: |
| 64 | + type = "NotImplemented"; |
| 65 | + break; |
| 66 | + case StatusCode::UnknownError: |
| 67 | + type = "Unknown error"; |
| 68 | + break; |
| 69 | + default: |
| 70 | + type = "Unknown"; |
| 71 | + break; |
| 72 | + } |
| 73 | + return std::string(type); |
| 74 | +} |
| 75 | + |
| 76 | +std::string Status::ToString() const { |
| 77 | + std::ostringstream oss; |
| 78 | + oss << CodeAsString(); |
| 79 | + if (state_ == nullptr) { |
| 80 | + return oss.str(); |
| 81 | + } |
| 82 | + oss << ": "; |
| 83 | + oss << state_->msg; |
| 84 | + if (state_->detail != nullptr) { |
| 85 | + oss << ". Detail: " << state_->detail->ToString(); |
| 86 | + } |
| 87 | + |
| 88 | + return oss.str(); |
| 89 | +} |
| 90 | + |
| 91 | +void Status::Warn() const { std::cerr << *this; } |
| 92 | + |
| 93 | +void Status::Warn(const std::string& message) const { |
| 94 | + std::cerr << message << ": " << *this; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace iceberg |
0 commit comments