Java源码示例:com.facebook.stetho.dumpapp.DumpException
示例1
@Override
public void dump(DumperContext dumpContext) throws DumpException {
Iterator<String> argsIter = dumpContext.getArgsAsList().iterator();
String command = ArgsHelper.nextOptionalArg(argsIter, null);
if ("throw".equals(command)) {
doUncaughtException(argsIter);
} else if ("kill".equals(command)) {
doKill(dumpContext, argsIter);
} else if ("exit".equals(command)) {
doSystemExit(argsIter);
} else {
doUsage(dumpContext.getStdout());
if (command != null) {
throw new DumpUsageException("Unsupported command: " + command);
}
}
}
示例2
private void doKill(DumperContext dumpContext, Iterator<String> argsIter) throws DumpException {
String signal = ArgsHelper.nextOptionalArg(argsIter, OPTION_KILL_DEFAULT);
try {
Process kill = new ProcessBuilder()
.command("/system/bin/kill", "-" + signal, String.valueOf(android.os.Process.myPid()))
.redirectErrorStream(true)
.start();
// Handle kill command output gracefully in the event that the signal delivered didn't
// actually take out our process...
try {
InputStream in = kill.getInputStream();
Util.copy(in, dumpContext.getStdout(), new byte[1024]);
} finally {
kill.destroy();
}
} catch (IOException e) {
throw new DumpException("Failed to invoke kill: " + e);
}
}
示例3
@Override
public void dump(DumperContext dumpContext) throws DumpException {
final PrintStream output = dumpContext.getStdout();
Iterator<String> argsIter = dumpContext.getArgsAsList().iterator();
String outputPath = argsIter.hasNext() ? argsIter.next() : null;
if (outputPath == null) {
usage(output);
} else {
if ("-".equals(outputPath)) {
handlePipeOutput(output);
} else {
File outputFile = new File(outputPath);
if (!outputFile.isAbsolute()) {
outputFile = mContext.getFileStreamPath(outputPath);
}
writeHprof(outputFile);
output.println("Wrote to " + outputFile);
}
}
}
示例4
private void handlePipeOutput(OutputStream output) throws DumpException {
File hprofFile = mContext.getFileStreamPath("hprof-dump.hprof");
try {
writeHprof(hprofFile);
try {
InputStream input = new FileInputStream(hprofFile);
try {
Util.copy(input, output, new byte[2048]);
} finally {
input.close();
}
} catch (IOException e) {
throw new DumpException("Failure copying " + hprofFile + " to dumper output");
}
} finally {
if (hprofFile.exists()) {
hprofFile.delete();
}
}
}
示例5
@Override
public void dump(DumperContext dumpContext) throws DumpException {
Iterator<String> args = dumpContext.getArgsAsList().iterator();
String command = ArgsHelper.nextOptionalArg(args, "");
if ("ls".equals(command)) {
doLs(dumpContext.getStdout());
} else if ("tree".equals(command)) {
doTree(dumpContext.getStdout());
} else if ("download".equals(command)) {
doDownload(dumpContext.getStdout(), args);
} else {
doUsage(dumpContext.getStdout());
if (!"".equals(command)) {
throw new DumpUsageException("Unknown command: " + command);
}
}
}
示例6
@Override
public void dump(DumperContext dumpContext) throws DumpException {
PrintStream writer = dumpContext.getStdout();
Iterator<String> argsIter = dumpContext.getArgsAsList().iterator();
String command = ArgsHelper.nextOptionalArg(argsIter, null);
if (CMD_LIST.equalsIgnoreCase(command)) {
doList(writer);
} else if (CMD_DELETE.equalsIgnoreCase(command)) {
doRemove(writer, argsIter);
} else if (CMD_CLEAR.equalsIgnoreCase(command)) {
doClear(writer);
} else if (CMD_REFRESH.equalsIgnoreCase(command)) {
doRefresh(writer);
} else {
usage(writer);
if (command != null) {
throw new DumpUsageException("Unknown command: " + command);
}
}
}
示例7
/**
* Entry point for the Stetho dumpapp script.
*
* <p>{@link #initialize} must have been called in the app before running dumpapp.
*/
@Override
public void dump(DumperContext dumpContext) throws DumpException {
ensureInitialized();
List<String> args = dumpContext.getArgsAsList();
PrintStream writer = dumpContext.getStdout();
String cmd = args.isEmpty() ? null : args.get(0);
List<String> rest = args.isEmpty() ? new ArrayList<String>() : args.subList(1, args.size());
if (cmd != null && cmd.equals("memcache")) {
memcache(writer, rest);
} else if (cmd != null && cmd.equals("diskcache")) {
diskcache(mMainFileCache, "Main", writer, rest);
diskcache(mSmallFileCache, "Small", writer, rest);
} else if (cmd != null && cmd.equals("clear")) {
mImagePipeline.clearCaches();
} else {
usage(writer);
if (TextUtils.isEmpty(cmd)) {
throw new DumpUsageException("Missing command");
} else {
throw new DumpUsageException("Unknown command: " + cmd);
}
}
}
示例8
private void diskcache(FileCache cache, String title, PrintStream writer, List<String> args)
throws DumpException {
DiskStorage.DiskDumpInfo intDiskDumpInfo;
try {
intDiskDumpInfo = cache.getDumpInfo();
} catch (IOException e) {
throw new DumpException(e.getMessage());
}
if (!args.isEmpty() && args.get(0).equals("-s")) {
writeDiskDumpInfoScriptReadable(writer, intDiskDumpInfo);
} else {
writer.println();
writer.println(title + " disk cache contents:");
writeDiskDumpInfo(writer, intDiskDumpInfo);
}
}
示例9
@Override
public void dump(DumperContext dumpContext) throws DumpException {
final PrintStream writer = dumpContext.getStdout();
List<String> args = dumpContext.getArgsAsList();
String commandName = args.isEmpty() ? "" : args.remove(0);
switch (commandName) {
case "alarms":
displayAlarms(writer);
break;
case "appInfo":
displayAppInfo(writer);
break;
case "bootReceiver":
displayBootReceiverState(writer);
break;
case "currentSession":
displayCurrentSessionData(writer);
break;
case "endpoint":
changeEndpoint(writer, args);
break;
case "notif":
displayNotificationReminder();
break;
default:
doUsage(writer);
break;
}
}
示例10
private void writeHprof(File outputPath) throws DumpException {
try {
// Test that we can write here. dumpHprofData appears to hang if it cannot write
// to the target location on ART.
truncateAndDeleteFile(outputPath);
Debug.dumpHprofData(outputPath.getAbsolutePath());
} catch (IOException e) {
throw new DumpException("Failure writing to " + outputPath + ": " + e.getMessage());
}
}
示例11
@Override
public void dump(DumperContext dumpContext) throws DumpException {
PrintStream writer = dumpContext.getStdout();
Iterator<String> args = dumpContext.getArgsAsList().iterator();
String helloToWhom = ArgsHelper.nextOptionalArg(args, null);
if (helloToWhom != null) {
doHello(dumpContext.getStdin(), writer, helloToWhom);
} else {
doUsage(writer);
}
}
示例12
private void doHello(InputStream in, PrintStream writer, String name) throws DumpException {
if (TextUtils.isEmpty(name)) {
// This will print an error to the dumpapp user and cause a non-zero exit of the
// script.
throw new DumpUsageException("Name is empty");
} else if ("-".equals(name)) {
try {
name = new BufferedReader(new InputStreamReader(in)).readLine();
} catch (IOException e) {
throw new DumpException(e.toString());
}
}
writer.println("Hello " + name + "!");
}
示例13
private void getFiles(
PrintStream writer, CountingMemoryCacheInspector.DumpInfo<CacheKey, CloseableImage> dumpInfo)
throws DumpException, IOException {
writer.println("\nStoring all images in the memory cache into /sdcard/imagedumperfiles/ ...");
File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/imagedumperfiles/");
if (dir.exists() && dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
file.delete();
}
}
if (!dir.delete()) {
throw new DumpException("Failed to clear existing /sdcard/imagedumperfiles directory");
}
}
if (!dir.mkdirs()) {
throw new DumpException("Failed to create /sdcard/imagedumperfiles directory");
}
if (!dumpInfo.lruEntries.isEmpty()) {
writer.println("LRU Entries:");
storeEntries(dumpInfo.lruEntries, 1, writer, dir);
}
if (!dumpInfo.sharedEntries.isEmpty()) {
writer.println("Shared Entries:");
storeEntries(dumpInfo.sharedEntries, dumpInfo.lruEntries.size() + 1, writer, dir);
}
writer.println("Done!");
}
示例14
protected abstract void ensureInitialized() throws DumpException;