What is a good way to ensure that Logcat logs are cleared?
<p>I'm writing unit tests for one of my company's Android apps. As some of the tests check for the presence of certain lines in the Logcat output, I want to clear the logs before each test is run.</p>

<p>I know the basic command for doing so is <code>logcat -c</code>. This process isn't instantaneous and takes a few seconds, but because it happens on a separate thread, the next command in the test case would immediately execute and possibly return an incorrect result. One workaround is to add a forced wait. So my basic code looks like this:</p>

<pre><code>private void clearLogs() throws Exception {
    Runtime.getRuntime().exec("logcat -c");
    Thread.sleep(2000);
}
</code></pre>

<p>This has worked without issues so far. However, my manager wants the code to be able to handle cases in which clearing the logs takes an arbitrary amount of time. For example, suppose there is a hypothetical situation in which clearing the logs takes 20 seconds. How would I go about doing this?</p>

<p>I did find a solution that involves creating a filter to get only entries that are written after a certain time stamp:</p>

<pre><code>NOW=$(date +"%m-%d %H:%M:%S.000") ; adb logcat -T "$NOW"
</code></pre>

<p>This would normally remove the need to clear loogs altogether. However, it doesn't work for me as Logcat gives me an "invalid option -- T" error.</p>

<p>Anyone have any solutions?</p>