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 {
compileSdkVersion 24
buildToolsVersion "24.0.3"
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.topjohnwu.magisk"

View File

@ -9,6 +9,8 @@ public class Logger {
private static final String LOG_TAG = "Magisk: DEV";
private static final boolean logShell = true;
public static void dev(String msg, Object... args) {
Context context = null;
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 {
return (Application) Class.forName("android.app.AppGlobals")
.getMethod("getInitialApplication").invoke(null, (Object[]) null);

View File

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

View File

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