1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef OHOS_PROFILER_LOGGING_H
17 #define OHOS_PROFILER_LOGGING_H
18
19 #define EXPORT_API __attribute__((visibility("default")))
20
21 #undef NDEBUG
22
23 #ifndef LOG_TAG
24 #define LOG_TAG "Hiprofiler"
25 #endif
26
27 #define PROFILER_SUBSYSTEM 0xD002D0C
28 #ifndef LOG_DOMAIN
29 #define LOG_DOMAIN PROFILER_SUBSYSTEM
30 #endif
31
32 #ifndef UNUSED_PARAMETER
33 #define UNUSED_PARAMETER(x) ((void)(x))
34 #endif
35
36 #ifdef HAVE_HILOG
37 #include "hilog/log.h"
38 #include <string>
39 #else // HAVE_HILOG
40 #include <mutex>
41 #include <string>
42 #include <securec.h>
43 #include <stdarg.h>
44 #if !is_mingw
45 #include <sys/syscall.h>
46 #undef getsystid
47 #define getsystid() syscall(SYS_gettid)
48 #else
49 #include "windows.h"
getsystid()50 inline long getsystid()
51 {
52 return GetCurrentThreadId();
53 }
54 #endif
55
56 #include <ctime>
57 #include <vector>
58 #include <unistd.h>
59
GetTid(void)60 static inline long GetTid(void)
61 {
62 return getsystid();
63 }
64
65 enum {
66 LOG_UNKNOWN = 0,
67 LOG_DEFAULT,
68 LOG_VERBOSE,
69 LOG_DEBUG,
70 LOG_INFO,
71 LOG_WARN,
72 LOG_ERROR,
73 LOG_FATAL,
74 LOG_SILENT,
75 };
76
77 namespace {
78 constexpr int NS_PER_MS_LOG = 1000 * 1000;
79 }
80
81 static inline std::string GetTimeString();
82
83 typedef const char* ConstCharPtr;
84
HiLogPrintArgs(int prio,int domain,ConstCharPtr tag,ConstCharPtr fmt,va_list vargs)85 static inline int HiLogPrintArgs(int prio, int domain, ConstCharPtr tag, ConstCharPtr fmt, va_list vargs)
86 {
87 static std::mutex mtx;
88 static std::vector<std::string> prioNames = {"U", " ", "V", "D", "I", "W", "E", "F", "S"};
89 std::unique_lock<std::mutex> lock(mtx);
90 int count = fprintf(stderr, "%04x %s %7d %7ld %5s %s ", domain, GetTimeString().c_str(), getpid(), GetTid(),
91 prioNames[prio].c_str(), tag);
92 if (count < 0) {
93 return 0;
94 }
95 count = count + vfprintf(stderr, fmt, vargs) + fprintf(stderr, "\n");
96 fflush(stderr);
97 return count;
98 }
99
HiLogPrint(int type,int prio,int domain,ConstCharPtr tag,ConstCharPtr fmt,...)100 static inline int HiLogPrint(int type, int prio, int domain, ConstCharPtr tag, ConstCharPtr fmt, ...)
101 {
102 va_list vargs;
103 UNUSED_PARAMETER(type);
104 va_start(vargs, fmt);
105 int count = HiLogPrintArgs(prio, domain, tag, fmt, vargs);
106 va_end(vargs);
107 return count;
108 }
109
110 #ifndef LOG_CORE
111 #define LOG_CORE 0
112 #endif
113
114 #define HILOG_DEBUG(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
115 #define HILOG_INFO(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_INFO, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
116 #define HILOG_WARN(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_WARN, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
117 #define HILOG_ERROR(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_ERROR, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
118
119 #endif // HAVE_HILOG
120
121 #ifndef NDEBUG
122 #include <securec.h>
123 namespace logging {
StringReplace(std::string & str,const std::string & oldStr,const std::string & newStr)124 inline void StringReplace(std::string& str, const std::string& oldStr, const std::string& newStr)
125 {
126 std::string::size_type pos = 0u;
127 while ((pos = str.find(oldStr, pos)) != std::string::npos) {
128 str.replace(pos, oldStr.length(), newStr);
129 pos += newStr.length();
130 }
131 }
132
133 // let compiler check format string and variable arguments
134 static inline std::string StringFormat(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
135
StringFormat(const char * fmt,...)136 static inline std::string StringFormat(const char* fmt, ...)
137 {
138 va_list vargs;
139 char buf[1024] = {0};
140
141 if (fmt == nullptr) {
142 return "";
143 }
144 std::string format(fmt);
145 StringReplace(format, "%{public}", "%");
146
147 va_start(vargs, fmt);
148 if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format.c_str(), vargs) < 0) {
149 va_end(vargs);
150 return "";
151 }
152
153 va_end(vargs);
154 return buf;
155 }
156 } // logging
157
158 #ifdef HAVE_HILOG
159 #define HILOG_PRINT_DEBUG(type, fmt, ...) \
160 HILOG_DEBUG(type, "%{public}s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
161 #define HILOG_PRINT_INFO(type, fmt, ...) \
162 HILOG_INFO(type, "%{public}s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
163 #define HILOG_PRINT_WARN(type, fmt, ...) \
164 HILOG_WARN(type, "%{public}s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
165 #define HILOG_PRINT_ERROR(type, fmt, ...) \
166 HILOG_ERROR(type, "%{public}s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
167 #else
168 #define HILOG_PRINT_DEBUG(type, fmt, ...) \
169 HiLogPrint(type, LOG_DEBUG, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
170 #define HILOG_PRINT_INFO(type, fmt, ...) \
171 HiLogPrint(type, LOG_INFO, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
172 #define HILOG_PRINT_WARN(type, fmt, ...) \
173 HiLogPrint(type, LOG_WARN, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
174 #define HILOG_PRINT_ERROR(type, fmt, ...) \
175 HiLogPrint(type, LOG_ERROR, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
176 #endif
177
178 #define PROFILER_LOG_DEBUG(type, fmt, ...) HILOG_PRINT_DEBUG(type, fmt, ##__VA_ARGS__)
179 #define PROFILER_LOG_INFO(type, fmt, ...) HILOG_PRINT_INFO(type, fmt, ##__VA_ARGS__)
180 #define PROFILER_LOG_WARN(type, fmt, ...) HILOG_PRINT_WARN(type, fmt, ##__VA_ARGS__)
181 #define PROFILER_LOG_ERROR(type, fmt, ...) HILOG_PRINT_ERROR(type, fmt, ##__VA_ARGS__)
182 #endif // NDEBUG
183
184 #define STD_PTR(K, T) std::K##_ptr<T>
185
186 #define NO_RETVAL /* retval */
187
188 #define CHECK_NOTNULL(ptr, retval, fmt, ...) \
189 do { \
190 if (ptr == nullptr) { \
191 std::string str = std::string("CHECK_NOTNULL(") + logging::StringFormat(fmt, ##__VA_ARGS__) + \
192 ") in " + __func__ + ":" + std::to_string(__LINE__) + "FAILED"; \
193 HILOG_WARN(LOG_CORE, "%{public}s", str.c_str()); \
194 return retval; \
195 } \
196 } while (0)
197
198 #ifndef FUZZ_TEST
199 #define CHECK_TRUE(expr, retval, fmt, ...) \
200 do { \
201 if (!(expr)) { \
202 std::string str = std::string("CHECK_TRUE(") + logging::StringFormat(fmt, ##__VA_ARGS__) + \
203 ") in " + __func__ + ":" + std::to_string(__LINE__) + "FAILED"; \
204 HILOG_WARN(LOG_CORE, "%{public}s", str.c_str()); \
205 return retval; \
206 } \
207 } while (0)
208 #else
209 #define CHECK_TRUE(expr, retval, fmt, ...) \
210 do { \
211 if (!(expr)) { \
212 return retval; \
213 } \
214 } while (0)
215 #endif // FUZZ_TEST
216
217 #define RETURN_IF(expr, retval, fmt, ...) \
218 do { \
219 if ((expr)) { \
220 HILOG_WARN(LOG_CORE, "%{public}s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str()); \
221 return retval; \
222 } \
223 } while (0)
224
225 #ifndef HAVE_HILOG
GetTimeString()226 static std::string GetTimeString()
227 {
228 char timeStr[64];
229 struct timespec ts;
230 struct tm tmStruct;
231 clock_gettime(CLOCK_REALTIME, &ts);
232 #if !is_mingw
233 localtime_r(&ts.tv_sec, &tmStruct);
234 #else
235 CHECK_TRUE(localtime_s(&tmStruct, &ts.tv_sec) == 0, "", "localtime_s FAILED!");
236 #endif
237 size_t used = strftime(timeStr, sizeof(timeStr), "%m-%d %H:%M:%S", &tmStruct);
238 (void)snprintf_s(&timeStr[used], sizeof(timeStr) - used, sizeof(timeStr) - used - 1, ".%03ld",
239 ts.tv_nsec / NS_PER_MS_LOG);
240 return timeStr;
241 }
242 #endif // !HAVE_HILOG
243 #endif // OHOS_PROFILER_LOGGING_H
244