From 49b96d926f9026ddde31a465f63e61df10616df2 Mon Sep 17 00:00:00 2001 From: ruri Date: Sat, 21 Dec 2019 01:44:55 +0100 Subject: [PATCH] Refactored Logging System --- OpenBullet/Globals.cs | 100 +-------- OpenBullet/LogWindow.xaml | 2 +- OpenBullet/LogWindow.xaml.cs | 6 +- OpenBullet/LoggerViewModel.cs | 190 ++++++++++++++++++ OpenBullet/MainWindow.xaml.cs | 38 ++-- OpenBullet/OBLog.cs | 48 ----- OpenBullet/OpenBullet.csproj | 2 +- .../ViewModels/ConfigManagerViewModel.cs | 4 +- .../Views/Dialogs/DialogAddProxies.xaml.cs | 10 +- .../Views/Dialogs/DialogShowLog.xaml.cs | 4 +- .../Views/Main/Configs/ConfigManager.xaml.cs | 46 ++--- OpenBullet/Views/Main/Configs/Stacker.xaml.cs | 54 ++--- OpenBullet/Views/Main/ConfigsSection.xaml.cs | 6 +- OpenBullet/Views/Main/HitsDB.xaml.cs | 44 ++-- OpenBullet/Views/Main/Runner/Runner.xaml.cs | 44 ++-- OpenBullet/Views/Main/Tools/Database.xaml.cs | 6 +- OpenBullet/Views/Main/WordlistManager.xaml.cs | 16 +- RuriLib/Interfaces/ILogger.cs | 10 +- 18 files changed, 341 insertions(+), 289 deletions(-) create mode 100644 OpenBullet/LoggerViewModel.cs delete mode 100644 OpenBullet/OBLog.cs diff --git a/OpenBullet/Globals.cs b/OpenBullet/Globals.cs index 9808021..d3cc84e 100644 --- a/OpenBullet/Globals.cs +++ b/OpenBullet/Globals.cs @@ -12,25 +12,6 @@ using System.Windows.Media; namespace OpenBullet { - public enum Components - { - Main, - RunnerManager, - Runner, - ProxyManager, - WordlistManager, - HitsDB, - ConfigManager, - Stacker, - OtherOptions, - Settings, - ListGenerator, - SeleniumTools, - Database, - ComboSuite, - About - } - public static class Globals { // Version @@ -41,7 +22,7 @@ namespace OpenBullet // Log Window public static LogWindow logWindow; - public static OBLog log = new OBLog(); + public static LoggerViewModel logger = new LoggerViewModel(); // Constant file paths public static string dataBaseFile = @"DB/OpenBullet.db"; @@ -62,84 +43,7 @@ namespace OpenBullet // Runners public static ObservableCollection Runners = new ObservableCollection(); - // Logging Methods - public static void Log(Components component, LogLevel level, string message, bool prompt = false, int timeout = 0) - { - if (prompt) - { - if (timeout == 0) - { - MessageBox.Show(message, level.ToString()); - } - else - { - var w = new System.Windows.Forms.Form() { Size = new System.Drawing.Size(0, 0) }; - Task.Delay(TimeSpan.FromSeconds(timeout)) - .ContinueWith((t) => w.Close(), TaskScheduler.FromCurrentSynchronizationContext()); - - System.Windows.Forms.MessageBox.Show(w, message, level.ToString()); - } - } - - if (!CanLog()) return; - App.Current.Dispatcher.Invoke(new Action(() => - { - var entry = new LogEntry(component.ToString(), message, level); - InsertEntry(entry); - LogToFile(entry); - })); - } - - public static void LogInfo(Components component, string message, bool prompt = false, int timeout = 0) - { - Log(component, LogLevel.Info, message, prompt, timeout); - } - - public static void LogWarning(Components component, string message, bool prompt = false, int timeout = 0) - { - Log(component, LogLevel.Warning, message, prompt, timeout); - } - - public static void LogError(Components component, string message, bool prompt = false, int timeout = 0) - { - Log(component, LogLevel.Error, message, prompt, timeout); - } - - private static bool CanLog() - { - try - { - return obSettings.General.EnableLogging; - } - catch { return false; } - } - - private static void InsertEntry(LogEntry entry) - { - try - { - log.List.Insert(0, entry); - - var count = log.List.Count; - if (count > obSettings.General.LogBufferSize) - { - log.List.RemoveAt(count - 1); - } - } - catch { } - } - - private static void LogToFile(LogEntry entry) - { - try - { - if(obSettings.General.LogToFile) - File.AppendAllText(logFile, $"[{entry.LogTime}] ({entry.LogLevel}) {entry.LogComponent} - " + entry.LogString + Environment.NewLine); - } - catch { } - } - - public static System.Windows.Media.Color GetColor(string propertyName) + public static Color GetColor(string propertyName) { try { return ((SolidColorBrush)App.Current.Resources[propertyName]).Color; } catch { return ((SolidColorBrush)App.Current.Resources["ForegroundMain"]).Color; } diff --git a/OpenBullet/LogWindow.xaml b/OpenBullet/LogWindow.xaml index dd81155..43ac890 100644 --- a/OpenBullet/LogWindow.xaml +++ b/OpenBullet/LogWindow.xaml @@ -19,7 +19,7 @@ - +