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 3.6 kB

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