Skip to content

Instantly share code, notes, and snippets.

@asanand3
Created September 9, 2015 11:45
Show Gist options
  • Save asanand3/16edc8162d2e3e77c3dc to your computer and use it in GitHub Desktop.
Save asanand3/16edc8162d2e3e77c3dc to your computer and use it in GitHub Desktop.
Read the log through the following code:
public class LogTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) {
}
}
}
The above code reads the log and displays it in a textview.
You should define:
<uses-permission android:name="android.permission.READ_LOGS" />
permission in your manifest.
To copy the log directly to a file,use the following method:
public static void copyLogcatToFile(Context context) {
String fileName = "log_cat"+System.currentTimeMillis()+".txt";
File outputFile = new File(context.getExternalCacheDir(),fileName);
@SuppressWarnings("unused")
Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment