You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

status.cc 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  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. #include "dataset/util/status.h"
  17. #include <sstream>
  18. #include "common/utils.h"
  19. #include "dataset/util/task_manager.h"
  20. namespace mindspore {
  21. namespace dataset {
  22. std::string CodeAsString(const StatusCode c) {
  23. const char *s = nullptr;
  24. if (c == StatusCode::kOK) {
  25. // Optimize the most frequent case
  26. return std::string("OK");
  27. } else {
  28. switch (c) {
  29. case StatusCode::kOutOfMemory:
  30. s = "Out of memory";
  31. break;
  32. case StatusCode::kInterrupted:
  33. s = "Interrupted system call";
  34. break;
  35. case StatusCode::kShapeMisMatch:
  36. s = "Shape is incorrect.";
  37. break;
  38. case StatusCode::kNoSpace:
  39. s = "No space left on device";
  40. break;
  41. case StatusCode::kPyFuncException:
  42. s = "Exception thrown from PyFunc";
  43. break;
  44. case StatusCode::kDuplicateKey:
  45. s = "Duplicate key";
  46. break;
  47. case StatusCode::kUnexpectedError:
  48. default:
  49. s = "Unexpected error";
  50. break;
  51. }
  52. }
  53. return std::string(s);
  54. }
  55. Status::Status(StatusCode c) noexcept : code_(c), err_msg_(std::move(CodeAsString(c))) {}
  56. Status::Status() noexcept : code_(StatusCode::kOK), err_msg_("") {}
  57. Status::~Status() noexcept {}
  58. Status::Status(const Status &s) : code_(s.code_), err_msg_(s.err_msg_) {}
  59. Status &Status::operator=(const Status &s) {
  60. if (this == &s) {
  61. return *this;
  62. }
  63. code_ = s.code_;
  64. err_msg_ = s.err_msg_;
  65. return *this;
  66. }
  67. Status::Status(Status &&s) noexcept {
  68. code_ = s.code_;
  69. s.code_ = StatusCode::kOK;
  70. err_msg_ = std::move(s.err_msg_);
  71. }
  72. Status &Status::operator=(Status &&s) noexcept {
  73. if (this == &s) {
  74. return *this;
  75. }
  76. code_ = s.code_;
  77. s.code_ = StatusCode::kOK;
  78. err_msg_ = std::move(s.err_msg_);
  79. return *this;
  80. }
  81. Status::Status(const StatusCode code, const std::string &msg) : code_(code), err_msg_(msg) {}
  82. Status::Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra) {
  83. code_ = code;
  84. std::ostringstream ss;
  85. ss << "Thread ID " << this_thread::get_id() << " " << CodeAsString(code) << ". ";
  86. if (!extra.empty()) {
  87. ss << extra;
  88. }
  89. ss << "\n";
  90. ss << "Line of code : " << line_of_code << "\n";
  91. if (file_name != nullptr) {
  92. ss << "File : " << file_name << "\n";
  93. }
  94. err_msg_ = ss.str();
  95. MS_LOG(INFO) << err_msg_;
  96. }
  97. std::ostream &operator<<(std::ostream &os, const Status &s) {
  98. os << s.ToString();
  99. return os;
  100. }
  101. std::string Status::ToString() const { return err_msg_; }
  102. StatusCode Status::get_code() const { return code_; }
  103. } // namespace dataset
  104. } // namespace mindspore