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.h 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Copyright 2020 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_SERVING_LOG_H
  17. #define MINDSPORE_SERVING_LOG_H
  18. #include <iostream>
  19. #include <vector>
  20. #include <unordered_map>
  21. #include <map>
  22. #include <sstream>
  23. #include <memory>
  24. #include <string>
  25. namespace mindspore::serving {
  26. #define MS_API __attribute__((visibility("default")))
  27. #define SERVING_LOG_HDR_FILE_REL_PATH "mindspore_serving/ccsrc/common/log.h"
  28. // Get start index of file relative path in __FILE__
  29. static constexpr int GetRelPathPos() noexcept {
  30. return sizeof(__FILE__) > sizeof(SERVING_LOG_HDR_FILE_REL_PATH)
  31. ? sizeof(__FILE__) - sizeof(SERVING_LOG_HDR_FILE_REL_PATH)
  32. : 0;
  33. }
  34. #define SERVING_FILE_NAME \
  35. (sizeof(__FILE__) > mindspore::serving::GetRelPathPos() \
  36. ? static_cast<const char *>(__FILE__) + mindspore::serving::GetRelPathPos() \
  37. : static_cast<const char *>(__FILE__))
  38. class LogStream {
  39. public:
  40. LogStream() { sstream_ = std::make_shared<std::stringstream>(); }
  41. ~LogStream() = default;
  42. template <typename T>
  43. LogStream &operator<<(const T &val) noexcept {
  44. (*sstream_) << val;
  45. return *this;
  46. }
  47. template <typename T>
  48. LogStream &operator<<(const std::vector<T> &val) noexcept {
  49. (*sstream_) << "[";
  50. for (size_t i = 0; i < val.size(); i++) {
  51. (*this) << val[i];
  52. if (i + 1 < val.size()) {
  53. (*sstream_) << ", ";
  54. }
  55. }
  56. (*sstream_) << "]";
  57. return *this;
  58. }
  59. template <typename K, typename V>
  60. LogStream &operator<<(const std::unordered_map<K, V> &val) noexcept {
  61. (*sstream_) << "{";
  62. for (auto &item : val) {
  63. (*this) << "{" << item.first << ": " << item.second << "} ";
  64. }
  65. (*sstream_) << "}";
  66. return *this;
  67. }
  68. template <typename K, typename V>
  69. LogStream &operator<<(const std::map<K, V> &val) noexcept {
  70. (*sstream_) << "{";
  71. for (auto &item : val) {
  72. (*this) << "{" << item.first << ": " << item.second << "} ";
  73. }
  74. (*sstream_) << "}";
  75. return *this;
  76. }
  77. LogStream &operator<<(std::ostream &func(std::ostream &os)) noexcept {
  78. (*sstream_) << func;
  79. return *this;
  80. }
  81. friend class LogWriter;
  82. friend class Status;
  83. private:
  84. std::shared_ptr<std::stringstream> sstream_;
  85. };
  86. enum MsLogLevel {
  87. LOG_DEBUG,
  88. LOG_INFO,
  89. LOG_WARNING,
  90. LOG_ERROR,
  91. LOG_EXCEPTION,
  92. };
  93. class MS_API LogWriter {
  94. public:
  95. LogWriter(const char *file, int line, const char *func, MsLogLevel log_level)
  96. : file_(file), line_(line), func_(func), log_level_(log_level) {}
  97. ~LogWriter() = default;
  98. std::string operator<(const LogStream &stream) const noexcept __attribute__((visibility("default"))) {
  99. std::ostringstream msg;
  100. msg << stream.sstream_->rdbuf();
  101. OutputLog(msg);
  102. return msg.str();
  103. }
  104. void operator^(const LogStream &stream) const __attribute__((noreturn, visibility("default"))) {
  105. std::ostringstream msg;
  106. msg << stream.sstream_->rdbuf();
  107. OutputLog(msg);
  108. throw std::runtime_error(msg.str());
  109. }
  110. private:
  111. void OutputLog(const std::ostringstream &msg) const;
  112. const char *file_;
  113. int line_;
  114. const char *func_;
  115. MsLogLevel log_level_;
  116. };
  117. extern int g_ms_serving_log_level MS_API;
  118. #define MSILOG_IF(level, condition) \
  119. static_cast<void>(0), \
  120. !(condition) ? std::string() \
  121. : mindspore::serving::LogWriter(SERVING_FILE_NAME, __LINE__, __FUNCTION__, \
  122. mindspore::serving::LOG_##level) < mindspore::serving::LogStream()
  123. #define MSILOG_NOIF(level) \
  124. mindspore::serving::LogWriter(SERVING_FILE_NAME, __LINE__, __FUNCTION__, mindspore::serving::LOG_##level) < \
  125. mindspore::serving::LogStream()
  126. #define IS_OUTPUT_ON(level) (mindspore::serving::LOG_##level) >= mindspore::serving::g_ms_serving_log_level
  127. #define MSILOG_THROW \
  128. mindspore::serving::LogWriter(SERVING_FILE_NAME, __LINE__, __FUNCTION__, mindspore::serving::LOG_EXCEPTION) ^ \
  129. mindspore::serving::LogStream()
  130. #define MSI_LOG(level) MSI_LOG_##level
  131. #define MSI_LOG_DEBUG MSILOG_IF(DEBUG, IS_OUTPUT_ON(DEBUG))
  132. #define MSI_LOG_INFO MSILOG_IF(INFO, IS_OUTPUT_ON(INFO))
  133. #define MSI_LOG_WARNING MSILOG_IF(WARNING, IS_OUTPUT_ON(WARNING))
  134. #define MSI_LOG_ERROR MSILOG_IF(ERROR, IS_OUTPUT_ON(ERROR))
  135. #define MSI_LOG_EXCEPTION MSILOG_THROW
  136. #define MSI_EXCEPTION_IF_NULL(ptr) \
  137. do { \
  138. if ((ptr) == nullptr) { \
  139. MSI_LOG_EXCEPTION << ": The pointer[" << #ptr << "] is null."; \
  140. } \
  141. } while (0)
  142. } // namespace mindspore::serving
  143. #endif // MINDSPORE_SERVING_LOG_H

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.