1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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 GetTimeStr();
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, GetTimeStr().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 HILOG_DEBUG
159 #undef HILOG_DEBUG
160 #endif
161
162 #ifdef HILOG_INFO
163 #undef HILOG_INFO
164 #endif
165
166 #ifdef HILOG_WARN
167 #undef HILOG_WARN
168 #endif
169
170 #ifdef HILOG_ERROR
171 #undef HILOG_ERROR
172 #endif
173
174 #ifdef HILOG_PRINT
175 #undef HILOG_PRINT
176 #endif
177
178 #ifdef HAVE_HILOG
179 #define HILOG_PRINT(type, level, fmt, ...) \
180 HiLogPrint(type, level, LOG_DOMAIN, LOG_TAG, "%{public}s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
181 #else
182 #define HILOG_PRINT(type, level, fmt, ...) \
183 HiLogPrint(type, level, LOG_DOMAIN, LOG_TAG, "%s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
184 #endif
185
186 #define HILOG_DEBUG(type, fmt, ...) HILOG_PRINT(type, LOG_DEBUG, fmt, ##__VA_ARGS__)
187 #define HILOG_INFO(type, fmt, ...) HILOG_PRINT(type, LOG_INFO, fmt, ##__VA_ARGS__)
188 #define HILOG_WARN(type, fmt, ...) HILOG_PRINT(type, LOG_WARN, fmt, ##__VA_ARGS__)
189 #define HILOG_ERROR(type, fmt, ...) HILOG_PRINT(type, LOG_ERROR, fmt, ##__VA_ARGS__)
190 #endif // NDEBUG
191
192 #define STD_PTR(K, T) std::K##_ptr<T>
193
194 #define NO_RETVAL /* retval */
195
196 #define CHECK_NOTNULL(ptr, retval, fmt, ...) \
197 do { \
198 if (ptr == nullptr) { \
199 HILOG_WARN(LOG_CORE, "CHECK_NOTNULL(%s) in %s:%d FAILED, " fmt, #ptr, __func__, \
200 __LINE__, ##__VA_ARGS__); \
201 return retval; \
202 } \
203 } while (0)
204
205 #ifndef FUZZ_TEST
206 #define CHECK_TRUE(expr, retval, fmt, ...) \
207 do { \
208 if (!(expr)) { \
209 HILOG_WARN(LOG_CORE, "CHECK_TRUE(%s) in %s:%d FAILED, " fmt, #expr, __func__, __LINE__, ##__VA_ARGS__); \
210 return retval; \
211 } \
212 } while (0)
213 #else
214 #define CHECK_TRUE(expr, retval, fmt, ...) \
215 do { \
216 if (!(expr)) { \
217 return retval; \
218 } \
219 } while (0)
220 #endif // FUZZ_TEST
221
222 #define RETURN_IF(expr, retval, fmt, ...) \
223 do { \
224 if ((expr)) { \
225 HILOG_WARN(LOG_CORE, fmt, ##__VA_ARGS__); \
226 return retval; \
227 } \
228 } while (0)
229
230 #ifndef HAVE_HILOG
GetTimeStr()231 static std::string GetTimeStr()
232 {
233 char timeStr[64];
234 struct timespec ts;
235 struct tm tmStruct;
236 clock_gettime(CLOCK_REALTIME, &ts);
237 #if !is_mingw
238 localtime_r(&ts.tv_sec, &tmStruct);
239 #else
240 CHECK_TRUE(localtime_s(&tmStruct, &ts.tv_sec) == 0, "", "localtime_s FAILED!");
241 #endif
242 size_t used = strftime(timeStr, sizeof(timeStr), "%m-%d %H:%M:%S", &tmStruct);
243 (void)snprintf_s(&timeStr[used], sizeof(timeStr) - used, sizeof(timeStr) - used - 1, ".%03ld",
244 ts.tv_nsec / NS_PER_MS_LOG);
245 return timeStr;
246 }
247 #endif // !HAVE_HILOG
248 #endif // OHOS_PROFILER_LOGGING_H
249