Skip to content

Commit

Permalink
Update error processing
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalsleuth committed Apr 21, 2023
1 parent a2f5944 commit de8bb51
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 66 deletions.
6 changes: 3 additions & 3 deletions WinFOR-Customizer/ErrorWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MaxWidth="800"/>
<ColumnDefinition Width="*" MaxWidth="1400"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" MaxHeight="500"/>
<RowDefinition Height="*" MaxHeight="600"/>
</Grid.RowDefinitions>
<TextBox x:Name="ErrorsTextBox" HorizontalAlignment="Center" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="Auto" Height="Auto" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" IsReadOnly="True" AcceptsReturn="True"/>
<TextBox x:Name="ErrorsTextBox" HorizontalAlignment="Center" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="Auto" Height="Auto" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" IsReadOnly="True" AcceptsReturn="True" BorderBrush="{x:Null}" BorderThickness="0" FontSize="14"/>
</Grid>
</Window>
5 changes: 3 additions & 2 deletions WinFOR-Customizer/ErrorWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.Text;

namespace WinFOR_Customizer
{
Expand All @@ -7,10 +8,10 @@ namespace WinFOR_Customizer
/// </summary>
public partial class ErrorWindow : Window
{
public ErrorWindow(string errors)
public ErrorWindow(StringBuilder errors)
{
InitializeComponent();
ErrorsTextBox.Text = errors;
ErrorsTextBox.Text = errors.ToString();
}
}
}
121 changes: 63 additions & 58 deletions WinFOR-Customizer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Printing;
using System.Reflection;
using System.Security.AccessControl;
using System.Security.Cryptography;
Expand Down Expand Up @@ -41,7 +42,7 @@ public MainWindow()
{
InitializeComponent();
DataContext = this;
Version.Content = $"v{appversion}-rc12";
Version.Content = $"v{appversion}-rc13";
outputter = new TextBoxOutputter(OutputConsole);
Console.SetOut(outputter);
CommandBindings.Add(new CommandBinding(KeyboardShortcuts.LoadFile, (sender, e) => { File_Load(); }, (sender, e) => { e.CanExecute = true; }));
Expand Down Expand Up @@ -649,8 +650,6 @@ private async void Install_Click(object sender, RoutedEventArgs e)
is_themed = false;
Console_Output($"No theme has been selected.");
}


