Skip to content

Commit

Permalink
优化日志打印规则
Browse files Browse the repository at this point in the history
修正悬浮窗横竖屏切换位置计算的问题
  • Loading branch information
getActivity committed Apr 23, 2020
1 parent 24cb291 commit 62fd6dd
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
Binary file modified Logcat.apk
Binary file not shown.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#### 集成步骤

dependencies {
debugImplementation 'com.hjq:logcat:6.0'
debugImplementation 'com.hjq:logcat:6.2'
}

#### 使用方式
Expand Down Expand Up @@ -57,6 +57,8 @@

* 日志搜索结果支持文本高亮

* 支持对指定 TAG 的日志屏蔽

* 多个相同 TAG 日志自动合并显示

* 仅在 Debug 下集成,无需手动初始化
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.logcat.demo"
minSdkVersion 14
targetSdkVersion 28
versionCode 60
versionName "6.0"
versionCode 62
versionName "6.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 26
versionCode 60
versionName "6.0"
versionCode 62
versionName "6.2"
}
}

dependencies {
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.hjq:xxpermissions:6.2'
// 悬浮窗框架:https://github.com/getActivity/XToast
implementation 'com.hjq:xtoast:3.5'
implementation 'com.hjq:xtoast:5.0'
}

publish {
Expand Down
45 changes: 25 additions & 20 deletions library/src/main/java/com/hjq/logcat/LogcatInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class LogcatInfo {
private static final String LINE_SPACE = "\n ";

private static final Pattern PATTERN = Pattern.compile(
"([0-9^-]+-[0-9^ ]+ [0-9^:]+:[0-9^:]+\\.[0-9]+) +([0-9]+) +([0-9]+) ([VDIWEF]) ([^ ]*) *: (.*)");
"([0-9^-]+-[0-9^ ]+\\s[0-9^:]+:[0-9^:]+\\.[0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s([VDIWEF])\\s([^\\s]*)\\s*:\\s(.*)");

static final ArrayList<String> IGNORED_LOG = new ArrayList<String>() {{
add("--------- beginning of crash");
Expand All @@ -24,55 +24,60 @@ final class LogcatInfo {
}};

/** 时间 */
private String mTime;
private String time;
/** 等级 */
private String mLevel;
private String level;
/** 标记 */
private String mTag;
private String tag;
/** 内容 */
private String mLog;
private String log;
/** 进程id */
private String mPid;
private String pid;

LogcatInfo(String line) {
static LogcatInfo create(String line) {
Matcher matcher = PATTERN.matcher(line);
// 判断日志格式是否合法(目前发现华为手机有在日志的 TAG 中加空格导致识别不出来,这种无法做兼容)
if (!matcher.find()) {
throw new IllegalStateException("logcat pattern not match: " + line);
return null;
}

mTime = matcher.group(1);
mPid = matcher.group(3);
mLevel = matcher.group(4);
mTag = matcher.group(5);
mLog = matcher.group(6);
LogcatInfo info = new LogcatInfo();
info.time = matcher.group(1);
info.pid = matcher.group(3);
info.level = matcher.group(4);
info.tag = matcher.group(5);
info.log = matcher.group(6);
return info;
}

private LogcatInfo() {}

String getTime() {
return mTime;
return time;
}

String getLevel() {
return mLevel;
return level;
}

String getTag() {
return mTag;
return tag;
}

String getLog() {
return mLog;
return log;
}

String getPid() {
return mPid;
return pid;
}

void addLog(String text) {
mLog = (mLog.startsWith(LINE_SPACE) ? "" : LINE_SPACE) + mLog + LINE_SPACE + text;
log = (log.startsWith(LINE_SPACE) ? "" : LINE_SPACE) + log + LINE_SPACE + text;
}

@Override
public String toString() {
return String.format("%s %s %s", mTime, mTag, mLog);
return String.format("%s %s %s", time, tag, log);
}
}
12 changes: 9 additions & 3 deletions library/src/main/java/com/hjq/logcat/LogcatManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ public void run() {
}
if (FLAG_WORK) {
if (sListener != null) {
sListener.onReceiveLog(new LogcatInfo(line));
LogcatInfo info = LogcatInfo.create(line);
if (info != null) {
sListener.onReceiveLog(info);
}
}
} else {
LOG_BACKUP.add(new LogcatInfo(line));
LogcatInfo info = LogcatInfo.create(line);
if (info != null) {
LOG_BACKUP.add(info);
}
}
}
pause();
Expand All @@ -98,6 +104,6 @@ public void run() {
}

public interface Listener {
void onReceiveLog(LogcatInfo line);
void onReceiveLog(LogcatInfo info);
}
}

0 comments on commit 62fd6dd

Please sign in to comment.