• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #undef NDEBUG
20 
21 #ifndef LOG_TAG
22 #define LOG_TAG "Hiprofiler"
23 #endif
24 
25 #define PROFILER_SUBSYSTEM 0x0000
26 #ifndef LOG_DOMAIN
27 #define LOG_DOMAIN PROFILER_SUBSYSTEM
28 #endif
29 
30 #ifndef UNUSED_PARAMETER
31 #define UNUSED_PARAMETER(x) ((void)x)
32 #endif
33 
34 #ifdef HAVE_HILOG
35 #include "hilog/log.h"
36 #include <string>
37 #else // HAVE_HILOG
38 #include <mutex>
39 #include <string>
40 #include <securec.h>
41 #include <stdarg.h>
42 #include <sys/syscall.h>
43 #include <time.h>
44 #include <unistd.h>
45 #include <vector>
46 
GetTid(void)47 static inline long GetTid(void)
48 {
49     return syscall(SYS_gettid);
50 }
51 
52 enum {
53     LOG_UNKNOWN = 0,
54     LOG_DEFAULT,
55     LOG_VERBOSE,
56     LOG_DEBUG,
57     LOG_INFO,
58     LOG_WARN,
59     LOG_ERROR,
60     LOG_FATAL,
61     LOG_SILENT,
62 };
63 
64 namespace {
65 constexpr int NS_PER_MS_LOG = 1000 * 1000;
66 }
67 
GetTimeStr()68 static inline std::string GetTimeStr()
69 {
70     char timeStr[64];
71     struct timespec ts;
72     struct tm tmStruct;
73     clock_gettime(CLOCK_REALTIME, &ts);
74     localtime_r(&ts.tv_sec, &tmStruct);
75     size_t used = strftime(timeStr, sizeof(timeStr), "%m-%d %H:%M:%S", &tmStruct);
76     snprintf_s(&timeStr[used], sizeof(timeStr) - used, sizeof(timeStr) - used - 1, ".%03ld",
77         ts.tv_nsec / NS_PER_MS_LOG);
78     return timeStr;
79 }
80 
81 typedef const char* ConstCharPtr;
82 
HiLogPrintArgs(int prio,int domain,ConstCharPtr tag,ConstCharPtr fmt,va_list vargs)83 static inline int HiLogPrintArgs(int prio, int domain, ConstCharPtr tag, ConstCharPtr fmt, va_list vargs)
84 {
85     static std::mutex mtx;
86     static std::vector<std::string> prioNames = {"U", " ", "V", "D", "I", "W", "E", "F", "S"};
87     std::unique_lock<std::mutex> lock(mtx);
88     int count = fprintf(stderr, "%04x %s %7d %7ld %5s %s ", domain, GetTimeStr().c_str(), getpid(), GetTid(),
89                         prioNames[prio].c_str(), tag);
90     if (count < 0) {
91         return 0;
92     }
93     count = count + vfprintf(stderr, fmt, vargs) + fprintf(stderr, "\n");
94     fflush(stderr);
95     return count;
96 }
97 
HiLogPrint(int type,int prio,int domain,ConstCharPtr tag,ConstCharPtr fmt,...)98 static inline int HiLogPrint(int type, int prio, int domain, ConstCharPtr tag, ConstCharPtr fmt, ...)
99 {
100     va_list vargs;
101     UNUSED_PARAMETER(type);
102     va_start(vargs, fmt);
103     int count = HiLogPrintArgs(prio, domain, tag, fmt, vargs);
104     va_end(vargs);
105     return count;
106 }
107 
108 #ifndef LOG_CORE
109 #define LOG_CORE 0
110 #endif
111 
112 #define HILOG_DEBUG(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
113 #define HILOG_INFO(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_INFO, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
114 #define HILOG_WARN(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_WARN, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
115 #define HILOG_ERROR(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_ERROR, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
116 
117 #endif // HAVE_HILOG
118 
119 #ifndef NDEBUG
120 #include <securec.h>
121 namespace logging {
StringReplace(std::string & str,const std::string & oldStr,const std::string & newStr)122 inline void StringReplace(std::string& str, const std::string& oldStr, const std::string& newStr)
123 {
124     std::string::size_type pos = 0u;
125     while ((pos = str.find(oldStr, pos)) != std::string::npos) {
126         str.replace(pos, oldStr.length(), newStr);
127         pos += newStr.length();
128     }
129 }
130 
131 // let compiler check format string and variable arguments
132 static inline std::string StringFormat(const char* fmt, ...)  __attribute__((format(printf, 1, 2)));
133 
StringFormat(const char * fmt,...)134 static inline std::string StringFormat(const char* fmt, ...)
135 {
136     va_list vargs;
137     char buf[1024] = {0};
138 
139     if (fmt == nullptr) {
140         return "";
141     }
142     std::string format(fmt);
143     StringReplace(format, "%{public}", "%");
144 
145     va_start(vargs, fmt);
146     if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format.c_str(), vargs) < 0) {
147         va_end(vargs);
148         return "";
149     }
150 
151     va_end(vargs);
152     return buf;
153 }
154 }  // logging
155 
156 #ifdef HILOG_DEBUG
157 #undef HILOG_DEBUG
158 #endif
159 
160 #ifdef HILOG_INFO
161 #undef HILOG_INFO
162 #endif
163 
164 #ifdef HILOG_WARN
165 #undef HILOG_WARN
166 #endif
167 
168 #ifdef HILOG_ERROR
169 #undef HILOG_ERROR
170 #endif
171 
172 #ifdef HILOG_PRINT
173 #undef HILOG_PRINT
174 #endif
175 
176 #ifdef HAVE_HILOG
177 #define HILOG_PRINT(type, level, fmt, ...) \
178     HiLogPrint(type, level, LOG_DOMAIN, LOG_TAG, "%{public}s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
179 #else
180 #define HILOG_PRINT(type, level, fmt, ...) \
181     HiLogPrint(type, level, LOG_DOMAIN, LOG_TAG, "%s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
182 #endif
183 
184 #define HILOG_DEBUG(type, fmt, ...) HILOG_PRINT(type, LOG_DEBUG, fmt, ##__VA_ARGS__)
185 #define HILOG_INFO(type, fmt, ...) HILOG_PRINT(type, LOG_INFO, fmt, ##__VA_ARGS__)
186 #define HILOG_WARN(type, fmt, ...) HILOG_PRINT(type, LOG_WARN, fmt, ##__VA_ARGS__)
187 #define HILOG_ERROR(type, fmt, ...) HILOG_PRINT(type, LOG_ERROR, fmt, ##__VA_ARGS__)
188 #endif  // NDEBUG
189 
190 #define STD_PTR(K, T) std::K##_ptr<T>
191 
192 #define NO_RETVAL /* retval */
193 
194 #define CHECK_NOTNULL(ptr, retval, fmt, ...)                                                                        \
195     do {                                                                                                            \
196         if (ptr == nullptr) {                                                                                       \
197             HILOG_WARN(LOG_CORE, "CHECK_NOTNULL(%s) in %s:%d FAILED, " fmt, #ptr, __func__, \
198                        __LINE__, ##__VA_ARGS__);                                                                    \
199             return retval;                                                                                          \
200         }                                                                                                           \
201     } while (0)
202 
203 #define CHECK_TRUE(expr, retval, fmt, ...)                                                                        \
204     do {                                                                                                          \
205         if (!(expr)) {                                                                                            \
206             HILOG_WARN(LOG_CORE, "CHECK_TRUE(%s) in %s:%d FAILED, " fmt, #expr, __func__, \
207                        __LINE__, ##__VA_ARGS__);                                                                  \
208             return retval;                                                                                        \
209         }                                                                                                         \
210     } while (0)
211 
212 #define RETURN_IF(expr, retval, fmt, ...)             \
213     do {                                              \
214         if ((expr)) {                                 \
215             HILOG_WARN(LOG_CORE, fmt, ##__VA_ARGS__); \
216             return retval;                            \
217         }                                             \
218     } while (0)
219 
220 #endif
221