From 4842bdd38b93bfc69ba810e7dbce7c78e8bf77b4 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 24 Mar 2021 20:20:24 -0700 Subject: [PATCH] Have the log catch exceptions during matching --- MPF/UserControls/LogOutput.xaml.cs | 94 ++++++++++++++++-------------- 1 file changed, 51 insertions(+), 43 deletions(-) diff --git a/MPF/UserControls/LogOutput.xaml.cs b/MPF/UserControls/LogOutput.xaml.cs index f475b9dc..b28b1f5e 100644 --- a/MPF/UserControls/LogOutput.xaml.cs +++ b/MPF/UserControls/LogOutput.xaml.cs @@ -402,68 +402,76 @@ namespace MPF.UserControls if (verbose && !ViewModels.OptionsViewModel.VerboseLogging) return; - // Get thr brush color from the flags + // Get the brush color from the flags Brush brush = Brushes.White; if (error) brush = Brushes.Red; else if (verbose) brush = Brushes.Yellow; - // Get last line - if (lastLineText == null) - lastLineText = GetLastLine(); + try + { + // Get last line + if (lastLineText == null) + lastLineText = GetLastLine(); - // Always append if there's no previous line - if (lastLineText == null) - { - AppendToTextBox(text, brush); - lastUsedMatcher = null; - } - // Return always means overwrite - else if (text.StartsWith("\r")) - { - ReplaceLastLine(text, brush); - lastUsedMatcher = null; - } - else - { - // If we have a cached matcher, try it first - if (lastUsedMatcher?.Matches(ref text) == true) + // Always append if there's no previous line + if (lastLineText == null) + { + AppendToTextBox(text, brush); + lastUsedMatcher = null; + } + // Return always means overwrite + else if (text.StartsWith("\r")) { ReplaceLastLine(text, brush); + lastUsedMatcher = null; } else { - // Get all matching Matchers - var matches = _matchers.Where(m => m.Matches(ref text)); - if (matches.Any()) + // If we have a cached matcher, try it first + if (lastUsedMatcher?.Matches(ref text) == true) { - // Use the first Matcher - var firstMatcher = matches.First(); - if (firstMatcher.Matches(ref lastLineText)) - ReplaceLastLine(text, brush); - else if (string.IsNullOrWhiteSpace(lastLineText)) - ReplaceLastLine(text, brush); - else - AppendToTextBox(text, brush); - - // Cache the last used Matcher - lastUsedMatcher = firstMatcher; + ReplaceLastLine(text, brush); } - // Default case for all other text else { - AppendToTextBox(text, brush); - lastUsedMatcher = null; + // Get all matching Matchers + var matches = _matchers.Where(m => m.Matches(ref text)); + if (matches.Any()) + { + // Use the first Matcher + var firstMatcher = matches.First(); + if (firstMatcher.Matches(ref lastLineText)) + ReplaceLastLine(text, brush); + else if (string.IsNullOrWhiteSpace(lastLineText)) + ReplaceLastLine(text, brush); + else + AppendToTextBox(text, brush); + + // Cache the last used Matcher + lastUsedMatcher = firstMatcher; + } + // Default case for all other text + else + { + AppendToTextBox(text, brush); + lastUsedMatcher = null; + } } } + + // Update the bar if needed + ProcessStringForProgressBar(text); + + // Cache the current text as the last line + lastLineText = text; + } + catch (Exception ex) + { + // In the event that something fails horribly, we want to log + AppendToTextBox(ex.ToString(), Brushes.Red); } - - // Update the bar if needed - ProcessStringForProgressBar(text); - - // Cache the current text as the last line - lastLineText = text; } ///