1 /**
2 * Copyright 2019-2021 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_LITE_SRC_COMMON_LOG_H_
17 #define MINDSPORE_LITE_SRC_COMMON_LOG_H_
18
19 #include <memory>
20 #include <sstream>
21 #include <vector>
22 #include <map>
23 #include <unordered_map>
24 #include "utils/overload.h"
25
26 // NOTICE: when relative path of 'log.h' changed, macro 'LITE_LOG_HEAR_FILE_REL_PATH' must be changed
27 #ifndef LITE_LOG_HEAR_FILE_REL_PATH
28 #define LITE_LOG_HEAR_FILE_REL_PATH "mindspore/lite/src/common/log.h"
29 #endif
30
31 // Get start index of file relative path in __FILE__
GetRealPathPos()32 static constexpr size_t GetRealPathPos() noexcept {
33 return sizeof(__FILE__) > sizeof(LITE_LOG_HEAR_FILE_REL_PATH) ? sizeof(__FILE__) - sizeof(LITE_LOG_HEAR_FILE_REL_PATH)
34 : 0;
35 }
36
37 namespace mindspore {
38 #define LITE_FILE_NAME \
39 (sizeof(__FILE__) > GetRealPathPos() ? static_cast<const char *>(__FILE__) + GetRealPathPos() \
40 : static_cast<const char *>(__FILE__))
41
42 struct LiteLocationInfo {
LiteLocationInfoLiteLocationInfo43 LiteLocationInfo(const char *file, int line, const char *func) : file_(file), line_(line), func_(func) {}
44
45 ~LiteLocationInfo() = default;
46
47 const char *file_;
48 int line_;
49 const char *func_;
50 };
51
52 class LiteLogStream {
53 public:
LiteLogStream()54 LiteLogStream() { sstream_ = std::make_shared<std::stringstream>(); }
55
56 ~LiteLogStream() = default;
57
58 template <typename T>
59 LiteLogStream &operator<<(const T &val) noexcept {
60 (*sstream_) << val;
61 return *this;
62 }
63
64 template <typename T>
65 LiteLogStream &operator<<(const std::vector<T> &val) noexcept {
66 (*sstream_) << "[";
67 for (size_t i = 0; i < val.size(); i++) {
68 (*this) << val[i];
69 if (i + 1 < val.size()) {
70 (*sstream_) << ", ";
71 }
72 }
73 (*sstream_) << "]";
74 return *this;
75 }
76
77 template <typename K, typename V>
78 LiteLogStream &operator<<(const std::unordered_map<K, V> &val) noexcept {
79 (*sstream_) << "{";
80 size_t index = 0;
81 for (auto &item : val) {
82 (*this) << item.first << ": " << item.second;
83 if (index + 1 < val.size()) {
84 (*sstream_) << ", ";
85 }
86 }
87 (*sstream_) << "}";
88 return *this;
89 }
90
91 template <typename K, typename V>
92 LiteLogStream &operator<<(const std::map<K, V> &val) noexcept {
93 (*sstream_) << "{";
94 size_t index = 0;
95 for (auto &item : val) {
96 (*this) << item.first << ": " << item.second;
97 if (index + 1 < val.size()) {
98 (*sstream_) << ", ";
99 }
100 }
101 (*sstream_) << "}";
102 return *this;
103 }
104
105 LiteLogStream &operator<<(std::ostream &func(std::ostream &os)) noexcept {
106 (*sstream_) << func;
107 return *this;
108 }
109 friend class LiteLogWriter;
110
111 private:
112 std::shared_ptr<std::stringstream> sstream_;
113 };
114
115 enum class LiteLogLevel : int { DEBUG = 0, INFO, WARNING, ERROR };
116
117 class LiteLogWriter {
118 public:
LiteLogWriter(const LiteLocationInfo & location,mindspore::LiteLogLevel log_level)119 LiteLogWriter(const LiteLocationInfo &location, mindspore::LiteLogLevel log_level)
120 : location_(location), log_level_(log_level) {}
121
122 ~LiteLogWriter() = default;
123
124 #ifdef _WIN32
125 __declspec(dllexport) void operator<(const LiteLogStream &stream) const noexcept;
126 #else
127 __attribute__((visibility("default"))) void operator<(const LiteLogStream &stream) const noexcept;
128 #endif
129
130 private:
131 void OutputLog(const std::ostringstream &msg) const;
132
133 LiteLocationInfo location_;
134 LiteLogLevel log_level_;
135 };
136
137 #define MSLOG_IF(level) \
138 mindspore::LiteLogWriter(mindspore::LiteLocationInfo(LITE_FILE_NAME, __LINE__, __FUNCTION__), level) < \
139 mindspore::LiteLogStream()
140
141 #define MS_LOG(level) MS_LOG_##level
142
143 #define MS_LOG_DEBUG MSLOG_IF(mindspore::LiteLogLevel::DEBUG)
144 #define MS_LOG_INFO MSLOG_IF(mindspore::LiteLogLevel::INFO)
145 #define MS_LOG_WARNING MSLOG_IF(mindspore::LiteLogLevel::WARNING)
146 #define MS_LOG_ERROR MSLOG_IF(mindspore::LiteLogLevel::ERROR)
147
148 } // namespace mindspore
149
150 #ifdef Debug
151 #include <cassert>
152 #define MS_ASSERT(f) assert(f)
153 #else
154 #define MS_ASSERT(f) ((void)0)
155 #endif
156 #endif // MINDSPORE_LITE_SRC_COMMON_LOG_H_
157