Skip to content

Commit 9e19b1d

Browse files
committed
优化 LogcatInfo 类的代码逻辑
1 parent 9e184cb commit 9e19b1d

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

library/src/main/java/com/hjq/logcat/LogcatInfo.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.support.annotation.Nullable;
55
import java.util.ArrayList;
6+
import java.util.List;
67
import java.util.regex.Matcher;
78
import java.util.regex.Pattern;
89

@@ -35,7 +36,7 @@ final class LogcatInfo {
3536
*
3637
* 案例:05-19 23:59:18.383
3738
*/
38-
private static final String LOG_TIME = "([0-9^-]+-[0-9^ ]+\\s[0-9^:]+:[0-9^:]+\\.[0-9]+)";
39+
private static final String LOG_TIME = "([0-9^-]+-[0-9^ ]+\\s[0-9^:]+:[0-9^:]+\\.\\d+)";
3940

4041
/**
4142
* 应用 id 匹配正则
@@ -49,14 +50,14 @@ final class LogcatInfo {
4950
*
5051
* 案例:3177
5152
*/
52-
private static final String LOG_PID = "([0-9]+)";
53+
private static final String LOG_PID = "(\\d+)";
5354

5455
/**
5556
* 线程 id 匹配正则
5657
*
5758
* 案例:3258
5859
*/
59-
private static final String LOG_TID = "([0-9]+)";
60+
private static final String LOG_TID = "(\\d+)";
6061

6162
/**
6263
* 日志等级匹配正则
@@ -85,11 +86,13 @@ final class LogcatInfo {
8586
LOG_SEPARATOR + LOG_PID + LOG_SEPARATOR + LOG_TID + LOG_SEPARATOR +
8687
LOG_LEVEL + LOG_SEPARATOR + LOG_TAG + LOG_SEPARATOR + LOG_CONTENT);
8788

88-
static final ArrayList<String> IGNORED_LOG = new ArrayList<String>() {{
89-
add("--------- beginning of crash");
90-
add("--------- beginning of main");
91-
add("--------- beginning of system");
92-
}};
89+
static final List<String> IGNORED_LOG = new ArrayList<>();
90+
91+
static {
92+
IGNORED_LOG.add("--------- beginning of crash");
93+
IGNORED_LOG.add("--------- beginning of main");
94+
IGNORED_LOG.add("--------- beginning of system");
95+
}
9396

9497
/** 时间 */
9598
private String time;
@@ -186,17 +189,13 @@ void addLogContent(String text) {
186189

187190
@Override
188191
public String toString() {
189-
return String.format("%s" + SPACE + "%s" + SPACE + "%s", time, tag, content);
192+
return time + SPACE + tag + SPACE + content;
190193
}
191194

192195
public String toString(Context context) {
193196
if (!LogcatUtils.isPortrait(context)) {
194197
return toString();
195198
}
196-
197-
String log = getContent();
198-
return String.format("%s" + LogcatInfo.SPACE + "%s" +
199-
(log.startsWith("\n") ? LogcatInfo.SPACE : "\n")
200-
+ "%s", time, tag, log);
199+
return time + SPACE + tag + SPACE + (content.startsWith("\n") ? SPACE : "\n") + content;
201200
}
202201
}

0 commit comments

Comments
 (0)