|
@@ -0,0 +1,202 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Collections.ObjectModel;
|
|
|
+using System.ComponentModel;
|
|
|
+using System.IO;
|
|
|
+using System.Runtime.CompilerServices;
|
|
|
+using System.Security.AccessControl;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Windows.Input;
|
|
|
+using com.sun.java.swing.plaf.windows;
|
|
|
+using Microsoft.Win32;
|
|
|
+using Microsoft.Xaml.Behaviors.Core;
|
|
|
+using NPOI.XSSF.UserModel.Charts;
|
|
|
+
|
|
|
+
|
|
|
+namespace PRSServer;
|
|
|
+
|
|
|
+public class OutputData
|
|
|
+{
|
|
|
+ public string Directory { get; set; }
|
|
|
+ public string DisplayData { get; set; }
|
|
|
+ public string LineData { get; set; }
|
|
|
+ public int LineNumber { get; set; }
|
|
|
+
|
|
|
+ public ICommand OpenFileCommand { get; set; }
|
|
|
+
|
|
|
+ public OutputData()
|
|
|
+ {
|
|
|
+ OpenFileCommand = new ActionCommand(OpenFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OpenFile()
|
|
|
+ {
|
|
|
+ logsearcherViewModel.LoadFile(LineNumber, Directory);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+public class logsearcherViewModel : INotifyPropertyChanged
|
|
|
+{
|
|
|
+
|
|
|
+ public string SearchString { get; set; }
|
|
|
+
|
|
|
+ public static int LineBuffer { get; set; }
|
|
|
+
|
|
|
+ public static string Folder { get; set; }
|
|
|
+
|
|
|
+ public ObservableCollection<OutputData> Output { get; set; }
|
|
|
+
|
|
|
+ public ICommand SearchCommand { get; set; }
|
|
|
+
|
|
|
+ public static String FileName { get; set; }
|
|
|
+
|
|
|
+ public static int Line { get; set; }
|
|
|
+
|
|
|
+ public static ObservableCollection<String> FileInfo { get; set; }
|
|
|
+
|
|
|
+ public logsearcherViewModel()
|
|
|
+ {
|
|
|
+ SearchCommand = new ActionCommand(Search);
|
|
|
+
|
|
|
+ Output = new ObservableCollection<OutputData>();
|
|
|
+
|
|
|
+ FileInfo = new ObservableCollection<String>();
|
|
|
+
|
|
|
+ LineBuffer = 10;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void LoadFile(int LineNumber, string FileLocation)
|
|
|
+ {
|
|
|
+ if (FileLocation == "")
|
|
|
+ {
|
|
|
+ FileInfo.Clear();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Line = LineNumber;
|
|
|
+ FileName = FileLocation;
|
|
|
+
|
|
|
+ String[] TempFile = System.IO.File.ReadAllLines(FileName);
|
|
|
+
|
|
|
+ FileInfo.Clear();
|
|
|
+
|
|
|
+ if (LineBuffer < 0)
|
|
|
+ {
|
|
|
+ FileInfo.Add("Please Provide a positive line buffer");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ for (int i = LineNumber - LineBuffer; i < LineNumber + LineBuffer + 1; i++)
|
|
|
+ {
|
|
|
+ if (i > 0 && i < TempFile.Length)
|
|
|
+ {
|
|
|
+ FileInfo.Add(TempFile[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Search()
|
|
|
+ {
|
|
|
+ Output.Clear();
|
|
|
+ Task.Run(() => { SearchDir(SearchString, Folder, SendOutput); });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SendOutput(string text, string fileDirectory, int LineNumber, bool Succeed)
|
|
|
+ {
|
|
|
+ App.Current.Dispatcher.Invoke(() =>
|
|
|
+ {
|
|
|
+ OutputData data = new OutputData();
|
|
|
+ data.Directory = fileDirectory;
|
|
|
+ data.DisplayData = Convert.ToString(LineNumber) + text;
|
|
|
+ data.LineNumber = LineNumber;
|
|
|
+
|
|
|
+ if (Succeed) data.LineData = "O";
|
|
|
+ else data.LineData = "X";
|
|
|
+ if (fileDirectory != "") data.LineData = " ";
|
|
|
+ data.LineData += " ";
|
|
|
+
|
|
|
+ Output.Add(data);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SearchDir(string keyword, string folderDirectory, Action<String, String, int, bool> report)
|
|
|
+ {
|
|
|
+ if (keyword == "")
|
|
|
+ {
|
|
|
+ report("No keyword specified", "", 0, false);
|
|
|
+ }
|
|
|
+ else if (keyword == " ")
|
|
|
+ {
|
|
|
+ report("Invalid keyword specified", "", 0, false);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(@folderDirectory))
|
|
|
+ {
|
|
|
+ report("Directory does not exist: " + folderDirectory, "", 0, false);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ string path = @folderDirectory;
|
|
|
+
|
|
|
+ string[] logFiles = Directory.GetFiles(path, "*.log");
|
|
|
+
|
|
|
+ for (int i = logFiles.Length - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ string newPath = logFiles[i];
|
|
|
+ string fileName = Path.GetFileName(newPath);
|
|
|
+ List<(string, int)> results = new List<(string, int)>();
|
|
|
+
|
|
|
+ string[] CurrentFile = File.ReadAllLines(newPath);
|
|
|
+ for (int j = 0; j < CurrentFile.Length; j++)
|
|
|
+ {
|
|
|
+ string line = CurrentFile[j];
|
|
|
+
|
|
|
+ if (Regex.IsMatch(line, keyword))
|
|
|
+ {
|
|
|
+ results.Add((line, j));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (results.Count > 0)
|
|
|
+ {
|
|
|
+ report($"{fileName}: found {results.Count} matches", "", 0, true);
|
|
|
+
|
|
|
+ for (int j = 0; j < results.Count; j++)
|
|
|
+ {
|
|
|
+ (string, int) result = results[j];
|
|
|
+ report(" " + result.Item1, newPath, result.Item2, false);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ report($"{fileName}: found no matches", "", 0, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
+
|
|
|
+ protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
|
|
|
+ {
|
|
|
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
+ }
|
|
|
+
|
|
|
+ protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
|
|
+ {
|
|
|
+ if (EqualityComparer<T>.Default.Equals(field, value)) return false;
|
|
|
+ field = value;
|
|
|
+ OnPropertyChanged(propertyName);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|