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 LOG_DEFINED
17 #define LOG_DEFINED
18 
19 #ifdef USE_SKIA_TXT
20 #include <hilog/log.h>
21 
22 #undef LOG_DOMAIN
23 #define LOG_DOMAIN 0xD001408
24 #undef LOG_TAG
25 #define LOG_TAG "Text"
26 
27 #define TEXT_LOG_LIMIT_HOURS 3600
28 #define TEXT_LOG_LIMIT_MINUTE 60
29 #define TEXT_LOG_LIMIT_PRINT_FREQUENCY 3
30 
31 #define LOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
32 #define LOGI(fmt, ...) HILOG_INFO(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
33 #define LOGW(fmt, ...) HILOG_WARN(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
34 #define LOGE(fmt, ...) HILOG_ERROR(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
35 
36 #define TEXT_LOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
37 #define TEXT_LOGI(fmt, ...) HILOG_INFO(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
38 #define TEXT_LOGW(fmt, ...) HILOG_WARN(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
39 #define TEXT_LOGE(fmt, ...) HILOG_ERROR(LOG_CORE, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
40 
41 #define TEXT_PRINT_LIMIT(type, level, intervals, canPrint, frequency)                       \
42     do {                                                                                    \
43         static auto last = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>(); \
44         static uint32_t supressed = 0;                                                      \
45         static int printCount = 0;                                                          \
46         auto now = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now()); \
47         auto duration = now - last;                                                         \
48         if (duration.count() >= (intervals)) {                                              \
49             last = now;                                                                     \
50             uint32_t supressedCnt = supressed;                                              \
51             supressed = 0;                                                                  \
52             printCount = 1;                                                                 \
53             if (supressedCnt != 0) {                                                        \
54                 ((void)HILOG_IMPL((type), (level), LOG_DOMAIN, LOG_TAG,                     \
55                     "%{public}s log suppressed cnt %{public}u", __func__, supressedCnt));   \
56             }                                                                               \
57             (canPrint) = true;                                                              \
58         } else {                                                                            \
59             if ((printCount++) < (frequency)) {                                             \
60                 (canPrint) = true;                                                          \
61             } else {                                                                        \
62                 supressed++;                                                                \
63                 (canPrint) = false;                                                         \
64             }                                                                               \
65         }                                                                                   \
66     } while (0)
67 
68 #define TEXT_LOGI_LIMIT3_HOUR(fmt, ...)                                      \
69     do {                                                                     \
70         bool canPrint = true;                                                \
71         LOGD(fmt, ##__VA_ARGS__);                                            \
72         TEXT_PRINT_LIMIT(LOG_CORE, LOG_INFO, TEXT_LOG_LIMIT_HOURS, canPrint, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
73         if (canPrint) {                                                      \
74             LOGI(fmt, ##__VA_ARGS__);                                        \
75         }                                                                    \
76     } while (0)
77 
78 #define TEXT_LOGW_LIMIT3_HOUR(fmt, ...)                                      \
79     do {                                                                     \
80         bool canPrint = true;                                                \
81         LOGD(fmt, ##__VA_ARGS__);                                            \
82         TEXT_PRINT_LIMIT(LOG_CORE, LOG_WARN, TEXT_LOG_LIMIT_HOURS, canPrint, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
83         if (canPrint) {                                                      \
84             LOGW(fmt, ##__VA_ARGS__);                                        \
85         }                                                                    \
86     } while (0)
87 
88 #define TEXT_LOGE_LIMIT3_HOUR(fmt, ...)                                      \
89     do {                                                                     \
90         bool canPrint = true;                                                \
91         LOGD(fmt, ##__VA_ARGS__);                                            \
92         TEXT_PRINT_LIMIT(LOG_CORE, LOG_ERROR, TEXT_LOG_LIMIT_HOURS, canPrint, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
93         if (canPrint) {                                                      \
94             LOGE(fmt, ##__VA_ARGS__);                                        \
95         }                                                                    \
96     } while (0)
97 
98 #define TEXT_LOGI_LIMIT3_MIN(fmt, ...)                                       \
99     do {                                                                     \
100         bool canPrint = true;                                                \
101         LOGD(fmt, ##__VA_ARGS__);                                            \
102         TEXT_PRINT_LIMIT(LOG_CORE, LOG_INFO, TEXT_LOG_LIMIT_MINUTE, canPrint, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
103         if (canPrint) {                                                      \
104             LOGI(fmt, ##__VA_ARGS__);                                        \
105         }                                                                    \
106     } while (0)
107 
108 #define TEXT_LOGW_LIMIT3_MIN(fmt, ...)                                       \
109     do {                                                                     \
110         bool canPrint = true;                                                \
111         LOGD(fmt, ##__VA_ARGS__);                                            \
112         TEXT_PRINT_LIMIT(LOG_CORE, LOG_WARN, TEXT_LOG_LIMIT_MINUTE, canPrint, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
113         if (canPrint) {                                                      \
114             LOGW(fmt, ##__VA_ARGS__);                                        \
115         }                                                                    \
116     } while (0)
117 
118 #define TEXT_LOGE_LIMIT3_MIN(fmt, ...)                                       \
119     do {                                                                     \
120         bool canPrint = true;                                                \
121         LOGD(fmt, ##__VA_ARGS__);                                            \
122         TEXT_PRINT_LIMIT(LOG_CORE, LOG_ERROR, TEXT_LOG_LIMIT_MINUTE, canPrint, TEXT_LOG_LIMIT_PRINT_FREQUENCY); \
123         if (canPrint) {                                                      \
124             LOGE(fmt, ##__VA_ARGS__);                                        \
125         }                                                                    \
126     } while (0)
127 
128 #else
129 
130 #define LOGD(fmt, ...)
131 #define LOGI(fmt, ...)
132 #define LOGW(fmt, ...)
133 #define LOGE(fmt, ...)
134 
135 #define TEXT_LOGD(fmt, ...)
136 #define TEXT_LOGI(fmt, ...)
137 #define TEXT_LOGW(fmt, ...)
138 #define TEXT_LOGE(fmt, ...)
139 
140 #define TEXT_LOGI_LIMIT3_HOUR(fmt, ...)
141 #define TEXT_LOGW_LIMIT3_HOUR(fmt, ...)
142 #define TEXT_LOGE_LIMIT3_HOUR(fmt, ...)
143 
144 #define TEXT_LOGI_LIMIT3_MIN(fmt, ...)
145 #define TEXT_LOGW_LIMIT3_MIN(fmt, ...)
146 #define TEXT_LOGE_LIMIT3_MIN(fmt, ...)
147 
148 #endif // USE_SKIA_TXT
149 #endif // LOG_DEFINED
150