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 HIPERF_HILOG
17 #define HIPERF_HILOG
18
19 #include <securec.h>
20 #include <stdarg.h>
21
22 #ifndef CONFIG_NO_HILOG
23 #define HILOG_PUBLIC "{public}"
24 #define HILOG_NEWLINE ""
25 #else
26 #define HILOG_PUBLIC ""
27 #define HILOG_NEWLINE "\n"
28 #endif
29
30 #ifdef IS_RELEASE_VERSION
31 #define FORMATTED(fmt, ...) "[%" HILOG_PUBLIC "s]" fmt HILOG_NEWLINE, __FUNCTION__, ##__VA_ARGS__
32 #else
33 #define FORMATTED(fmt, ...) \
34 "[%" HILOG_PUBLIC "s:%" HILOG_PUBLIC "d] %" HILOG_PUBLIC "s# " fmt HILOG_NEWLINE, __FILE_NAME__, \
35 __LINE__, __FUNCTION__, ##__VA_ARGS__
36 #endif
37
38 #ifndef CONFIG_NO_HILOG
39 #include "hilog/log.h"
40
41 #ifdef HIPERF_HILOGF
42 #undef HIPERF_HILOGF
43 #endif
44
45 #ifdef HIPERF_HILOGE
46 #undef HIPERF_HILOGE
47 #endif
48
49 #ifdef HIPERF_HILOGW
50 #undef HIPERF_HILOGW
51 #endif
52
53 #ifdef HIPERF_HILOGI
54 #undef HIPERF_HILOGI
55 #endif
56
57 #ifdef HIPERF_HILOGD
58 #undef HIPERF_HILOGD
59 #endif
60
61 // param of log interface, such as HIPERF_HILOGF.
62 enum HiperfModule {
63 MODULE_DEFAULT = 0,
64 MODULE_JS_NAPI,
65 MODULE_CPP_API,
66 };
67
68 static constexpr unsigned int BASE_HIPERF_DOMAIN_ID = 0xD002D0D;
69 static constexpr OHOS::HiviewDFX::HiLogLabel HIPERF_HILOG_LABLE[] = {
70 {LOG_CORE, BASE_HIPERF_DOMAIN_ID, "hiperf"},
71 {LOG_CORE, BASE_HIPERF_DOMAIN_ID, "HiperfJSNAPI"},
72 {LOG_CORE, BASE_HIPERF_DOMAIN_ID, "HiperfCPPAPI"},
73 };
74
75 #ifdef LOG_DOMAIN
76 #undef LOG_DOMAIN
77 #endif
78 #define LOG_DOMAIN 0xD002D0D
79
80 #ifdef LOG_TAG
81 #undef LOG_TAG
82 #endif
83 #define LOG_TAG "hiperf"
84
85 // In order to improve performance, do not check the module range
86
87 #define HIPERF_HILOGF(module, ...) \
88 HILOG_FATAL(LOG_CORE, __VA_ARGS__)
89 #define HIPERF_HILOGE(module, ...) \
90 HILOG_ERROR(LOG_CORE, __VA_ARGS__)
91 #define HIPERF_HILOGW(module, ...) \
92 HILOG_WARN(LOG_CORE, __VA_ARGS__)
93 #define HIPERF_HILOGI(module, ...) \
94 HILOG_INFO(LOG_CORE, __VA_ARGS__)
95 #define HIPERF_HILOGD(module, ...) \
96 HILOG_DEBUG(LOG_CORE, __VA_ARGS__)
97 #else
98
99 #define HIPERF_HILOGF(module, ...) printf(FORMATTED(__VA_ARGS__))
100 #define HIPERF_HILOGE(module, ...) printf(FORMATTED(__VA_ARGS__))
101 #define HIPERF_HILOGW(module, ...) printf(FORMATTED(__VA_ARGS__))
102 #define HIPERF_HILOGI(module, ...) printf(FORMATTED(__VA_ARGS__))
103 #define HIPERF_HILOGD(module, ...) printf(FORMATTED(__VA_ARGS__))
104
105 #endif // CONFIG_NO_HILOG
106
StringFormat(const char* fmt, ...)107 static inline std::string StringFormat(const char* fmt, ...)
108 {
109 va_list vargs;
110 char buf[1024] = {0}; // 1024: buf size
111 std::string format(fmt);
112 va_start(vargs, fmt);
113 if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format.c_str(), vargs) < 0) {
114 va_end(vargs);
115 return "";
116 }
117
118 va_end(vargs);
119 return buf;
120 }
121
122 #define NO_RETVAL /* retval */
123 #define LOG_TYPE_PRINTF 2
124 #define LOG_TYPE_WITH_HILOG 3
125 #define CHECK_TRUE(expr, retval, log, fmt, ...) \
126 do { \
127 if (expr) { \
128 if (log == 1) { \
129 std::string str = StringFormat(fmt, ##__VA_ARGS__); \
130 HLOGE("%s", str.c_str()); \
131 } else if (log == LOG_TYPE_PRINTF) { \
132 printf("%s", StringFormat(fmt, ##__VA_ARGS__).c_str()); \
133 } else if (log == LOG_TYPE_WITH_HILOG) { \
134 std::string str = StringFormat(fmt, ##__VA_ARGS__); \
135 HLOGE("%s", str.c_str()); \
136 HIPERF_HILOGE(MODULE_DEFAULT, "%s", str.c_str()); \
137 } \
138 return retval; \
139 } \
140 } while (0)
141
142 #endif // HIPERF_HILOG
143