• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package ohos.devtools.views.hilog;
17 
18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 
22 import javax.swing.JTextArea;
23 import javax.swing.SwingUtilities;
24 import javax.swing.text.BadLocationException;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.regex.Pattern;
28 
29 /**
30  * HiLog Filter
31  *
32  * @since 2021/08/07 13:41
33  */
34 public class HiLogFilter {
35     private static final Logger LOGGER = LogManager.getLogger(HiLogFilter.class);
36 
37     /**
38      * max row num
39      */
40     public static final int MAX_ROW_NUM = 7500;
41 
42     /**
43      * line break num
44      */
45     public static final int LINE_BREAK_NUM = 2;
46     private static final int VERBOSE_LEVEL = 7;
47     private static final int DEBUG_LEVEL = 6;
48     private static final int INFO_LEVEL = 5;
49     private static final int WARN_LEVEL = 4;
50     private static final int ERROR_LEVEL = 3;
51     private static final int FATAL_LEVEL = 2;
52     private static final int ASSERT_LEVEL = 1;
53     private static final int SPLIT_LENGTH = 6;
54     private static final HiLogFilter INSTANCE = new HiLogFilter();
55 
56     /**
57      * regex yyyy-MM-dd、MM-dd or yyyy-M-dd
58      */
59     private final String dataRegex =
60         "^([1-9]\\d{3}-)(([0]{0,1}[1-9]-)|([1][0-2]-))(([0-3]{0,1}[0-9]))$|^(([0]{0,1}[1-9]-)|([1][0-2]-))"
61             + "(([0-3]{0,1}[0-9]))$";
62 
63     /**
64      * regex hh-mm-ss
65      */
66     private final String timeRegex = "^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\.([0-9]{3})$";
67     private String type = "";
68     private Map<String, Integer> levelMap = new HashMap<>();
69 
HiLogFilter()70     private HiLogFilter() {
71         levelMap.put("V", VERBOSE_LEVEL);
72         levelMap.put("D", DEBUG_LEVEL);
73         levelMap.put("I", INFO_LEVEL);
74         levelMap.put("W", WARN_LEVEL);
75         levelMap.put("E", ERROR_LEVEL);
76         levelMap.put("F", FATAL_LEVEL);
77         levelMap.put("A", ASSERT_LEVEL);
78     }
79 
80     /**
81      * getInstance
82      *
83      * @return HiLogFilter
84      */
getInstance()85     public static HiLogFilter getInstance() {
86         return INSTANCE;
87     }
88 
89     /**
90      * According to the conditions filterLog
91      *
92      * @param logTextArea logTextArea
93      * @param logLevel logLevel
94      * @param searchValue searchValue
95      * @param wholeBuilder wholeBuilder
96      */
filterLog(JTextArea logTextArea, String logLevel, String searchValue, StringBuilder wholeBuilder)97     public void filterLog(JTextArea logTextArea, String logLevel, String searchValue, StringBuilder wholeBuilder) {
98         if (ProfilerLogManager.isInfoEnabled()) {
99             LOGGER.info("filterLog");
100         }
101         StringBuilder stringBuilder = new StringBuilder();
102         String[] lineStr = wholeBuilder.toString().split(System.lineSeparator());
103         logTextArea.setText("");
104         for (String str : lineStr) {
105             String[] lineStrs = str.split(" ");
106             getLineLogLevel(lineStrs);
107             boolean dataMatches = Pattern.matches(dataRegex, lineStrs[0]);
108             boolean timeMatches = Pattern.matches(timeRegex, lineStrs[1]);
109             if (lineStrs.length >= SPLIT_LENGTH && dataMatches && timeMatches) {
110                 // W
111                 if (levelMap.get(type) <= levelMap.get(logLevel) && str.contains(searchValue)) {
112                     stringBuilder.append(str).append(System.lineSeparator());
113                 }
114             } else {
115                 if (levelMap.containsKey(type) && levelMap.get(type) <= levelMap.get(logLevel) && str
116                     .contains(searchValue)) {
117                     stringBuilder.append(str).append(System.lineSeparator());
118                 }
119             }
120         }
121         SwingUtilities.invokeLater(new Runnable() {
122             /**
123              * run
124              */
125             public void run() {
126                 logTextArea.setText(stringBuilder.toString());
127             }
128         });
129     }
130 
getLineLogLevel(String[] lineStrs)131     private void getLineLogLevel(String[] lineStrs) {
132         if (ProfilerLogManager.isInfoEnabled()) {
133             LOGGER.info("getLineLogLevel");
134         }
135         if (lineStrs.length <= 1) {
136             return;
137         }
138         for (String item : lineStrs) {
139             if (item.length() == 1 && levelMap.containsKey(item)) {
140                 type = item;
141                 break;
142             }
143         }
144     }
145 
146     /**
147      * Row filtering
148      *
149      * @param str str
150      * @param searchValue searchValue
151      * @param logLevel logLevel
152      * @param logTextArea logTextArea
153      */
lineFilter(String str, String searchValue, String logLevel, JTextArea logTextArea)154     public void lineFilter(String str, String searchValue, String logLevel, JTextArea logTextArea) {
155         if (ProfilerLogManager.isInfoEnabled()) {
156             LOGGER.info("lineFilter");
157         }
158         if (logTextArea.getLineCount() >= MAX_ROW_NUM) {
159             String[] lines = logTextArea.getText().split(System.lineSeparator());
160             try {
161                 logTextArea.getDocument().remove(0, lines[0].length() + LINE_BREAK_NUM);
162             } catch (BadLocationException locationException) {
163                 if (ProfilerLogManager.isErrorEnabled()) {
164                     LOGGER.error("Error deleting the top row when the maximum number of rows is exceeded {}",
165                         locationException.getMessage());
166                 }
167             }
168         }
169         String[] lineStrs = str.split(" ");
170         getLineLogLevel(lineStrs);
171         boolean dataMatches = Pattern.matches(dataRegex, lineStrs[0]);
172         boolean timeMatches = Pattern.matches(timeRegex, lineStrs[1]);
173         if (lineStrs.length >= SPLIT_LENGTH && dataMatches && timeMatches) {
174             if (levelMap.get(type) <= levelMap.get(logLevel) && str.contains(searchValue)) {
175                 logTextArea.append(str + System.lineSeparator());
176             }
177         } else {
178             if (levelMap.containsKey(type) && levelMap.get(type) <= levelMap.get(logLevel) && str
179                 .contains(searchValue)) {
180                 logTextArea.append(str + System.lineSeparator());
181             }
182         }
183     }
184 
185     /**
186      * is error log
187      *
188      * @param line line
189      * @return boolean boolean
190      */
isErrorLog(String line)191     public boolean isErrorLog(String line) {
192         if (ProfilerLogManager.isInfoEnabled()) {
193             LOGGER.info("isErrorLog");
194         }
195         String[] lines = line.split(" ");
196         boolean dataMatches = Pattern.matches(dataRegex, lines[0]);
197         boolean timeMatches = Pattern.matches(timeRegex, lines[1]);
198         if (lines.length >= SPLIT_LENGTH && dataMatches && timeMatches) {
199             return false;
200         } else {
201             return true;
202         }
203     }
204 }
205