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.

log_adapter.h 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_CCSRC_UTILS_LOG_ADAPTER_H_
  17. #define MINDSPORE_CCSRC_UTILS_LOG_ADAPTER_H_
  18. #include <stdarg.h>
  19. #include <stdint.h>
  20. #include <string>
  21. #include <sstream>
  22. #include <memory>
  23. #include "./overload.h"
  24. #include "./securec.h"
  25. #ifdef USE_GLOG
  26. #include "glog/logging.h"
  27. #else
  28. #include "toolchain/slog.h"
  29. #endif
  30. // NOTICE: when relative path of 'log_adapter.h' changed, macro 'LOG_HDR_FILE_REL_PATH' must be changed
  31. #define LOG_HDR_FILE_REL_PATH "mindspore/ccsrc/utils/log_adapter.h"
  32. // Get start index of file relative path in __FILE__
  33. static constexpr int GetRelPathPos() noexcept {
  34. return sizeof(__FILE__) > sizeof(LOG_HDR_FILE_REL_PATH) ? sizeof(__FILE__) - sizeof(LOG_HDR_FILE_REL_PATH) : 0;
  35. }
  36. namespace mindspore {
  37. #define FILE_NAME \
  38. (sizeof(__FILE__) > GetRelPathPos() ? static_cast<const char *>(__FILE__) + GetRelPathPos() \
  39. : static_cast<const char *>(__FILE__))
  40. enum ExceptionType {
  41. NoExceptionType = 0,
  42. UnknownError,
  43. ArgumentError,
  44. NotSupportError,
  45. NotExistsError,
  46. AlreadyExistsError,
  47. UnavailableError,
  48. DeviceProcessError,
  49. AbortedError,
  50. TimeOutError,
  51. ResourceUnavailable,
  52. NoPermissionError,
  53. ValueError,
  54. TypeError,
  55. };
  56. struct LocationInfo {
  57. LocationInfo(const char *file, int line, const char *func) : file_(file), line_(line), func_(func) {}
  58. ~LocationInfo() = default;
  59. const char *file_;
  60. int line_;
  61. const char *func_;
  62. };
  63. class LogStream {
  64. public:
  65. LogStream() { sstream_ = std::make_shared<std::stringstream>(); }
  66. ~LogStream() = default;
  67. template <typename T>
  68. LogStream &operator<<(const T &val) noexcept {
  69. (*sstream_) << val;
  70. return *this;
  71. }
  72. LogStream &operator<<(std::ostream &func(std::ostream &os)) noexcept {
  73. (*sstream_) << func;
  74. return *this;
  75. }
  76. friend class LogWriter;
  77. private:
  78. std::shared_ptr<std::stringstream> sstream_;
  79. };
  80. template <class T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
  81. constexpr std::ostream &operator<<(std::ostream &stream, const T &value) {
  82. return stream << static_cast<typename std::underlying_type<T>::type>(value);
  83. }
  84. enum MsLogLevel : int { DEBUG = 0, INFO, WARNING, ERROR, EXCEPTION };
  85. #ifndef USE_GLOG
  86. extern int g_mslog_level;
  87. #endif
  88. class LogWriter {
  89. public:
  90. LogWriter(const LocationInfo &location, MsLogLevel log_level, ExceptionType excp_type = NoExceptionType)
  91. : location_(location), log_level_(log_level), exception_type_(excp_type) {}
  92. ~LogWriter() = default;
  93. void operator<(const LogStream &stream) const noexcept __attribute__((visibility("default")));
  94. void operator^(const LogStream &stream) const __attribute__((noreturn, visibility("default")));
  95. private:
  96. void OutputLog(const std::ostringstream &msg) const;
  97. LocationInfo location_;
  98. MsLogLevel log_level_;
  99. ExceptionType exception_type_;
  100. };
  101. #define MSLOG_IF(level, condition, excp_type) \
  102. static_cast<void>(0), !(condition) \
  103. ? void(0) \
  104. : mindspore::LogWriter(mindspore::LocationInfo(FILE_NAME, __LINE__, __FUNCTION__), level, \
  105. excp_type) < mindspore::LogStream()
  106. #define MSLOG_THROW(excp_type) \
  107. mindspore::LogWriter(mindspore::LocationInfo(FILE_NAME, __LINE__, __FUNCTION__), mindspore::EXCEPTION, excp_type) ^ \
  108. mindspore::LogStream()
  109. #ifdef USE_GLOG
  110. #define IS_OUTPUT_ON(level) (level) >= FLAGS_v
  111. #else
  112. #define IS_OUTPUT_ON(level) (level) >= mindspore::g_mslog_level
  113. #endif
  114. #define MS_LOG(level) MS_LOG_##level
  115. #define MS_LOG_DEBUG MSLOG_IF(mindspore::DEBUG, IS_OUTPUT_ON(mindspore::DEBUG), mindspore::NoExceptionType)
  116. #define MS_LOG_INFO MSLOG_IF(mindspore::INFO, IS_OUTPUT_ON(mindspore::INFO), mindspore::NoExceptionType)
  117. #define MS_LOG_WARNING MSLOG_IF(mindspore::WARNING, IS_OUTPUT_ON(mindspore::WARNING), mindspore::NoExceptionType)
  118. #define MS_LOG_ERROR MSLOG_IF(mindspore::ERROR, IS_OUTPUT_ON(mindspore::ERROR), mindspore::NoExceptionType)
  119. #define MS_LOG_EXCEPTION MSLOG_THROW(mindspore::NoExceptionType)
  120. #define MS_EXCEPTION(type) MSLOG_THROW(type)
  121. } // namespace mindspore
  122. #define MS_EXCEPTION_IF_NULL(ptr) \
  123. do { \
  124. if ((ptr) == nullptr) { \
  125. MS_LOG(EXCEPTION) << ": The pointer[" << #ptr << "] is null."; \
  126. } \
  127. } while (0)
  128. #ifdef DEBUG
  129. #include <cassert>
  130. #define MS_ASSERT(f) assert(f)
  131. #else
  132. #define MS_ASSERT(f) ((void)0)
  133. #endif
  134. #endif // MINDSPORE_CCSRC_UTILS_LOG_ADAPTER_H_