Add Shell logging

This commit is contained in:
topjohnwu 2016-09-30 10:41:40 +08:00
parent 0f5465c5da
commit 62523c815e
4 changed files with 22 additions and 2 deletions

View File

@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android { android {
compileSdkVersion 24 compileSdkVersion 24
buildToolsVersion "24.0.3" buildToolsVersion "24.0.2"
defaultConfig { defaultConfig {
applicationId "com.topjohnwu.magisk" applicationId "com.topjohnwu.magisk"

View File

@ -9,6 +9,8 @@ public class Logger {
private static final String LOG_TAG = "Magisk: DEV"; private static final String LOG_TAG = "Magisk: DEV";
private static final boolean logShell = true;
public static void dev(String msg, Object... args) { public static void dev(String msg, Object... args) {
Context context = null; Context context = null;
try { try {
@ -37,6 +39,12 @@ public class Logger {
} }
} }
public static void shell(boolean root, String msg) {
if (logShell) {
Log.d(root ? "SU" : "SH", msg);
}
}
private static Application getApplicationUsingReflection() throws Exception { private static Application getApplicationUsingReflection() throws Exception {
return (Application) Class.forName("android.app.AppGlobals") return (Application) Class.forName("android.app.AppGlobals")
.getMethod("getInitialApplication").invoke(null, (Object[]) null); .getMethod("getInitialApplication").invoke(null, (Object[]) null);

View File

@ -42,7 +42,7 @@ public class Shell {
} }
rootSTDIN = new DataOutputStream(rootShell.getOutputStream()); rootSTDIN = new DataOutputStream(rootShell.getOutputStream());
rootSTDOUT = new StreamGobbler(rootShell.getInputStream(), rootOutList); rootSTDOUT = new StreamGobbler(rootShell.getInputStream(), rootOutList, true);
rootSTDOUT.start(); rootSTDOUT.start();
List<String> ret = su("echo -BOC-", "id"); List<String> ret = su("echo -BOC-", "id");
@ -82,6 +82,7 @@ public class Shell {
for (String write : commands) { for (String write : commands) {
STDIN.write((write + "\n").getBytes("UTF-8")); STDIN.write((write + "\n").getBytes("UTF-8"));
STDIN.flush(); STDIN.flush();
Logger.shell(false, write);
} }
STDIN.write("exit\n".getBytes("UTF-8")); STDIN.write("exit\n".getBytes("UTF-8"));
STDIN.flush(); STDIN.flush();
@ -149,6 +150,7 @@ public class Shell {
for (String write : commands) { for (String write : commands) {
STDIN.write((write + "\n").getBytes("UTF-8")); STDIN.write((write + "\n").getBytes("UTF-8"));
STDIN.flush(); STDIN.flush();
Logger.shell(true, write);
} }
if (newShell) { if (newShell) {
STDIN.write("exit\n".getBytes("UTF-8")); STDIN.write("exit\n".getBytes("UTF-8"));

View File

@ -14,6 +14,7 @@ public class StreamGobbler extends Thread {
private BufferedReader reader = null; private BufferedReader reader = null;
private List<String> writer = null; private List<String> writer = null;
private boolean isRoot = false;
/** /**
* <p>StreamGobbler constructor</p> * <p>StreamGobbler constructor</p>
@ -30,6 +31,12 @@ public class StreamGobbler extends Thread {
writer = outputList; writer = outputList;
} }
public StreamGobbler(InputStream inputStream, List<String> outputList, boolean root) {
reader = new BufferedReader(new InputStreamReader(inputStream));
writer = outputList;
isRoot = root;
}
@Override @Override
public void run() { public void run() {
// keep reading the InputStream until it ends (or an error occurs) // keep reading the InputStream until it ends (or an error occurs)
@ -37,6 +44,9 @@ public class StreamGobbler extends Thread {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
writer.add(line); writer.add(line);
if (!line.equals("-root-done-") && !line.isEmpty()) {
Logger.shell(isRoot, "OUT: " + line);
}
} }
} catch (IOException e) { } catch (IOException e) {
// reader probably closed, expected exit condition // reader probably closed, expected exit condition