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.h 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright 2020 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. #ifndef MINDSPORE_SERVING_STATUS_H
  17. #define MINDSPORE_SERVING_STATUS_H
  18. #include <chrono>
  19. #include <string>
  20. #include <sstream>
  21. #include "common/log.h"
  22. namespace mindspore::serving {
  23. enum StatusCode { SUCCESS = 0, FAILED, INVALID_INPUTS, SYSTEM_ERROR };
  24. class Status {
  25. public:
  26. Status() : status_code_(FAILED) {}
  27. Status(enum StatusCode status_code, const std::string &status_msg = "")
  28. : status_code_(status_code), status_msg_(status_msg) {}
  29. bool IsSuccess() const { return status_code_ == SUCCESS; }
  30. enum StatusCode StatusCode() const { return status_code_; }
  31. std::string StatusMessage() const { return status_msg_; }
  32. bool operator==(const Status &other) const { return status_code_ == other.status_code_; }
  33. bool operator==(enum StatusCode other_code) const { return status_code_ == other_code; }
  34. bool operator!=(const Status &other) const { return status_code_ != other.status_code_; }
  35. bool operator!=(enum StatusCode other_code) const { return status_code_ != other_code; }
  36. operator bool() const = delete;
  37. Status &operator<(const LogStream &stream) noexcept __attribute__((visibility("default"))) {
  38. status_msg_ = stream.sstream_->str();
  39. return *this;
  40. }
  41. Status &operator=(const std::string &msg) noexcept __attribute__((visibility("default"))) {
  42. status_msg_ = msg;
  43. return *this;
  44. }
  45. private:
  46. enum StatusCode status_code_;
  47. std::string status_msg_;
  48. };
  49. #define MSI_TIME_STAMP_START(name) auto time_start_##name = std::chrono::steady_clock::now();
  50. #define MSI_TIME_STAMP_END(name) \
  51. { \
  52. auto time_end_##name = std::chrono::steady_clock::now(); \
  53. auto time_cost = std::chrono::duration<double, std::milli>(time_end_##name - time_start_##name).count(); \
  54. MSI_LOG_INFO << #name " Time Cost # " << time_cost << " ms ---------------------"; \
  55. }
  56. #define INFER_STATUS(code) mindspore::serving::Status(code) < mindspore::serving::LogStream()
  57. #define INFER_STATUS_LOG_ERROR(code) mindspore::serving::Status(code) = MSILOG_NOIF(ERROR)
  58. #define INFER_STATUS_LOG_WARNING(code) mindspore::serving::Status(code) = MSILOG_NOIF(WARNING)
  59. } // namespace mindspore::serving
  60. #endif // MINDSPORE_SERVING_STATUS_H

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.