if (wsl.IsChecked == true)
{
wsl_selected = true;
Expand Down Expand Up @@ -1807,80 +1806,86 @@ private async void Check_Updates(object sender, RoutedEventArgs e)
MessageBox.Show($"[ERROR] Unable to identify release:\n{ex}");
}
}
private void Results_Button(object sender, RoutedEventArgs e)
// Parses the available logs for the SaltStack and WSL installs to determine its summary
public (StringBuilder, StringBuilder) Process_Results()
{
if (!File.Exists(@"C:\ProgramData\Salt Project\Salt\srv\salt\winfor\VERSION"))
{
MessageBox.Show(@"No recently downloaded release attempts found in C:\ProgramData\Salt Project\Salt\srv\salt\winfor\VERSION\", "No recent installation attempts found!", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
else
{
StringBuilder sb = new();
string release_version = File.ReadAllText(@"C:\ProgramData\Salt Project\Salt\srv\salt\winfor\VERSION").TrimEnd();
string status = "";
string log_file = $@"C:\winfor-saltstack-{release_version}.log";
string download_log = $@"C:\winfor-saltstack-downloads-{release_version}.log";
string wsl_log = $@"C:\winfor-wsl.log";
if (File.Exists(log_file))
{
string[] contents = File.ReadAllLines(log_file);
string[] splits = contents[0].Split('[', ']');
string pid = splits[5];
string error_string = $"[ERROR ][{pid}]";
foreach (string line in contents)
{
if (line.Contains($"[ERROR ][{pid}]"))
{
if (line.Contains($"[ERROR ][{pid}] Can't"))
{
continue;
}
else if (line.Contains($"[ERROR ][{pid}] Command '$installedVersion' failed with return code: 1"))
{ }
else
{
sb.Append($"{line}\n");
}
}
}
}
List<string> logfiles = new()
StringBuilder errors = new();
StringBuilder results = new();
string version_file = @"C:\ProgramData\Salt Project\Salt\srv\salt\winfor\VERSION";
string release_version = File.ReadAllText($"{version_file}").TrimEnd();
string log_file = $@"C:\winfor-saltstack-{release_version}.log";
string download_log = $@"C:\winfor-saltstack-downloads-{release_version}.log";
string wsl_log = $@"C:\winfor-wsl.log";
List<string> logfiles = new()
{
log_file,
download_log,
wsl_log
};
try
{
foreach (string log in logfiles)
{
if (File.Exists(log))
{
status += $"{log}\n";
status += "_________________________________________________\n\n";
status += Parse_Log(log, "Summary for");
status += "_________________________________________________\n\n";
string[] contents = File.ReadAllLines(log);
string[] splits = contents[1].Split('[', ']');
string pid = splits[5];
string error_string = $"[ERROR ][{pid}]";
int log_length = log.Length;
results.Append(new String('\u2014', 40) + "\r");
results.Append($"\n{log}\r");
results.Append(new String('\u2014', 40) + "\r\n");
string log_results = (Parse_Log(log, "Summary for", 7));
log_results = log_results.Replace("Summary for local\r", "");
log_results = log_results.Replace("------------\r", "");
log_results = log_results.Replace("--Succeeded", "Succeeded");
log_results = log_results.Replace("-Succeeded", "Succeeded");
log_results = log_results.Replace("--Total", "Total");
log_results = log_results.Replace("-Total", "Total");
results.Append(log_results);
errors.Append(new String('\u2014', 40) + "\r");
errors.Append($"\n{log}\r");
errors.Append(new String('\u2014', 40) + "\r\n");
string error = Parse_Log(log, $"{error_string}", 1);
error = error.Replace(@"\r\n", "\n");
errors.Append(error);
}
}
if (status == "")
}
catch (Exception ex)
{
OutputExpander.IsExpanded = true;
Console_Output($"No recently downloaded release attempts found in {version_file}:\n {ex}");
}
return (results, errors);
}
private void Results_Button(object sender, RoutedEventArgs e)
// Parses the available logs for the SaltStack and WSL installs to determine its summary
{
string version_file = @"C:\ProgramData\Salt Project\Salt\srv\salt\winfor\VERSION";
string release_version = File.ReadAllText($"{version_file}").TrimEnd();
try
{
(StringBuilder results, StringBuilder errors) = Process_Results();
if (results.Length == 0)
{
status += $"No log files found for {release_version}";
MessageBox.Show($"Most recent downloaded version - {release_version}\n\n{status}", $"Results for {release_version}", MessageBoxButton.OK);
MessageBox.Show($"The most recent attempt at installation\nwas for version {release_version}.\n\nNo log files were found for this release.", $"No log file found for {release_version}", MessageBoxButton.OK, MessageBoxImage.Exclamation);
return;
}
else
{
MessageBox.Show($"Most recent downloaded version - {release_version}\n\n{status}", $"Results for {release_version}", MessageBoxButton.OK);
}
if (sb.Length != 0)
{
ErrorWindow w = new(sb.ToString())
ResultsWindow resultsWindow = new(results, errors)
{
Owner = this
};
w.Show();
resultsWindow.Show();
}
}
catch (Exception ex)
{
OutputExpander.IsExpanded = true;
Console_Output($"Unable to display results:\n{ex}");
}
}
private void Check_DistroVersion(object sender, RoutedEventArgs e)
// Checks the current environment to see if Win-FOR is installed and provide its version
Expand Down Expand Up @@ -1937,7 +1942,7 @@ public static List<int> Find_AllLineNumbers(string[] content, string search_term
}
return line_numbers;
}
private static string Parse_Log(string logfile, string search_text)
private static string Parse_Log(string logfile, string search_text, int context)
// The function for actually parsing the log file provided and searching for the given text
{
StringBuilder summary = new();
Expand All @@ -1950,9 +1955,9 @@ private static string Parse_Log(string logfile, string search_text)
List<int> line_numbers = Find_AllLineNumbers(contents, search_text);
foreach (int number in line_numbers)
{
for (int line = number; line < (number + 7); line++)
for (int line = number; line < (number + context); line++)
{
summary.Append($"{contents[line]}\n");
summary.Append($"{contents[line]}\r");
}
summary.Append('\n');
}
Expand Down
22 changes: 22 additions & 0 deletions WinFOR-Customizer/ResultsWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Window x:Class="WinFOR_Customizer.ResultsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WinFOR_Customizer"
mc:Ignorable="d"
Title="Installation Results" SizeToContent="WidthandHeight" ResizeMode="CanMinimize"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MaxWidth="800"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" MaxHeight="600"/>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<TextBox x:Name="ResultsTextBox" HorizontalAlignment="Center" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="Auto" Height="Auto" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" IsReadOnly="True" AcceptsReturn="True" BorderBrush="{x:Null}" BorderThickness="0" FontSize="14"/>
<Button x:Name="ShowErrors" Content="Display Failed / Errors" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="2" Click="Display_Errors"/>

</Grid>
</Window>
40 changes: 40 additions & 0 deletions WinFOR-Customizer/ResultsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Text;
using System.Windows;

namespace WinFOR_Customizer
{
/// <summary>
/// Interaction logic for ResultsWindow.xaml
/// </summary>
public partial class ResultsWindow : Window
{
public ResultsWindow(StringBuilder results, StringBuilder errors)
{
InitializeComponent();
ShowErrors.Visibility = Visibility.Hidden;
Display_Results(results, errors);
}
public void Display_Results(StringBuilder results, StringBuilder errors)
{
ResultsTextBox.Text = results.ToString();
int errors_lines = errors.ToString().Split('\n').Length;
if (errors_lines > 3)
{
ShowErrors.Visibility = Visibility.Visible;
}
}
public void Display_Errors(object sender, RoutedEventArgs e)
{
(StringBuilder _, StringBuilder errors) = (Application.Current.MainWindow as MainWindow)!.Process_Results();
int errors_lines = errors.ToString().Split('\n').Length;
if (errors_lines > 3)
{
ErrorWindow errorWindow = new(errors)
{
Owner = this
};
errorWindow.Show();
}
}
}
}
2 changes: 1 addition & 1 deletion WinFOR-Customizer/Win-FOR Customizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RepositoryUrl>https://github.com/digitalsleuth/win-for</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<ApplicationIcon>Images\fp.ico</ApplicationIcon>
<Version>6.0.0-rc12</Version>
<Version>6.0.0-rc13</Version>
<AssemblyVersion>6.0.0</AssemblyVersion>
<FileVersion>6.0.0</FileVersion>
<Authors>Corey Forman (digitalsleuth)</Authors>
Expand Down
4 changes: 2 additions & 2 deletions WinFOR-Customizer/app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->

<application xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- <application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
</application> -->

<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<!--
Expand Down

0 comments on commit de8bb51

Please sign in to comment.