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.

infer_log.h 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifndef MINDSPORE_INFERENCE_LOG_H_
  17. #define MINDSPORE_INFERENCE_LOG_H_
  18. #include <stdarg.h>
  19. #include <stdint.h>
  20. #include <string>
  21. #include <sstream>
  22. #include <memory>
  23. #include <iostream>
  24. #include <chrono>
  25. #include <vector>
  26. #ifndef ENABLE_ACL
  27. #include "mindspore/core/utils/log_adapter.h"
  28. #else // ENABLE_ACL
  29. #include "acl/acl.h"
  30. #endif
  31. namespace mindspore::inference {
  32. class LogStream {
  33. public:
  34. LogStream() { sstream_ = std::make_shared<std::stringstream>(); }
  35. ~LogStream() = default;
  36. template <typename T>
  37. LogStream &operator<<(const T &val) noexcept {
  38. (*sstream_) << val;
  39. return *this;
  40. }
  41. template <typename T>
  42. LogStream &operator<<(const std::vector<T> &val) noexcept {
  43. (*sstream_) << "[";
  44. for (size_t i = 0; i < val.size(); i++) {
  45. (*this) << val[i];
  46. if (i + 1 < val.size()) {
  47. (*sstream_) << ", ";
  48. }
  49. }
  50. (*sstream_) << "]";
  51. return *this;
  52. }
  53. LogStream &operator<<(std::ostream &func(std::ostream &os)) noexcept {
  54. (*sstream_) << func;
  55. return *this;
  56. }
  57. friend class LogWriter;
  58. friend class Status;
  59. private:
  60. std::shared_ptr<std::stringstream> sstream_;
  61. };
  62. #ifndef ENABLE_ACL
  63. #define MSI_LOG(level) MS_LOG(level)
  64. #define MSI_LOG_DEBUG MSI_LOG(DEBUG)
  65. #define MSI_LOG_INFO MSI_LOG(INFO)
  66. #define MSI_LOG_WARNING MSI_LOG(WARNING)
  67. #define MSI_LOG_ERROR MSI_LOG(ERROR)
  68. #define MSI_ASSERT(item) MS_ASSERT(item)
  69. #else // ENABLE_ACL
  70. class LogWriter {
  71. public:
  72. LogWriter(const char *file, int line, const char *func, aclLogLevel log_level)
  73. : file_(file), line_(line), func_(func), log_level_(log_level) {}
  74. ~LogWriter() = default;
  75. void operator<(const LogStream &stream) const noexcept __attribute__((visibility("default"))) {
  76. std::ostringstream msg;
  77. msg << stream.sstream_->rdbuf();
  78. OutputLog(msg);
  79. }
  80. private:
  81. void OutputLog(const std::ostringstream &msg) const { aclAppLog(log_level_, func_, file_, line_, msg.str().c_str()); }
  82. const char *file_;
  83. int line_;
  84. const char *func_;
  85. aclLogLevel log_level_;
  86. };
  87. #define MSILOG_IF(level) inference::LogWriter(__FILE__, __LINE__, __FUNCTION__, ACL_##level) < inference::LogStream()
  88. #define MSI_LOG(level) MSI_LOG_##level
  89. #define MSI_LOG_DEBUG MSILOG_IF(DEBUG)
  90. #define MSI_LOG_INFO MSILOG_IF(INFO)
  91. #define MSI_LOG_WARNING MSILOG_IF(WARNING)
  92. #define MSI_LOG_ERROR MSILOG_IF(ERROR)
  93. #define MSI_ASSERT(item)
  94. #endif // ENABLE_ACL
  95. #define MSI_TIME_STAMP_START(name) auto time_start_##name = std::chrono::steady_clock::now();
  96. #define MSI_TIME_STAMP_END(name) \
  97. { \
  98. auto time_end_##name = std::chrono::steady_clock::now(); \
  99. auto time_cost = std::chrono::duration<double, std::milli>(time_end_##name - time_start_##name).count(); \
  100. MSI_LOG_INFO << #name " Time Cost # " << time_cost << " ms ---------------------"; \
  101. }
  102. #define INFER_STATUS(code) inference::Status(code) < inference::LogStream()
  103. #define ERROR_INFER_STATUS(status, type, msg) \
  104. MSI_LOG_ERROR << msg; \
  105. status = inference::Status(type, msg)
  106. } // namespace mindspore::inference
  107. #endif // MINDSPORE_INFERENCE_LOG_H_