onnxruntime/onnxruntime/core/common/status.cc
Dmitri Smirnov d1b1cdc5c4
Replace GSL with GSL-LITE submodule and fix up refs (#1920)
Remove gsl subodule and replace with a local copy of gsl-lite
  Refactor for onnxruntime::make_unique
  gsl::span size and index are now size_t
  Remove lambda auto argument type detection.
  Remove constexpr from fail_fast in gsl due to Linux not being happy.
  Comment out std::stream support due to MacOS std lib broken.
  Move make_unique into include/core/common so it is accessible for server builds.
  Relax requirements for onnxruntime/test/providers/cpu/ml/write_scores_test.cc
  due to x86 build.
  Add ONNXRUNTIME_ROOT to Server Lib includes so gsl is recognized
2019-10-01 12:43:29 -07:00

91 lines
2.8 KiB
C++

/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// Modifications Copyright (c) Microsoft.
#include "core/common/status.h"
#include "core/common/common.h"
namespace onnxruntime {
namespace common {
Status::Status(StatusCategory category, int code, const std::string& msg) {
// state_ will be allocated here causing the status to be treated as a failure
ORT_ENFORCE(code != static_cast<int>(common::OK));
state_ = onnxruntime::make_unique<State>(category, code, msg);
}
Status::Status(StatusCategory category, int code, const char* msg) {
// state_ will be allocated here causing the status to be treated as a failure
ORT_ENFORCE(code != static_cast<int>(common::OK));
state_ = onnxruntime::make_unique<State>(category, code, msg);
}
Status::Status(StatusCategory category, int code)
: Status(category, code, "") {
}
StatusCategory Status::Category() const noexcept {
return IsOK() ? common::NONE : state_->category;
}
int Status::Code() const noexcept {
return IsOK() ? static_cast<int>(common::OK) : state_->code;
}
const std::string& Status::ErrorMessage() const noexcept {
return IsOK() ? EmptyString() : state_->msg;
}
std::string Status::ToString() const {
if (state_ == nullptr) {
return std::string("OK");
}
std::string result;
if (common::SYSTEM == state_->category) {
result += "SystemError";
result += " : ";
result += std::to_string(errno);
} else if (common::ONNXRUNTIME == state_->category) {
result += "[ONNXRuntimeError]";
result += " : ";
result += std::to_string(Code());
result += " : ";
result += StatusCodeToString(static_cast<StatusCode>(Code()));
result += " : ";
result += state_->msg;
}
return result;
}
// GSL_SUPRESS(i.22) is broken. Ignore the warnings for the static local variables that are trivial
// and should not have any destruction order issues via pragmas instead.
// https://developercommunity.visualstudio.com/content/problem/249706/gslsuppress-does-not-work-for-i22-c-core-guideline.html
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 26426)
#endif
const std::string& Status::EmptyString() noexcept {
static std::string s_empty;
return s_empty;
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
} // namespace common
} // namespace onnxruntime