1 /*
2 * Copyright (c) 2020 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 #include "ohos_init.h"
17 #include "hiview_def.h"
18 #include "hiview_util.h"
19 #include "hiview_config.h"
20 #include "hiview_log.h"
21 #include "hiview_log_limit.h"
22
23 #define LOG_LIMIT_CHECK_DURATION 60 /* seconds */
24 #define LOG_LIMIT_LEVEL1 6 /* Maximum number of log records in a check period. */
25 #define LOG_LIMIT_LEVEL2 30
26 #define LOG_LIMIT_LEVEL3 60
27 #define LOG_LIMIT_LEVEL4 120
28 #ifndef LOG_LIMIT_DEFAULT
29 #define LOG_LIMIT_DEFAULT LOG_LIMIT_LEVEL2
30 #endif
31
32 static HiLogLimitRule *g_hilogLimitList = NULL;
33
InitLogLimit(void)34 void InitLogLimit(void)
35 {
36 if (g_hilogLimitList == NULL) {
37 g_hilogLimitList = (HiLogLimitRule *)HIVIEW_MemAlloc(
38 MEM_POOL_HIVIEW_ID, sizeof(HiLogLimitRule) * HILOG_MODULE_MAX);
39 if (g_hilogLimitList == NULL) {
40 return;
41 }
42 }
43
44 uint16 i;
45 HiLogLimitRule *pRule = NULL;
46 for (i = 0; i < HILOG_MODULE_MAX; i++) {
47 pRule = g_hilogLimitList + i;
48 pRule->maxNum = LOG_LIMIT_DEFAULT;
49 pRule->logNum = 0;
50 pRule->baseTime = 0;
51 pRule->totalLogNum = 0;
52 pRule->totalDropLogNum = 0;
53 }
54 SetLimitThreshold(HILOG_MODULE_HIVIEW, LOG_LIMIT_LEVEL3);
55 SetLimitThreshold(HILOG_MODULE_APP, LOG_LIMIT_LEVEL2);
56 HILOG_DEBUG(HILOG_MODULE_HIVIEW, "log limit init success.");
57 }
58
LogIsLimited(uint8 module)59 boolean LogIsLimited(uint8 module)
60 {
61 if (GETOPTION(g_hiviewConfig.outputOption) == OUTPUT_OPTION_DEBUG) {
62 return FALSE;
63 }
64 /* covert ms to sec by dividing 1000, and integer overflow can be accepted */
65 uint16 curTime = (uint16)(HIVIEW_GetCurrentTime() / MS_PER_SECOND);
66 uint8 logNum;
67 uint16 baseTime;
68 HiLogLimitRule *pLimitRule = NULL;
69
70 if (module >= HILOG_MODULE_MAX) {
71 return TRUE;
72 }
73 if (g_hilogLimitList == NULL) {
74 return FALSE;
75 }
76
77 pLimitRule = g_hilogLimitList + module;
78 if (pLimitRule->baseTime == 0) {
79 pLimitRule->baseTime = (uint16)(HIVIEW_GetCurrentTime() / MS_PER_SECOND);
80 }
81 pLimitRule->logNum++;
82 pLimitRule->totalLogNum++;
83 logNum = pLimitRule->logNum;
84 baseTime = pLimitRule->baseTime;
85 if ((curTime < baseTime) || ((curTime - baseTime) >= LOG_LIMIT_CHECK_DURATION)) {
86 pLimitRule->baseTime = curTime;
87 pLimitRule->logNum = 0;
88 if (logNum > pLimitRule->maxNum) {
89 HILOG_WARN(module, "log limit, log lines:%u, drop lines:%u, total log lines:%u, total drop lines:%u",
90 logNum, logNum - pLimitRule->maxNum, pLimitRule->totalLogNum, pLimitRule->totalDropLogNum);
91 }
92 } else {
93 if (logNum > pLimitRule->maxNum) {
94 pLimitRule->totalDropLogNum++;
95 return TRUE;
96 }
97 }
98
99 return FALSE;
100 }
101
SetLimitThreshold(uint8 module,uint8 v)102 void SetLimitThreshold(uint8 module, uint8 v)
103 {
104 if (g_hilogLimitList == NULL) {
105 return;
106 }
107
108 HiLogLimitRule *pRule = g_hilogLimitList + module;
109 pRule->maxNum = v;
110 pRule->logNum = 0;
111 pRule->baseTime = 0;
112 pRule->totalLogNum = 0;
113 pRule->totalDropLogNum = 0;
114 }
115
GetLogLimitRule(uint8 module)116 const HiLogLimitRule *GetLogLimitRule(uint8 module)
117 {
118 if (module >= HILOG_MODULE_MAX || g_hilogLimitList == NULL) {
119 return NULL;
120 }
121 return g_hilogLimitList + module;
122 }
123