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

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