1 /*
2 * Copyright (c) 2024 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 TEXT_LOG_H
17 #define TEXT_LOG_H
18
19 #ifdef OHOS_TEXT_ENABLE
20 #include <hilog/log.h>
21 #endif
22
23 template<typename... Args>
ignore_unused(Args &&...)24 void ignore_unused(Args&&...)
25 {}
26 namespace OHOS {
27 namespace Rosen {
28 #ifdef OHOS_TEXT_ENABLE
29
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD001408
32
33 #undef LOG_TAG
34 #define LOG_TAG "Text"
35
36 #define TEXT_LOG_LIMIT_HOURS 3600
37 #define TEXT_LOG_LIMIT_MINUTE 60
38 #define TEXT_LOG_LIMIT_PRINT_FREQUENCY 3
39
40 #define TEXT_LOGD(fmt, ...) \
41 HILOG_DEBUG(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
42 #define TEXT_LOGI(fmt, ...) \
43 HILOG_INFO(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
44 #define TEXT_LOGW(fmt, ...) \
45 HILOG_WARN(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
46 #define TEXT_LOGE(fmt, ...) \
47 HILOG_ERROR(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
48
49 #define TEXT_PRINT_LIMIT(type, level, intervals, canPrint, frequency) \
50 do { \
51 static auto last = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>(); \
52 static uint32_t supressed = 0; \
53 static int printCount = 0; \
54 auto now = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now()); \
55 auto duration = now - last; \
56 if (duration.count() >= (intervals)) { \
57 last = now; \
58 uint32_t supressedCnt = supressed; \
59 supressed = 0; \
60 printCount = 1; \
61 if (supressedCnt != 0) { \
62 ((void)HILOG_IMPL((type), (level), LOG_DOMAIN, LOG_TAG, \
63 "%{public}s log suppressed cnt %{public}u", __func__, supressedCnt)); \
64 } \
65 (canPrint) = true; \
66 } else { \
67 if ((printCount++) < (frequency)) { \
68 (canPrint) = true; \
69 } else { \
70 supressed++; \
71 (canPrint) = false; \
72 } \
73 } \
74 } while (0)
75
76 #define TEXT_LOGI_LIMIT3_HOUR(fmt, ...) \
77 do { \
78 bool can = true; \
79 TEXT_LOGD(fmt, ##__VA_ARGS__); \
80 TEXT_PRINT_LIMIT(LOG_CORE, LOG_INFO, TEXT_LOG_LIMIT_HOURS, can, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
81 if (can) { \
82 TEXT_LOGI(fmt, ##__VA_ARGS__); \
83 } \
84 } while (0)
85
86 #define TEXT_LOGW_LIMIT3_HOUR(fmt, ...) \
87 do { \
88 bool can = true; \
89 TEXT_LOGD(fmt, ##__VA_ARGS__); \
90 TEXT_PRINT_LIMIT(LOG_CORE, LOG_WARN, TEXT_LOG_LIMIT_HOURS, can, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
91 if (can) { \
92 TEXT_LOGW(fmt, ##__VA_ARGS__); \
93 } \
94 } while (0)
95
96 #define TEXT_LOGE_LIMIT3_HOUR(fmt, ...) \
97 do { \
98 bool can = true; \
99 TEXT_LOGD(fmt, ##__VA_ARGS__); \
100 TEXT_PRINT_LIMIT(LOG_CORE, LOG_ERROR, TEXT_LOG_LIMIT_HOURS, can, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
101 if (can) { \
102 TEXT_LOGE(fmt, ##__VA_ARGS__); \
103 } \
104 } while (0)
105
106 #define TEXT_LOGI_LIMIT3_MIN(fmt, ...) \
107 do { \
108 bool can = true; \
109 TEXT_LOGD(fmt, ##__VA_ARGS__); \
110 TEXT_PRINT_LIMIT(LOG_CORE, LOG_INFO, TEXT_LOG_LIMIT_MINUTE, can, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
111 if (can) { \
112 TEXT_LOGI(fmt, ##__VA_ARGS__); \
113 } \
114 } while (0)
115
116 #define TEXT_LOGW_LIMIT3_MIN(fmt, ...) \
117 do { \
118 bool can = true; \
119 TEXT_LOGD(fmt, ##__VA_ARGS__); \
120 TEXT_PRINT_LIMIT(LOG_CORE, LOG_WARN, TEXT_LOG_LIMIT_MINUTE, can, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
121 if (can) { \
122 TEXT_LOGW(fmt, ##__VA_ARGS__); \
123 } \
124 } while (0)
125
126 #define TEXT_LOGE_LIMIT3_MIN(fmt, ...) \
127 do { \
128 bool can = true; \
129 TEXT_LOGD(fmt, ##__VA_ARGS__); \
130 TEXT_PRINT_LIMIT(LOG_CORE, LOG_ERROR, TEXT_LOG_LIMIT_MINUTE, can, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
131 if (can) { \
132 TEXT_LOGE(fmt, ##__VA_ARGS__); \
133 } \
134 } while (0)
135
136 #else
137 #define TEXT_LOGD(...) ignore_unused(__VA_ARGS__)
138 #define TEXT_LOGI(...) ignore_unused(__VA_ARGS__)
139 #define TEXT_LOGW(...) ignore_unused(__VA_ARGS__)
140 #define TEXT_LOGE(...) ignore_unused(__VA_ARGS__)
141
142 #define TEXT_LOGI_LIMIT3_HOUR(...) ignore_unused(__VA_ARGS__)
143 #define TEXT_LOGW_LIMIT3_HOUR(...) ignore_unused(__VA_ARGS__)
144 #define TEXT_LOGE_LIMIT3_HOUR(...) ignore_unused(__VA_ARGS__)
145
146 #define TEXT_LOGI_LIMIT3_MIN(...) ignore_unused(__VA_ARGS__)
147 #define TEXT_LOGW_LIMIT3_MIN(...) ignore_unused(__VA_ARGS__)
148 #define TEXT_LOGE_LIMIT3_MIN(...) ignore_unused(__VA_ARGS__)
149
150 #endif
151
152 #define TEXT_ERROR_CHECK(ret, statement, format, ...) \
153 do { \
154 if (!(ret)) { \
155 TEXT_LOGE(format, ##__VA_ARGS__); \
156 statement; \
157 } \
158 } while (0)
159
160 #define TEXT_INFO_CHECK(ret, statement, format, ...) \
161 do { \
162 if (!(ret)) { \
163 TEXT_LOGI(format, ##__VA_ARGS__); \
164 statement; \
165 } \
166 } while (0)
167
168 #define TEXT_CHECK(ret, statement) \
169 do { \
170 if (!(ret)) { \
171 statement; \
172 } \
173 } while (0)
174
175 #define TEXT_CHECK_RETURN_VALUE(ret, result) \
176 do { \
177 if (!(ret)) { \
178 return result; \
179 } \
180 } while (0)
181
182
183 } // namespace Rosen
184 } // namespace OHOS
185 #endif
186