• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "utils/overload.h"
22 
23 #ifdef MS_COMPILE_OHOS
24 #include "hilog/log.h"
25 #endif
26 // NOTICE: when relative path of 'log.h' changed, macro 'LITE_LOG_HEAR_FILE_REL_PATH' must be changed
27 #define LITE_LOG_HEAR_FILE_REL_PATH "mindspore/lite/src/common/log.h"
28 
29 // Get start index of file relative path in __FILE__
GetRealPathPos()30 static constexpr size_t GetRealPathPos() noexcept {
31   return sizeof(__FILE__) > sizeof(LITE_LOG_HEAR_FILE_REL_PATH) ? sizeof(__FILE__) - sizeof(LITE_LOG_HEAR_FILE_REL_PATH)
32                                                                 : 0;
33 }
34 
35 namespace mindspore {
36 #define LITE_FILE_NAME                                                                          \
37   (sizeof(__FILE__) > GetRealPathPos() ? static_cast<const char *>(__FILE__) + GetRealPathPos() \
38                                        : static_cast<const char *>(__FILE__))
39 
40 struct LiteLocationInfo {
LiteLocationInfoLiteLocationInfo41   LiteLocationInfo(const char *file, int line, const char *func) : file_(file), line_(line), func_(func) {}
42 
43   ~LiteLocationInfo() = default;
44 
45   const char *file_;
46   int line_;
47   const char *func_;
48 };
49 
50 class LiteLogStream {
51  public:
LiteLogStream()52   LiteLogStream() { sstream_ = std::make_shared<std::stringstream>(); }
53 
54   ~LiteLogStream() = default;
55 
56   template <typename T>
57   LiteLogStream &operator<<(const T &val) noexcept {
58     (*sstream_) << val;
59     return *this;
60   }
61 
62   LiteLogStream &operator<<(std::ostream &func(std::ostream &os)) noexcept {
63     (*sstream_) << func;
64     return *this;
65   }
66   friend class LiteLogWriter;
67 
68  private:
69   std::shared_ptr<std::stringstream> sstream_;
70 };
71 
72 enum class LiteLogLevel : int { DEBUG = 0, INFO, WARNING, ERROR };
73 
74 class LiteLogWriter {
75  public:
LiteLogWriter(const LiteLocationInfo & location,mindspore::LiteLogLevel log_level)76   LiteLogWriter(const LiteLocationInfo &location, mindspore::LiteLogLevel log_level)
77       : location_(location), log_level_(log_level) {}
78 
79   ~LiteLogWriter() = default;
80 
81 #ifdef _WIN32
82   __declspec(dllexport) void operator<(const LiteLogStream &stream) const noexcept;
83 #else
84   __attribute__((visibility("default"))) void operator<(const LiteLogStream &stream) const noexcept;
85 #endif
86 
87  private:
88   void OutputLog(const std::ostringstream &msg) const;
89 
90   LiteLocationInfo location_;
91   LiteLogLevel log_level_;
92 };
93 
94 #define MS_LOG(level) MS_LOG_##level
95 
96 #define MS_LOG_DEBUG MSLOG_IF(mindspore::LiteLogLevel::DEBUG)
97 #define MS_LOG_INFO MSLOG_IF(mindspore::LiteLogLevel::INFO)
98 #define MS_LOG_WARNING MSLOG_IF(mindspore::LiteLogLevel::WARNING)
99 #define MS_LOG_ERROR MSLOG_IF(mindspore::LiteLogLevel::ERROR)
100 
101 #ifdef MS_COMPILE_OHOS
102 namespace {
103 constexpr unsigned int MSLITE_DOMAIN_ID_START = 0xD0029A0;
104 constexpr unsigned int MSLITE_DOMAIN_ID_END = MSLITE_DOMAIN_ID_START + 32;
105 constexpr unsigned int TEST_DOMAIN_ID = 0xD000F00;
106 }  // namespace
107 
108 #define FILE_NAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
109 #define FORMAT "[%{public}s:%{public}d] %{public}s# %{public}s"
110 
111 #define MSLOG_IF(level)                                                                                  \
112   mindspore::LiteLogWriter(mindspore::LiteLocationInfo(LITE_FILE_NAME, __LINE__, __FUNCTION__), level) < \
113     mindspore::LiteLogStream()
114 
115 enum MSLiteManagerLogLabel {
116   // Component labels, you can add if needed
117   COMP_FWK = 0,
118   // Test label
119   LABEL_TEST,
120   // The end of labels, max to the domain id range length 32
121   LABEL_END,
122 };
123 
124 enum MSLiteManagerLogDomain {
125   DOMAIN_FRAMEWORK = MSLITE_DOMAIN_ID_START + COMP_FWK,  // 0xD0029A0
126   DOMAIN_TEST = TEST_DOMAIN_ID,                          // 0xD000F00
127   DOMAIN_END = MSLITE_DOMAIN_ID_END,  // Max to 0xD002940, keep the sequence and length same as MSLiteManagerLogLabel
128 };
129 
130 // Keep the sequence and length same as MSLiteManagerLogDomain
131 static constexpr OHOS::HiviewDFX::HiLogLabel MSLite_LABEL = {LOG_CORE, DOMAIN_FRAMEWORK, "MSLiteFwk"};
132 
133 #else
134 
135 #define MSLOG_IF(level)                                                                                  \
136   mindspore::LiteLogWriter(mindspore::LiteLocationInfo(LITE_FILE_NAME, __LINE__, __FUNCTION__), level) < \
137     mindspore::LiteLogStream()
138 
139 #endif
140 
141 }  // namespace mindspore
142 
143 #ifdef Debug
144 #include <cassert>
145 #define MS_ASSERT(f) assert(f)
146 #else
147 #define MS_ASSERT(f) ((void)0)
148 #endif
149 #endif  // MINDSPORE_LITE_SRC_COMMON_LOG_H_
150