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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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::kBuddySpaceFull:
  54. s = "BuddySpace full";
  55. break;
  56. case StatusCode::kNetWorkError:
  57. s = "Network error";
  58. break;
  59. case StatusCode::kUnexpectedError:
  60. default:
  61. s = "Unexpected error";
  62. break;
  63. }
  64. }
  65. return std::string(s);
  66. }
  67. Status::Status(StatusCode c) noexcept : code_(c), err_msg_(CodeAsString(c)) {}
  68. Status::Status() noexcept : code_(StatusCode::kOK), err_msg_("") {}
  69. Status::~Status() noexcept {}
  70. Status::Status(const Status &s) : code_(s.code_), err_msg_(s.err_msg_) {}
  71. Status &Status::operator=(const Status &s) {
  72. if (this == &s) {
  73. return *this;
  74. }
  75. code_ = s.code_;
  76. err_msg_ = s.err_msg_;
  77. return *this;
  78. }
  79. Status::Status(Status &&s) noexcept {
  80. code_ = s.code_;
  81. s.code_ = StatusCode::kOK;
  82. err_msg_ = std::move(s.err_msg_);
  83. }
  84. Status &Status::operator=(Status &&s) noexcept {
  85. if (this == &s) {
  86. return *this;
  87. }
  88. code_ = s.code_;
  89. s.code_ = StatusCode::kOK;
  90. err_msg_ = std::move(s.err_msg_);
  91. return *this;
  92. }
  93. Status::Status(const StatusCode code, const std::string &msg) : code_(code), err_msg_(msg) {}
  94. Status::Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra) {
  95. code_ = code;
  96. std::ostringstream ss;
  97. ss << "Thread ID " << this_thread::get_id() << " " << CodeAsString(code) << ". ";
  98. if (!extra.empty()) {
  99. ss << extra;
  100. }
  101. ss << "\n";
  102. ss << "Line of code : " << line_of_code << "\n";
  103. if (file_name != nullptr) {
  104. ss << "File : " << file_name << "\n";
  105. }
  106. err_msg_ = ss.str();
  107. MS_LOG(INFO) << err_msg_;
  108. }
  109. std::ostream &operator<<(std::ostream &os, const Status &s) {
  110. os << s.ToString();
  111. return os;
  112. }
  113. std::string Status::ToString() const { return err_msg_; }
  114. StatusCode Status::get_code() const { return code_; }
  115. } // namespace dataset
  116. } // namespace mindspore