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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "minddata/dataset/util/status.h"
  17. #include <sstream>
  18. #include "utils/ms_utils.h"
  19. #include "minddata/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::kProfilingError:
  48. s = "Error encountered while profiling";
  49. break;
  50. case StatusCode::kSyntaxError:
  51. s = "Syntax error";
  52. break;
  53. case StatusCode::kUnexpectedError:
  54. default:
  55. s = "Unexpected error";
  56. break;
  57. }
  58. }
  59. return std::string(s);
  60. }
  61. Status::Status(StatusCode c) noexcept : code_(c), err_msg_(std::move(CodeAsString(c))) {}
  62. Status::Status() noexcept : code_(StatusCode::kOK), err_msg_("") {}
  63. Status::~Status() noexcept {}
  64. Status::Status(const Status &s) : code_(s.code_), err_msg_(s.err_msg_) {}
  65. Status &Status::operator=(const Status &s) {
  66. if (this == &s) {
  67. return *this;
  68. }
  69. code_ = s.code_;
  70. err_msg_ = s.err_msg_;
  71. return *this;
  72. }
  73. Status::Status(Status &&s) noexcept {
  74. code_ = s.code_;
  75. s.code_ = StatusCode::kOK;
  76. err_msg_ = std::move(s.err_msg_);
  77. }
  78. Status &Status::operator=(Status &&s) noexcept {
  79. if (this == &s) {
  80. return *this;
  81. }
  82. code_ = s.code_;
  83. s.code_ = StatusCode::kOK;
  84. err_msg_ = std::move(s.err_msg_);
  85. return *this;
  86. }
  87. Status::Status(const StatusCode code, const std::string &msg) : code_(code), err_msg_(msg) {}
  88. Status::Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra) {
  89. code_ = code;
  90. std::ostringstream ss;
  91. ss << "Thread ID " << this_thread::get_id() << " " << CodeAsString(code) << ". ";
  92. if (!extra.empty()) {
  93. ss << extra;
  94. }
  95. ss << "\n";
  96. ss << "Line of code : " << line_of_code << "\n";
  97. if (file_name != nullptr) {
  98. ss << "File : " << file_name << "\n";
  99. }
  100. err_msg_ = ss.str();
  101. MS_LOG(INFO) << err_msg_;
  102. }
  103. std::ostream &operator<<(std::ostream &os, const Status &s) {
  104. os << s.ToString();
  105. return os;
  106. }
  107. std::string Status::ToString() const { return err_msg_; }
  108. StatusCode Status::get_code() const { return code_; }
  109. } // namespace dataset
  110. } // namespace mindspore