As it turns out, this tutorial describes a method for dumping a copy of the system logs to a StringBuilder
object.
Code as copied from the tutorial:
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) { }
}
}
Note that you'll need to include a permission for reading logs:
<uses-permission android:name="android.permission.READ_LOGS" />
.
The important part of the code is the Runtime.getRuntime().exec("logcat -d")
part. The -d
switch is the part that tosses out the current log and quits. If I'm reading the documentation correctly, leaving that switch off should, in theory, leave logcat
running, dumping successive lines of the log to its stdout
as they show up.