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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. auto msg_str = GetOutputMsg(msg);
  102. OutputLog(msg_str);
  103. return msg_str;
  104. }
  105. void operator^(const LogStream &stream) const __attribute__((noreturn, visibility("default"))) {
  106. std::ostringstream msg;
  107. msg << stream.sstream_->rdbuf();
  108. auto msg_str = GetOutputMsg(msg);
  109. OutputLog(msg_str);
  110. throw std::runtime_error(msg_str);
  111. }
  112. std::string GetOutputMsg(const std::ostringstream &msg) const {
  113. std::string msg_str = msg.str();
  114. constexpr int max_log_size = 384;
  115. constexpr int msg_log_start_size = 192;
  116. if (msg_str.length() > max_log_size) {
  117. msg_str = msg_str.substr(0, msg_log_start_size) + "..." + msg_str.substr(msg_str.length() - msg_log_start_size);
  118. }
  119. return msg_str;
  120. }
  121. private:
  122. void OutputLog(const std::string &msg_str) const;
  123. const char *file_;
  124. int line_;
  125. const char *func_;
  126. MsLogLevel log_level_;
  127. };
  128. extern int g_ms_serving_log_level MS_API;
  129. #define MSILOG_IF(level, condition) \
  130. static_cast<void>(0), \
  131. !(condition) ? std::string() \
  132. : mindspore::serving::LogWriter(SERVING_FILE_NAME, __LINE__, __FUNCTION__, \
  133. mindspore::serving::LOG_##level) < mindspore::serving::LogStream()
  134. #define MSILOG_NOIF(level) \
  135. mindspore::serving::LogWriter(SERVING_FILE_NAME, __LINE__, __FUNCTION__, mindspore::serving::LOG_##level) < \
  136. mindspore::serving::LogStream()
  137. #define IS_OUTPUT_ON(level) (mindspore::serving::LOG_##level) >= mindspore::serving::g_ms_serving_log_level
  138. #define MSILOG_THROW \
  139. mindspore::serving::LogWriter(SERVING_FILE_NAME, __LINE__, __FUNCTION__, mindspore::serving::LOG_EXCEPTION) ^ \
  140. mindspore::serving::LogStream()
  141. #define MSI_LOG(level) MSI_LOG_##level
  142. #define MSI_LOG_DEBUG MSILOG_IF(DEBUG, IS_OUTPUT_ON(DEBUG))
  143. #define MSI_LOG_INFO MSILOG_IF(INFO, IS_OUTPUT_ON(INFO))
  144. #define MSI_LOG_WARNING MSILOG_IF(WARNING, IS_OUTPUT_ON(WARNING))
  145. #define MSI_LOG_ERROR MSILOG_IF(ERROR, IS_OUTPUT_ON(ERROR))
  146. #define MSI_LOG_EXCEPTION MSILOG_THROW
  147. #define MSI_EXCEPTION_IF_NULL(ptr) \
  148. do { \
  149. if ((ptr) == nullptr) { \
  150. MSI_LOG_EXCEPTION << ": The pointer[" << #ptr << "] is null."; \
  151. } \
  152. } while (0)
  153. } // namespace mindspore::serving
  154. #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.