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 FOUNDATION_EVENT_COMMON_LOG_INCLUDE_EVENT_LOG_WRAPPER_H
17 #define FOUNDATION_EVENT_COMMON_LOG_INCLUDE_EVENT_LOG_WRAPPER_H
18 
19 #include "hilog/log.h"
20 
21 #ifndef EVENT_LOG_DOMAIN
22 #define EVENT_LOG_DOMAIN 0xD001202
23 #endif
24 #ifndef EVENT_LOG_TAG
25 #define EVENT_LOG_TAG "Ces"
26 #endif
27 
28 #define EVENT_LOG_LIMIT_INTERVALS 10000 //ms
29 
30 #define CUR_FILE_NAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
31 
32 #define EVENT_LOGF(fmt, ...)            \
33     ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, EVENT_LOG_DOMAIN, EVENT_LOG_TAG, \
34     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILE_NAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
35 #define EVENT_LOGE(fmt, ...)            \
36     ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, EVENT_LOG_DOMAIN, EVENT_LOG_TAG, \
37     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILE_NAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
38 #define EVENT_LOGW(fmt, ...)            \
39     ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, EVENT_LOG_DOMAIN, EVENT_LOG_TAG, \
40     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILE_NAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
41 #define EVENT_LOGI(fmt, ...)            \
42     ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, EVENT_LOG_DOMAIN, EVENT_LOG_TAG, \
43     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILE_NAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
44 #define EVENT_LOGD(fmt, ...)            \
45     ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, EVENT_LOG_DOMAIN, EVENT_LOG_TAG, \
46     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILE_NAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
47 
48 
49 #define EVENT_PRINT_LIMIT(type, level, intervals, canPrint)                               \
50 do {                                                                                    \
51     static auto last = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>();   \
52     static uint32_t supressed = 0;                                                      \
53     auto now = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()); \
54     auto duration = now - last;                                                         \
55     if (duration.count() >= (intervals)) {                                              \
56         last = now;                                                                     \
57         uint32_t supressedCnt = supressed;                                              \
58         supressed = 0;                                                                  \
59         if (supressedCnt != 0) {                                                        \
60             ((void)HILOG_IMPL((type), (level), EVENT_LOG_DOMAIN, EVENT_LOG_TAG,    \
61             "[%{public}s(%{public}s:%{public}d)]log suppressed cnt %{public}u",         \
62             CUR_FILE_NAME, __FUNCTION__, __LINE__, supressedCnt));                       \
63         }                                                                               \
64         (canPrint) = true;                                                              \
65     } else {                                                                            \
66         supressed++;                                                                    \
67         (canPrint) = false;                                                             \
68     }                                                                                   \
69 } while (0)
70 
71 #define EVENT_LOGF_LIMIT(fmt, ...)                                        \
72 do {                                                                    \
73     bool can = true;                                                    \
74     EVENT_PRINT_LIMIT(LOG_CORE, LOG_FATAL, EVENT_LOG_LIMIT_INTERVALS, can); \
75     if (can) {                                                          \
76         EVENT_LOGF(fmt, ##__VA_ARGS__);                                   \
77     }                                                                   \
78 } while (0)
79 
80 #define EVENT_LOGE_LIMIT(fmt, ...)                                        \
81 do {                                                                    \
82     bool can = true;                                                    \
83     EVENT_PRINT_LIMIT(LOG_CORE, LOG_ERROR, EVENT_LOG_LIMIT_INTERVALS, can); \
84     if (can) {                                                          \
85         EVENT_LOGE(fmt, ##__VA_ARGS__);                                   \
86     }                                                                   \
87 } while (0)
88 
89 
90 #define EVENT_LOGW_LIMIT(fmt, ...)                                        \
91 do {                                                                    \
92     bool can = true;                                                    \
93     EVENT_PRINT_LIMIT(LOG_CORE, LOG_WARN, EVENT_LOG_LIMIT_INTERVALS, can);  \
94     if (can) {                                                          \
95         EVENT_LOGW(fmt, ##__VA_ARGS__);                                   \
96     }                                                                   \
97 } while (0)
98 
99 
100 #define EVENT_LOGI_LIMIT(fmt, ...)                                        \
101 do {                                                                    \
102     bool can = true;                                                    \
103     EVENT_PRINT_LIMIT(LOG_CORE, LOG_INFO, EVENT_LOG_LIMIT_INTERVALS, can);  \
104     if (can) {                                                          \
105         EVENT_LOGI(fmt, ##__VA_ARGS__);                                   \
106     }                                                                   \
107 } while (0)
108 
109 #define EVENT_LOGD_LIMIT(fmt, ...)                                        \
110 do {                                                                    \
111     bool can = true;                                                    \
112     EVENT_PRINT_LIMIT(LOG_CORE, LOG_DEBUG, EVENT_LOG_LIMIT_INTERVALS, can); \
113     if (can) {                                                          \
114         EVENT_LOGD(fmt, ##__VA_ARGS__);                                   \
115     }                                                                   \
116 } while (0)
117 #endif  // FOUNDATION_EVENT_COMMON_LOG_INCLUDE_EVENT_LOG_WRAPPER_H
118