diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs index 5a639c3e12e..a40d1b355fb 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs @@ -369,11 +369,18 @@ protected static bool MonitorAdbLogcat (Func action, string logcat bool didActionSucceed = false; ManualResetEventSlim stdout_done = new ManualResetEventSlim (); using (var sw = File.CreateText (logcatFilePath)) { + // Process already-buffered logcat lines first so startup messages emitted before + // this monitor starts are still visible to tests. + if (TryMatchLogcatOutput (RunAdbCommand ("logcat -d"), sw, action)) { + sw.Flush (); + return true; + } + using (var proc = Process.Start (info)) { proc.OutputDataReceived += (sender, e) => { if (e.Data != null) { sw.WriteLine (e.Data); - if (action (e.Data)) { + if (!didActionSucceed && action (e.Data)) { didActionSucceed = true; } } else { @@ -395,6 +402,23 @@ protected static bool MonitorAdbLogcat (Func action, string logcat } } + static bool TryMatchLogcatOutput (string output, TextWriter logcatOutput, Func action) + { + bool didActionSucceed = false; + using (var sr = new StringReader (output ?? "")) { + string line = sr.ReadLine (); + while (line != null) { + logcatOutput.WriteLine (line); + if (action (line)) { + didActionSucceed = true; + break; + } + line = sr.ReadLine (); + } + } + return didActionSucceed; + } + protected static bool WaitForDebuggerToStart (string logcatFilePath, int timeout = 120) { bool result = MonitorAdbLogcat ((line) => {