Console.xaml.cs 9.8 KB

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