Skip to content

Instantly share code, notes, and snippets.

@silin
Created March 17, 2015 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silin/c8fabfc04d485e65308e to your computer and use it in GitHub Desktop.
Save silin/c8fabfc04d485e65308e to your computer and use it in GitHub Desktop.
Save app LogCat logs to file
<!--To fetch LogCat logs-->
<uses-permission android:name="android.permission.READ_LOGS" />
public class LogCatUtil {
public static void fetch(File file) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
fetch(fos, true);
}
private static void fetch(OutputStream out, boolean close) throws IOException {
byte[] log = new byte[1024 * 2];
InputStream in = null;
try {
Process proc = Runtime.getRuntime().exec("logcat -d -v time");
in = proc.getInputStream();
int read = in.read(log);
while (-1 != read) {
out.write(log, 0, read);
read = in.read(log);
}
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
if (null != out) {
try {
out.flush();
if (close) {
out.close();
}
} catch (IOException e) {
// ignore
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment