Fixed: Invalid log lines being reported

This commit is contained in:
Akash Yadav
2021-10-27 18:55:43 +05:30
parent 85d72bc53c
commit db8c0f4760
5 changed files with 116 additions and 69 deletions
@@ -0,0 +1,82 @@
package com.itsaky.androidide.logsender;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.Intent;
import android.content.Context;
public class LogSender extends Thread {
private static LogSender instance;
private BufferedReader output;
private Process process;
private Context ctx;
private static final String PACKAGE_NAME = "com.itsaky.androidide";
private static final String APPEND_LOG = "com.itsaky.androidide.logs.APPEND_LOG";
private static final String EXTRA_LINE = "log_line";
private Callback CALLBACK = new Callback() {
@Override
public void output(CharSequence c) {
if(c != null && c.toString().trim().length() > 0) {
Intent i = new Intent();
i.setAction(APPEND_LOG);
i.putExtra(EXTRA_LINE, c.toString());
i.setPackage(PACKAGE_NAME);
ctx.sendBroadcast(i);
}
}
};
private LogSender(Context ctx) {
this.ctx = ctx;
final String command = "sh";
final String dirPath = ctx.getFilesDir().getAbsolutePath();
ProcessBuilder processBuilder = new ProcessBuilder(new String[]{command});
processBuilder.directory(new File(dirPath));
processBuilder.redirectErrorStream(true);
try {
this.process = processBuilder.start();
this.output = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
final String str = "logcat -v threadtime";
this.process.getOutputStream().write(str.concat("\n").getBytes());
this.process.getOutputStream().flush();
} catch (Throwable th) {
}
}
public static void startLogging(Context ctx) {
instance = instance == null ? instance = new LogSender(ctx) : instance;
instance.start();
}
@Override
public void run() {
while (true) {
try {
String readLine = this.output.readLine();
if (readLine == null) break;
CALLBACK.output(readLine.concat("\n"));
} catch (Throwable th) {}
}
try {
this.output.close();
this.process.getInputStream().close();
this.process.getErrorStream().close();
this.process.getOutputStream().close();
this.process.destroy();
} catch (Throwable th) {}
}
public interface Callback {
public void output(CharSequence charSequence);
}
}
@@ -41,6 +41,10 @@ public class LogLine {
this.tag = tag;
this.message = message;
this.priority = parsePriority(priorityChar);
if(tag.length() > 25) {
this.tag = "...".concat(tag.substring(tag.length() - 25));
}
}
private int parsePriority(String s) {
@@ -73,6 +77,6 @@ public class LogLine {
@Override
public String toString() {
return String.format("%-6s %-13s %-6s %-6s %-2s %-26s %s", date, time, pid, tid, priorityChar, tag, message);
return String.format("%-6s %-13s %-6s %-6s %-2s %-40s %s", date, time, pid, tid, priorityChar, tag, message);
}
}
@@ -13,9 +13,7 @@ public class LogReceiver extends BroadcastReceiver {
public static final String APPEND_LOG = "com.itsaky.androidide.logs.APPEND_LOG";
public static final String EXTRA_LINE = "log_line";
private LogLine logTag = null;
public LogReceiver setLogListener(LogListener listener) {
this.listener = listener;
return this;
@@ -33,68 +31,23 @@ public class LogReceiver extends BroadcastReceiver {
}
private void sendLogLine(String line) {
Matcher m = LOG_HEAD_PATTERN.matcher(line);
LogLine log = null;
final boolean find = line != null && line.trim().length() > 2 /*if length() == 2 then only [] is available*/ && m.find();
if(find) {
String date = "";
String time = "";
String pid = "";
String tid = "";
String priority = "";
String tag = "";
m = DATE_MATCHER.matcher(line);
if(m.find())
date = line.substring(m.start(), m.end());
m = TIME_MATCHER.matcher(line);
if(m.find())
time = line.substring(m.start(), m.end());
m = PIDTID_MATCHER.matcher(line);
if(m.find()) {
String[] s = line.substring(m.start(), m.end()).split(":");
if(s.length > 0)
pid = s[0];
if(s.length > 1)
tid = s[1];
}
m = PRIORITYTAG_MATCHER.matcher(line);
if(m.find()) {
String[] s = line.substring(m.start(), m.end()).split("/", 2);
if(s.length > 0)
priority = s[0];
if(s.length > 1)
tag = s[1];
if(tag.trim().endsWith("]"))
tag = tag.substring(0, tag.lastIndexOf("]")).trim();
}
logTag = new LogLine(date, time, pid, tid, priority, tag);
return;
}
if(logTag != null) {
log = new LogLine(logTag);
log.message = line;
} else {
log = new LogLine("", "", "", "", "", "", line);
}
final String[] split = line.split("\\s", 7);
final LogLine log = new LogLine(
split[0], // date
split[1], // time
split[2], // process id
split[3], // thread id
split[4], // priority
split[5], // tag
split[6] // message
);
if(log != null && listener != null) {
listener.appendLogLine(log);
logTag = null;
}
if(listener != null) {
listener.appendLogLine(log);
}
}
public static interface LogListener {
public void appendLogLine(LogLine line);
}
public static final Pattern DATE_MATCHER = Pattern.compile("\\d{2}-\\d{2}");
public static final Pattern TIME_MATCHER = Pattern.compile("\\d{2}:\\d{2}:\\d{2}\\.\\d{2,3}");
public static final Pattern PIDTID_MATCHER = Pattern.compile("\\d{4,5}\\s*:\\s*\\d{4,5}");
public static final Pattern PRIORITYTAG_MATCHER = Pattern.compile("[A-Z]\\/.*");
public static final Pattern LOG_HEAD_PATTERN = Pattern.compile("\\[\\s*\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}\\.\\d{2,3}\\s+\\d{4,5}\\s*:\\s*\\d{4,5}\\s+\\w/.*\\]");
}
@@ -159,12 +159,7 @@ public final class Environment {
addToEnvIfPresent(map, "ANDROID_RUNTIME_ROOT");
addToEnvIfPresent(map, "ANDROID_TZDATA_ROOT");
String path = "";
path += String.format("%s/bin", JAVA_HOME.getAbsolutePath());
path += String.format(":%s/cmdline-tools/latest/bin", ANDROID_HOME.getAbsolutePath());
path += String.format(":%s/cmake/bin", ANDROID_HOME.getAbsolutePath());
path += String.format(":%s/bin", SYSROOT.getAbsolutePath());
path += String.format(":%s", System.getenv("PATH"));
String path = createPath();
map.put("PATH", path);
@@ -178,6 +173,16 @@ public final class Environment {
return map;
}
private static String createPath() {
String path = "";
path += String.format("%s/bin", JAVA_HOME.getAbsolutePath());
path += String.format(":%s/cmdline-tools/latest/bin", ANDROID_HOME.getAbsolutePath());
path += String.format(":%s/cmake/bin", ANDROID_HOME.getAbsolutePath());
path += String.format(":%s/bin", SYSROOT.getAbsolutePath());
path += String.format(":%s", System.getenv("PATH"));
return path;
}
public static void addToEnvIfPresent(Map<String, String> environment, String name) {
String value = System.getenv(name);
@@ -201,6 +206,9 @@ public final class Environment {
if(value.contains("$SYSROOT")) {
value = value.replace("$SYSROOT", SYSROOT.getAbsolutePath());
}
if(value.contains("$PATH")) {
value = value.replace("$PATH", createPath());
}
return value;
}
+1 -1
View File
@@ -7,7 +7,7 @@ buildscript {
buildTools = "31.0.0"
minSdk = 24
targetSdk = 28
versionCode = 103210928
versionCode = 103211027
versionName = "1.0.3-alpha"
packageName = "com.itsaky.androidide"