Created
March 17, 2015 13:04
-
-
Save silin/c8fabfc04d485e65308e to your computer and use it in GitHub Desktop.
Save app LogCat logs to file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--To fetch LogCat logs--> | |
<uses-permission android:name="android.permission.READ_LOGS" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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