Spaces:
Sleeping
Sleeping
File size: 4,103 Bytes
2aebc50 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
// Copyright 2016 Google Inc.
//
// 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.!
#include <cstring>
#include "common.h"
#include "init.h"
#include "sentencepiece_processor.h"
#ifdef _USE_EXTERNAL_ABSL
// Naive workaround to define minloglevel on external absl package.
// We want to define them in other cc file.
#include "third_party/absl/flags/flag.h"
#include "third_party/absl/flags/parse.h"
ABSL_FLAG(int32, minloglevel, 0,
"Messages logged at a lower level than this don't actually.");
#endif
namespace sentencepiece {
namespace error {
int gTestCounter = 0;
void Abort() {
if (GetTestCounter() == 1) {
SetTestCounter(2);
} else {
std::cerr << "Program terminated with an unrecoverable error." << std::endl;
ShutdownLibrary();
exit(-1);
}
}
void Exit(int code) {
if (GetTestCounter() == 1) {
SetTestCounter(2);
} else {
ShutdownLibrary();
exit(code);
}
}
void SetTestCounter(int c) { gTestCounter = c; }
bool GetTestCounter() { return gTestCounter; }
} // namespace error
namespace util {
Status::Status() {}
Status::~Status() {}
struct Status::Rep {
StatusCode code;
std::string error_message;
};
Status::Status(StatusCode code, absl::string_view error_message)
: rep_(new Rep) {
rep_->code = code;
rep_->error_message = std::string(error_message);
}
Status::Status(const Status& s)
: rep_((s.rep_ == nullptr) ? nullptr : new Rep(*s.rep_)) {}
void Status::operator=(const Status& s) {
if (rep_ != s.rep_)
rep_.reset((s.rep_ == nullptr) ? nullptr : new Rep(*s.rep_));
}
bool Status::operator==(const Status& s) const { return (rep_ == s.rep_); }
bool Status::operator!=(const Status& s) const { return (rep_ != s.rep_); }
const char* Status::error_message() const {
return ok() ? "" : rep_->error_message.c_str();
}
void Status::set_error_message(const char* str) {
if (rep_ == nullptr) rep_.reset(new Rep);
rep_->error_message = str;
}
StatusCode Status::code() const { return ok() ? StatusCode::kOk : rep_->code; }
std::string Status::ToString() const {
if (rep_ == nullptr) return "OK";
std::string result;
switch (code()) {
case StatusCode::kCancelled:
result = "Cancelled";
break;
case StatusCode::kUnknown:
result = "Unknown";
break;
case StatusCode::kInvalidArgument:
result = "Invalid argument";
break;
case StatusCode::kDeadlineExceeded:
result = "Deadline exceeded";
break;
case StatusCode::kNotFound:
result = "Not found";
break;
case StatusCode::kAlreadyExists:
result = "Already exists";
break;
case StatusCode::kPermissionDenied:
result = "Permission denied";
break;
case StatusCode::kResourceExhausted:
result = "Unauthenticated";
break;
case StatusCode::kFailedPrecondition:
result = "Failed precondition";
break;
case StatusCode::kAborted:
result = "Aborted";
break;
case StatusCode::kOutOfRange:
result = "Out of range";
break;
case StatusCode::kUnimplemented:
result = "Unimplemented";
break;
case StatusCode::kInternal:
result = "Internal";
break;
case StatusCode::kUnavailable:
result = "Unavailable";
break;
case StatusCode::kDataLoss:
result = "Data loss";
break;
case StatusCode::kUnauthenticated:
result = "Unauthenticated";
default:
break;
}
result += ": ";
result += rep_->error_message;
return result;
}
void Status::IgnoreError() {}
} // namespace util
} // namespace sentencepiece
|