public static string Await(this ILineReader reader, [Implicit] ICallContext context, string text, TimeSpan timeout, bool removeFound = true) { System.Diagnostics.Debug.WriteLine("Reader.Await: " + text); if (context != null && context.LoggingEnabled) { context.Logger.Log("Await", "\"" + text + "\""); } var comparer = StringUtils.CreateComparer(text); reader.DebugDump(); // If the reader has timestampe, set the timeout relative to the time of the current entry; otherwise just use current wall time. DateTime entry = (reader.LinesHaveTimestamp && reader.Current != null) ? reader.Current.Timestamp : DateTime.Now; // The time where the timeout expires. DateTime to = (timeout == TimeSpan.MaxValue) ? DateTime.MaxValue : entry + timeout; //bool sleep = false; do { var result = reader.Find(null, comparer, true); if (result != null) { if (removeFound) { reader.Next(); } return(result); } lock (reader.Sync) { if (reader.Current == null) { Monitor.Wait(reader.Sync, 50); } } //sleep = true; } while (DateTime.Now.TimeTill(to) > TimeSpan.Zero); if ((context as IScriptCallContext) != null) { var ctx = context as IScriptCallContext; var readerName = reader.Source.Name; if (readerName == null) { readerName = "log reader"; } ctx.ReportFailure($"No entry matching \"{text}\" was found in {readerName}."); } return(null); }
public static string Find(this ILineReader reader, [Implicit] ICallContext context, Func <string, string> comparer, bool flushIfNotFound = false) { var peaker = reader.Peak(); ILineReaderEntry last = null; foreach (var entry in peaker) { var result = comparer(entry.Text); if (result != null) { reader.Flush(entry); return(result); } last = entry; } if (flushIfNotFound) { reader.Flush(last); // First flush until the last seen entry reader.Next(); // ... then also flush the last seen. } return(null); }