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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. IndexError,
  54. ValueError,
  55. TypeError,
  56. };
  57. struct LocationInfo {
  58. LocationInfo(const char *file, int line, const char *func) : file_(file), line_(line), func_(func) {}
  59. ~LocationInfo() = default;
  60. const char *file_;
  61. int line_;
  62. const char *func_;
  63. };
  64. class LogStream {
  65. public:
  66. LogStream() { sstream_ = std::make_shared<std::stringstream>(); }
  67. ~LogStream() = default;
  68. template <typename T>
  69. LogStream &operator<<(const T &val) noexcept {
  70. (*sstream_) << val;
  71. return *this;
  72. }
  73. LogStream &operator<<(std::ostream &func(std::ostream &os)) noexcept {
  74. (*sstream_) << func;
  75. return *this;
  76. }
  77. friend class LogWriter;
  78. private:
  79. std::shared_ptr<std::stringstream> sstream_;
  80. };
  81. template <class T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
  82. constexpr std::ostream &operator<<(std::ostream &stream, const T &value) {
  83. return stream << static_cast<typename std::underlying_type<T>::type>(value);
  84. }
  85. enum MsLogLevel : int { DEBUG = 0, INFO, WARNING, ERROR, EXCEPTION };
  86. enum SubModuleId : int {
  87. SM_UNKNOWN = 0, // unknown submodule
  88. SM_ANALYZER, // static analyzer
  89. SM_COMMON, // common
  90. SM_DEBUG, // debug
  91. SM_DEVICE, // device
  92. SM_GE_ADPT, // ge adapter
  93. SM_IR, // IR
  94. SM_KERNEL, // kernel
  95. SM_MD, // MindData
  96. SM_ME, // MindExpression
  97. SM_ONNX, // ONNX
  98. SM_OPTIMIZER, // optimzer
  99. SM_PARALLEL, // parallel
  100. SM_PARSER, // parser
  101. SM_PIPELINE, // ME pipeline
  102. SM_PRE_ACT, // pre-activate
  103. SM_PYNATIVE, // PyNative
  104. SM_SESSION, // session
  105. SM_UTILS, // utils
  106. SM_VM, // VM
  107. NUM_SUBMODUES // number of submodules
  108. };
  109. #ifndef SUBMODULE_ID
  110. #define SUBMODULE_ID mindspore::SubModuleId::SM_ME
  111. #endif
  112. #if defined(_WIN32) || defined(_WIN64)
  113. extern int g_ms_submodule_log_levels[] __attribute__((dllexport));
  114. #else
  115. extern int g_ms_submodule_log_levels[] __attribute__((visibility("default")));
  116. #endif
  117. class LogWriter {
  118. public:
  119. LogWriter(const LocationInfo &location, MsLogLevel log_level, SubModuleId submodule,
  120. ExceptionType excp_type = NoExceptionType)
  121. : location_(location), log_level_(log_level), submodule_(submodule), exception_type_(excp_type) {}
  122. ~LogWriter() = default;
  123. void operator<(const LogStream &stream) const noexcept __attribute__((visibility("default")));
  124. void operator^(const LogStream &stream) const __attribute__((noreturn, visibility("default")));
  125. private:
  126. void OutputLog(const std::ostringstream &msg) const;
  127. LocationInfo location_;
  128. MsLogLevel log_level_;
  129. SubModuleId submodule_;
  130. ExceptionType exception_type_;
  131. };
  132. #define MSLOG_IF(level, condition, excp_type) \
  133. static_cast<void>(0), !(condition) \
  134. ? void(0) \
  135. : mindspore::LogWriter(mindspore::LocationInfo(FILE_NAME, __LINE__, __FUNCTION__), level, \
  136. SUBMODULE_ID, excp_type) < mindspore::LogStream()
  137. #define MSLOG_THROW(excp_type) \
  138. mindspore::LogWriter(mindspore::LocationInfo(FILE_NAME, __LINE__, __FUNCTION__), mindspore::EXCEPTION, SUBMODULE_ID, \
  139. excp_type) ^ \
  140. mindspore::LogStream()
  141. #define IS_OUTPUT_ON(level) (level) >= mindspore::g_ms_submodule_log_levels[SUBMODULE_ID]
  142. #define MS_LOG(level) MS_LOG_##level
  143. #define MS_LOG_DEBUG MSLOG_IF(mindspore::DEBUG, IS_OUTPUT_ON(mindspore::DEBUG), mindspore::NoExceptionType)
  144. #define MS_LOG_INFO MSLOG_IF(mindspore::INFO, IS_OUTPUT_ON(mindspore::INFO), mindspore::NoExceptionType)
  145. #define MS_LOG_WARNING MSLOG_IF(mindspore::WARNING, IS_OUTPUT_ON(mindspore::WARNING), mindspore::NoExceptionType)
  146. #define MS_LOG_ERROR MSLOG_IF(mindspore::ERROR, IS_OUTPUT_ON(mindspore::ERROR), mindspore::NoExceptionType)
  147. #define MS_LOG_EXCEPTION MSLOG_THROW(mindspore::NoExceptionType)
  148. #define MS_EXCEPTION(type) MSLOG_THROW(type)
  149. } // namespace mindspore
  150. #define MS_EXCEPTION_IF_NULL(ptr) \
  151. do { \
  152. if ((ptr) == nullptr) { \
  153. MS_LOG(EXCEPTION) << ": The pointer[" << #ptr << "] is null."; \
  154. } \
  155. } while (0)
  156. #ifdef DEBUG
  157. #include <cassert>
  158. #define MS_ASSERT(f) assert(f)
  159. #else
  160. #define MS_ASSERT(f) ((void)0)
  161. #endif
  162. #endif // MINDSPORE_CCSRC_UTILS_LOG_ADAPTER_H_