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

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