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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef ENABLE_ACL
  25. #include "mindspore/core/utils/log_adapter.h"
  26. namespace mindspore::inference {
  27. #define MSI_LOG(level) MS_LOG(level)
  28. #define MSI_LOG_DEBUG MSI_LOG(DEBUG)
  29. #define MSI_LOG_INFO MSI_LOG(INFO)
  30. #define MSI_LOG_WARNING MSI_LOG(WARNING)
  31. #define MSI_LOG_ERROR MSI_LOG(ERROR)
  32. #define MSI_ASSERT(item) MS_ASSERT(item)
  33. } // namespace mindspore::inference
  34. #else // ENABLE_ACL
  35. #include "acl/acl.h"
  36. namespace mindspore::inference {
  37. class LogStream {
  38. public:
  39. LogStream() { sstream_ = std::make_shared<std::stringstream>(); }
  40. ~LogStream() = default;
  41. template <typename T>
  42. LogStream &operator<<(const T &val) noexcept {
  43. (*sstream_) << val;
  44. return *this;
  45. }
  46. LogStream &operator<<(std::ostream &func(std::ostream &os)) noexcept {
  47. (*sstream_) << func;
  48. return *this;
  49. }
  50. friend class LogWriter;
  51. private:
  52. std::shared_ptr<std::stringstream> sstream_;
  53. };
  54. template <class T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
  55. constexpr std::ostream &operator<<(std::ostream &stream, const T &value) {
  56. return stream << static_cast<typename std::underlying_type<T>::type>(value);
  57. }
  58. class LogWriter {
  59. public:
  60. LogWriter(const char *file, int line, const char *func, aclLogLevel log_level)
  61. : file_(file), line_(line), func_(func), log_level_(log_level) {}
  62. ~LogWriter() = default;
  63. void operator<(const LogStream &stream) const noexcept __attribute__((visibility("default"))) {
  64. std::ostringstream msg;
  65. msg << stream.sstream_->rdbuf();
  66. OutputLog(msg);
  67. }
  68. private:
  69. void OutputLog(const std::ostringstream &msg) const { aclAppLog(log_level_, func_, file_, line_, msg.str().c_str()); }
  70. const char *file_;
  71. int line_;
  72. const char *func_;
  73. aclLogLevel log_level_;
  74. };
  75. #define MSILOG_IF(level) inference::LogWriter(__FILE__, __LINE__, __FUNCTION__, ACL_##level) < inference::LogStream()
  76. #define MSI_LOG(level) MSI_LOG_##level
  77. #define MSI_LOG_DEBUG MSILOG_IF(DEBUG)
  78. #define MSI_LOG_INFO MSILOG_IF(INFO)
  79. #define MSI_LOG_WARNING MSILOG_IF(WARNING)
  80. #define MSI_LOG_ERROR MSILOG_IF(ERROR)
  81. #define MSI_ASSERT(item)
  82. } // namespace mindspore::inference
  83. #endif // ENABLE_ACL
  84. #endif // MINDSPORE_INFERENCE_LOG_H_