Console.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Runtime.CompilerServices;
  7. using System.Text.RegularExpressions;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using com.sun.org.glassfish.gmbal;
  14. using FastReport.DataVisualization.Charting;
  15. using Microsoft.Win32;
  16. using NPOI.XWPF.Usermodel;
  17. namespace InABox.Wpf.Console;
  18. public static class ItemsControlExtensions
  19. {
  20. public static void ScrollIntoView(
  21. this ItemsControl control,
  22. object item)
  23. {
  24. var framework =
  25. control.ItemContainerGenerator.ContainerFromItem(item)
  26. as FrameworkElement;
  27. if (framework == null) return;
  28. framework.BringIntoView();
  29. }
  30. public static void ScrollIntoView(this ItemsControl control)
  31. {
  32. var count = control.Items.Count;
  33. if (count == 0) return;
  34. var item = control.Items[count - 1];
  35. control.ScrollIntoView(item);
  36. }
  37. }
  38. /// <summary>
  39. /// Interaction logic for Console.xaml
  40. /// </summary>
  41. public partial class ConsoleControl : UserControl, INotifyPropertyChanged
  42. {
  43. public static readonly DependencyProperty EnabledProperty
  44. = DependencyProperty.Register(nameof(Enabled), typeof(bool), typeof(ConsoleControl));
  45. private CollectionViewSource _filtered;
  46. public CollectionViewSource Filtered
  47. {
  48. get => _filtered;
  49. set
  50. {
  51. _filtered = value;
  52. OnPropertyChanged();
  53. }
  54. }
  55. public readonly ObservableCollection<LogEntry> LogEntries;
  56. private readonly TimeSpan regexTimeOut = TimeSpan.FromMilliseconds(100);
  57. private Regex? searchRegex;
  58. public event PropertyChangedEventHandler? PropertyChanged;
  59. public event Action? OnLoadLog;
  60. public event Action? OnCloseLog;
  61. public bool _allowLoadLogButton = true;
  62. public bool AllowLoadLogButton
  63. {
  64. get => _allowLoadLogButton;
  65. set
  66. {
  67. _allowLoadLogButton = value;
  68. OnPropertyChanged();
  69. OnPropertyChanged(nameof(ShowLoadLogButton));
  70. OnPropertyChanged(nameof(ShowCloseLogButton));
  71. }
  72. }
  73. private bool _loadedLog = false;
  74. public bool LoadedLog
  75. {
  76. get => _loadedLog;
  77. set
  78. {
  79. _loadedLog = value;
  80. OnPropertyChanged();
  81. OnPropertyChanged(nameof(ShowLoadLogButton));
  82. OnPropertyChanged(nameof(ShowCloseLogButton));
  83. }
  84. }
  85. public bool ShowLoadLogButton => !LoadedLog && AllowLoadLogButton;
  86. public bool ShowCloseLogButton => LoadedLog && AllowLoadLogButton;
  87. public bool Enabled
  88. {
  89. get => (bool)GetValue(EnabledProperty);
  90. set => SetValue(EnabledProperty, value);
  91. }
  92. public ConsoleControl()
  93. {
  94. InitializeComponent();
  95. Filtered = new CollectionViewSource();
  96. LogEntries = new ObservableCollection<LogEntry>();
  97. Filtered.Source = LogEntries;
  98. Filtered.Filter += (sender, args) =>
  99. {
  100. var logEntry = (LogEntry)args.Item;
  101. if (ShowImportant.IsChecked == true && !IsImportant(logEntry))
  102. {
  103. args.Accepted = false;
  104. return;
  105. }
  106. if (UseRegEx.IsChecked == true && searchRegex != null)
  107. args.Accepted = string.IsNullOrWhiteSpace(Search.Text)
  108. || searchRegex.IsMatch(logEntry.DateTime)
  109. || searchRegex.IsMatch(logEntry.Type)
  110. || searchRegex.IsMatch(logEntry.User)
  111. || searchRegex.IsMatch(logEntry.Message);
  112. else
  113. args.Accepted = string.IsNullOrWhiteSpace(Search.Text)
  114. || logEntry.DateTime.Contains(Search.Text)
  115. || logEntry.Type.Contains(Search.Text)
  116. || logEntry.User.Contains(Search.Text)
  117. || logEntry.Message.Contains(Search.Text);
  118. };
  119. }
  120. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  121. {
  122. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  123. }
  124. private static bool IsImportant(LogEntry entry)
  125. {
  126. return entry.Type == "IMPTNT";
  127. }
  128. // DATE TRANSACTION TYPE USERID MESSAGE
  129. [GeneratedRegex("(\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d)\\s*([0-9a-zA-Z]{8}(?:\\-[0-9a-zA-Z]{4}){3}\\-[0-9a-zA-Z]{12})?\\s*(\\S+) (.+)", RegexOptions.Singleline)]
  130. internal static partial Regex LogEntryParseRegex();
  131. public static LogEntry? ParseLogMessage(string logLine)
  132. {
  133. var match = LogEntryParseRegex().Match(logLine);
  134. if (match.Success)
  135. {
  136. var date = match.Groups[1].Value;
  137. if (!Guid.TryParse(match.Groups[2].Value, out var transaction))
  138. {
  139. transaction = Guid.Empty;
  140. }
  141. var type = match.Groups[3].Value;
  142. var remaining = match.Groups[4].Value;
  143. var user = remaining.Length >= 12 ? remaining[..12] : "";
  144. var msg = remaining.Length >= 12 ? remaining[13..] : "";
  145. return new LogEntry
  146. {
  147. DateTime = date,
  148. Type = type,
  149. User = user,
  150. Message = msg,
  151. Transaction = transaction
  152. };
  153. }
  154. else
  155. {
  156. return null;
  157. }
  158. }
  159. public void LoadLogEntries(IEnumerable<string> lines)
  160. {
  161. var logEntries = new List<LogEntry>();
  162. var numberSkipped = 0;
  163. foreach (var line in lines)
  164. {
  165. var logEntry = ParseLogMessage(line ?? "");
  166. if(logEntry is not null)
  167. {
  168. var logType = logEntry.Type;
  169. if (logType == "ERROR" || logType == "INFO" || logType == "IMPTNT")
  170. logEntries.Add(logEntry);
  171. else if (string.IsNullOrWhiteSpace(logType)) numberSkipped++;
  172. }
  173. }
  174. if (numberSkipped > 0)
  175. {
  176. if (logEntries.Count == 0)
  177. SetErrorMessage("File does not contain valid log information!");
  178. else
  179. SetErrorMessage(string.Format("Skipped {0} lines that did not contain valid log information", numberSkipped));
  180. }
  181. Filtered.Source = logEntries;
  182. }
  183. public void LoadLogEntry(string line)
  184. {
  185. var logEntry = ParseLogMessage(line);
  186. if(logEntry is not null)
  187. {
  188. var logType = logEntry.Type;
  189. if (logType == "INFO" || logType == "ERROR" || logType == "IMPTNT")
  190. {
  191. LogEntries.Insert(0, logEntry);
  192. }
  193. }
  194. }
  195. private void Search_KeyDown(object sender, KeyEventArgs e)
  196. {
  197. if (e.Key == Key.Enter && UseRegEx.IsChecked == true)
  198. {
  199. try
  200. {
  201. searchRegex = new Regex(Search.Text, RegexOptions.Compiled, regexTimeOut);
  202. }
  203. catch (ArgumentException)
  204. {
  205. searchRegex = null;
  206. }
  207. Filtered.View.Refresh();
  208. SetSearchStyleNormal();
  209. }
  210. }
  211. private void SetSearchStyleChanged()
  212. {
  213. Search.Background = Brushes.White;
  214. }
  215. private void SetSearchStyleNormal()
  216. {
  217. Search.Background = Brushes.LightYellow;
  218. }
  219. private void Search_TextChanged(object sender, TextChangedEventArgs e)
  220. {
  221. if (UseRegEx.IsChecked != true)
  222. {
  223. Filtered.View.Refresh();
  224. }
  225. else
  226. {
  227. if (string.IsNullOrWhiteSpace(Search.Text))
  228. {
  229. searchRegex = null;
  230. Filtered.View.Refresh();
  231. SetSearchStyleNormal();
  232. }
  233. else
  234. {
  235. SetSearchStyleChanged();
  236. }
  237. }
  238. }
  239. private void UseRegEx_Checked(object sender, RoutedEventArgs e)
  240. {
  241. try
  242. {
  243. searchRegex = new Regex(Search.Text, RegexOptions.Compiled, regexTimeOut);
  244. }
  245. catch (ArgumentException ex)
  246. {
  247. searchRegex = null;
  248. }
  249. Filtered.View.Refresh();
  250. }
  251. private void UseRegEx_Unchecked(object sender, RoutedEventArgs e)
  252. {
  253. searchRegex = null;
  254. Filtered.View.Refresh();
  255. SetSearchStyleNormal();
  256. }
  257. public void SetErrorMessage(string? error)
  258. {
  259. if (string.IsNullOrWhiteSpace(error))
  260. {
  261. Error.Content = "";
  262. Error.Visibility = Visibility.Collapsed;
  263. }
  264. else
  265. {
  266. Error.Content = error;
  267. Error.Visibility = Visibility.Visible;
  268. }
  269. }
  270. private void CloseLog_Click(object sender, RoutedEventArgs e)
  271. {
  272. Filtered.Source = LogEntries;
  273. OnCloseLog?.Invoke();
  274. }
  275. private void ShowImportant_Checked(object sender, RoutedEventArgs e)
  276. {
  277. Filtered.View.Refresh();
  278. }
  279. private void ShowImportant_Unchecked(object sender, RoutedEventArgs e)
  280. {
  281. Filtered.View.Refresh();
  282. }
  283. private void LoadLog_Click(object sender, RoutedEventArgs e)
  284. {
  285. OnLoadLog?.Invoke();
  286. }
  287. }
  288. public abstract class Console : Window
  289. {
  290. public ConsoleControl ConsoleControl { get; set; }
  291. private readonly string Description;
  292. public Console(string description)
  293. {
  294. ConsoleControl = new ConsoleControl();
  295. ConsoleControl.OnLoadLog += ConsoleControl_OnLoadLog;
  296. ConsoleControl.OnCloseLog += ConsoleControl_OnCloseLog;
  297. Content = ConsoleControl;
  298. Height = 800;
  299. Width = 1200;
  300. Loaded += Console_Loaded;
  301. Closing += Console_Closing;
  302. Title = description;
  303. Description = description;
  304. }
  305. private void ConsoleControl_OnCloseLog()
  306. {
  307. Title = Description;
  308. ConsoleControl.LoadedLog = false;
  309. }
  310. private void ConsoleControl_OnLoadLog()
  311. {
  312. var dialog = new OpenFileDialog
  313. {
  314. InitialDirectory = GetLogDirectory()
  315. };
  316. if (dialog.ShowDialog() == true)
  317. {
  318. var lines = File.ReadLines(dialog.FileName);
  319. ConsoleControl.LoadLogEntries(lines);
  320. ConsoleControl.LoadedLog = true;
  321. Title = dialog.FileName;
  322. }
  323. }
  324. protected virtual void OnLoaded()
  325. {
  326. }
  327. protected virtual void OnClosing()
  328. {
  329. }
  330. private void Console_Closing(object? sender, CancelEventArgs e)
  331. {
  332. OnClosing();
  333. }
  334. private void Console_Loaded(object sender, RoutedEventArgs e)
  335. {
  336. OnLoaded();
  337. }
  338. protected abstract string GetLogDirectory();
  339. }