From e31f14e60d94ab655513d9b7d962d6b9570b0f69 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 13 Apr 2025 17:11:36 +0800 Subject: [PATCH] Use local reference instead --- Flow.Launcher.Infrastructure/Http/Http.cs | 26 ++++++++++--------- .../Image/ImageLoader.cs | 21 ++++++--------- Flow.Launcher.Infrastructure/Logger/Log.cs | 6 ----- Flow.Launcher.Infrastructure/Stopwatch.cs | 21 +++++++-------- .../Storage/BinaryStorage.cs | 6 +++-- .../Storage/FlowLauncherJsonStorage.cs | 11 +++----- .../Storage/JsonStorage.cs | 4 ++- .../Storage/PluginBinaryStorage.cs | 11 +++----- .../Storage/PluginJsonStorage.cs | 11 +++----- 9 files changed, 47 insertions(+), 70 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Http/Http.cs b/Flow.Launcher.Infrastructure/Http/Http.cs index 0f2f302f1..12edf34a4 100644 --- a/Flow.Launcher.Infrastructure/Http/Http.cs +++ b/Flow.Launcher.Infrastructure/Http/Http.cs @@ -1,19 +1,21 @@ -using System.IO; +using System; +using System.IO; using System.Net; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; -using JetBrains.Annotations; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; -using System; -using System.Threading; using Flow.Launcher.Plugin; -using CommunityToolkit.Mvvm.DependencyInjection; +using JetBrains.Annotations; namespace Flow.Launcher.Infrastructure.Http { public static class Http { + private static readonly string ClassName = nameof(Http); + private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko"; private static readonly HttpClient client = new(); @@ -33,7 +35,7 @@ namespace Flow.Launcher.Infrastructure.Http public static HttpProxy Proxy { - private get { return proxy; } + private get => proxy; set { proxy = value; @@ -77,7 +79,7 @@ namespace Flow.Launcher.Infrastructure.Http catch (UriFormatException e) { Ioc.Default.GetRequiredService().ShowMsg("Please try again", "Unable to parse Http Proxy"); - Log.Exception("Flow.Launcher.Infrastructure.Http", "Unable to parse Uri", e); + Log.Exception(ClassName, "Unable to parse Uri", e); } } @@ -133,7 +135,7 @@ namespace Flow.Launcher.Infrastructure.Http } catch (HttpRequestException e) { - Log.Exception("Infrastructure.Http", "Http Request Error", e, "DownloadAsync"); + Log.Exception(ClassName, "Http Request Error", e, "DownloadAsync"); throw; } } @@ -146,7 +148,7 @@ namespace Flow.Launcher.Infrastructure.Http /// The Http result as string. Null if cancellation requested public static Task GetAsync([NotNull] string url, CancellationToken token = default) { - Log.Debug($"|Http.Get|Url <{url}>"); + Log.Debug(ClassName, $"Url <{url}>"); return GetAsync(new Uri(url), token); } @@ -158,7 +160,7 @@ namespace Flow.Launcher.Infrastructure.Http /// The Http result as string. Null if cancellation requested public static async Task GetAsync([NotNull] Uri url, CancellationToken token = default) { - Log.Debug($"|Http.Get|Url <{url}>"); + Log.Debug(ClassName, $"Url <{url}>"); using var response = await client.GetAsync(url, token); var content = await response.Content.ReadAsStringAsync(token); if (response.StatusCode != HttpStatusCode.OK) @@ -190,7 +192,7 @@ namespace Flow.Launcher.Infrastructure.Http public static async Task GetStreamAsync([NotNull] Uri url, CancellationToken token = default) { - Log.Debug($"|Http.Get|Url <{url}>"); + Log.Debug(ClassName, $"Url <{url}>"); return await client.GetStreamAsync(url, token); } @@ -201,7 +203,7 @@ namespace Flow.Launcher.Infrastructure.Http public static async Task GetResponseAsync([NotNull] Uri url, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken token = default) { - Log.Debug($"|Http.Get|Url <{url}>"); + Log.Debug(ClassName, $"Url <{url}>"); return await client.GetAsync(url, completionOption, token); } diff --git a/Flow.Launcher.Infrastructure/Image/ImageLoader.cs b/Flow.Launcher.Infrastructure/Image/ImageLoader.cs index 9e31d2b4e..a49385a02 100644 --- a/Flow.Launcher.Infrastructure/Image/ImageLoader.cs +++ b/Flow.Launcher.Infrastructure/Image/ImageLoader.cs @@ -7,9 +7,8 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Media; using System.Windows.Media.Imaging; -using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Storage; -using Flow.Launcher.Plugin; using SharpVectors.Converters; using SharpVectors.Renderers.Wpf; @@ -17,10 +16,6 @@ namespace Flow.Launcher.Infrastructure.Image { public static class ImageLoader { - // We should not initialize API in static constructor because it will create another API instance - private static IPublicAPI api = null; - private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService(); - private static readonly string ClassName = nameof(ImageLoader); private static readonly ImageCache ImageCache = new(); @@ -58,14 +53,14 @@ namespace Flow.Launcher.Infrastructure.Image _ = Task.Run(async () => { - await API.StopwatchLogInfoAsync(ClassName, "Preload images cost", async () => + await Stopwatch.InfoAsync(ClassName, "Preload images cost", async () => { foreach (var (path, isFullImage) in usage) { await LoadAsync(path, isFullImage); } }); - API.LogInfo(ClassName, $"Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}"); + Log.Info(ClassName, $"Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}"); }); } @@ -81,7 +76,7 @@ namespace Flow.Launcher.Infrastructure.Image } catch (System.Exception e) { - API.LogException(ClassName, "Failed to save image cache to file", e); + Log.Exception(ClassName, "Failed to save image cache to file", e); } finally { @@ -176,8 +171,8 @@ namespace Flow.Launcher.Infrastructure.Image } catch (System.Exception e2) { - API.LogException(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e); - API.LogException(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2); + Log.Exception(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e); + Log.Exception(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2); ImageSource image = ImageCache[Constant.MissingImgIcon, false]; ImageCache[path, false] = image; @@ -243,7 +238,7 @@ namespace Flow.Launcher.Infrastructure.Image { image = Image; type = ImageType.Error; - API.LogException(ClassName, $"Failed to load image file from path {path}: {ex.Message}", ex); + Log.Exception(ClassName, $"Failed to load image file from path {path}: {ex.Message}", ex); } } else @@ -267,7 +262,7 @@ namespace Flow.Launcher.Infrastructure.Image { image = Image; type = ImageType.Error; - API.LogException(ClassName, $"Failed to load SVG image from path {path}: {ex.Message}", ex); + Log.Exception(ClassName, $"Failed to load SVG image from path {path}: {ex.Message}", ex); } } else diff --git a/Flow.Launcher.Infrastructure/Logger/Log.cs b/Flow.Launcher.Infrastructure/Logger/Log.cs index 807d631c7..25cfbbd3d 100644 --- a/Flow.Launcher.Infrastructure/Logger/Log.cs +++ b/Flow.Launcher.Infrastructure/Logger/Log.cs @@ -227,12 +227,6 @@ namespace Flow.Launcher.Infrastructure.Logger { LogInternal(LogLevel.Warn, className, message, methodName); } - - /// Example: "|ClassName.MethodName|Message" - public static void Warn(string message) - { - LogInternal(message, LogLevel.Warn); - } } public enum LOGLEVEL diff --git a/Flow.Launcher.Infrastructure/Stopwatch.cs b/Flow.Launcher.Infrastructure/Stopwatch.cs index 784d323fe..870e0fe26 100644 --- a/Flow.Launcher.Infrastructure/Stopwatch.cs +++ b/Flow.Launcher.Infrastructure/Stopwatch.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Threading.Tasks; using Flow.Launcher.Infrastructure.Logger; @@ -9,54 +10,50 @@ namespace Flow.Launcher.Infrastructure /// /// This stopwatch will appear only in Debug mode /// - public static long Debug(string message, Action action) + public static long Debug(string className, string message, Action action, [CallerMemberName] string methodName = "") { var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); action(); stopWatch.Stop(); var milliseconds = stopWatch.ElapsedMilliseconds; - string info = $"{message} <{milliseconds}ms>"; - Log.Debug(info); + Log.Debug(className, $"{message} <{milliseconds}ms>", methodName); return milliseconds; } /// /// This stopwatch will appear only in Debug mode /// - public static async Task DebugAsync(string message, Func action) + public static async Task DebugAsync(string className, string message, Func action, [CallerMemberName] string methodName = "") { var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); await action(); stopWatch.Stop(); var milliseconds = stopWatch.ElapsedMilliseconds; - string info = $"{message} <{milliseconds}ms>"; - Log.Debug(info); + Log.Debug(className, $"{message} <{milliseconds}ms>", methodName); return milliseconds; } - public static long Normal(string message, Action action) + public static long Info(string className, string message, Action action, [CallerMemberName] string methodName = "") { var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); action(); stopWatch.Stop(); var milliseconds = stopWatch.ElapsedMilliseconds; - string info = $"{message} <{milliseconds}ms>"; - Log.Info(info); + Log.Info(className, $"{message} <{milliseconds}ms>", methodName); return milliseconds; } - public static async Task NormalAsync(string message, Func action) + public static async Task InfoAsync(string className, string message, Func action, [CallerMemberName] string methodName = "") { var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); await action(); stopWatch.Stop(); var milliseconds = stopWatch.ElapsedMilliseconds; - string info = $"{message} <{milliseconds}ms>"; - Log.Info(info); + Log.Info(className, $"{message} <{milliseconds}ms>", methodName); return milliseconds; } } diff --git a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs index 8ff10816c..48e6b5523 100644 --- a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs @@ -20,6 +20,8 @@ namespace Flow.Launcher.Infrastructure.Storage /// public class BinaryStorage : ISavable { + private static readonly string ClassName = "BinaryStorage"; + protected T? Data; public const string FileSuffix = ".cache"; @@ -59,7 +61,7 @@ namespace Flow.Launcher.Infrastructure.Storage { if (new FileInfo(FilePath).Length == 0) { - Log.Error($"|BinaryStorage.TryLoad|Zero length cache file <{FilePath}>"); + Log.Error(ClassName, $"Zero length cache file <{FilePath}>"); Data = defaultData; await SaveAsync(); } @@ -69,7 +71,7 @@ namespace Flow.Launcher.Infrastructure.Storage } else { - Log.Info("|BinaryStorage.TryLoad|Cache file not exist, load default data"); + Log.Info(ClassName, "Cache file not exist, load default data"); Data = defaultData; await SaveAsync(); } diff --git a/Flow.Launcher.Infrastructure/Storage/FlowLauncherJsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/FlowLauncherJsonStorage.cs index ca78b2f20..158e0cdf5 100644 --- a/Flow.Launcher.Infrastructure/Storage/FlowLauncherJsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/FlowLauncherJsonStorage.cs @@ -1,8 +1,7 @@ using System.IO; using System.Threading.Tasks; -using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; -using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedCommands; namespace Flow.Launcher.Infrastructure.Storage @@ -11,10 +10,6 @@ namespace Flow.Launcher.Infrastructure.Storage { private static readonly string ClassName = "FlowLauncherJsonStorage"; - // We should not initialize API in static constructor because it will create another API instance - private static IPublicAPI api = null; - private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService(); - public FlowLauncherJsonStorage() { DirectoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName); @@ -32,7 +27,7 @@ namespace Flow.Launcher.Infrastructure.Storage } catch (System.Exception e) { - API.LogException(ClassName, $"Failed to save FL settings to path: {FilePath}", e); + Log.Exception(ClassName, $"Failed to save FL settings to path: {FilePath}", e); } } @@ -44,7 +39,7 @@ namespace Flow.Launcher.Infrastructure.Storage } catch (System.Exception e) { - API.LogException(ClassName, $"Failed to save FL settings to path: {FilePath}", e); + Log.Exception(ClassName, $"Failed to save FL settings to path: {FilePath}", e); } } } diff --git a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs index f283be59e..0b10382ee 100644 --- a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs @@ -16,6 +16,8 @@ namespace Flow.Launcher.Infrastructure.Storage /// public class JsonStorage : ISavable where T : new() { + private static readonly string ClassName = "JsonStorage"; + protected T? Data; // need a new directory name @@ -104,7 +106,7 @@ namespace Flow.Launcher.Infrastructure.Storage private void RestoreBackup() { - Log.Info($"|JsonStorage.Load|Failed to load settings.json, {BackupFilePath} restored successfully"); + Log.Info(ClassName, $"Failed to load settings.json, {BackupFilePath} restored successfully"); if (File.Exists(FilePath)) File.Replace(BackupFilePath, FilePath, null); diff --git a/Flow.Launcher.Infrastructure/Storage/PluginBinaryStorage.cs b/Flow.Launcher.Infrastructure/Storage/PluginBinaryStorage.cs index d18060e3d..01da96d62 100644 --- a/Flow.Launcher.Infrastructure/Storage/PluginBinaryStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/PluginBinaryStorage.cs @@ -1,7 +1,6 @@ using System.IO; using System.Threading.Tasks; -using CommunityToolkit.Mvvm.DependencyInjection; -using Flow.Launcher.Plugin; +using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Plugin.SharedCommands; namespace Flow.Launcher.Infrastructure.Storage @@ -10,10 +9,6 @@ namespace Flow.Launcher.Infrastructure.Storage { private static readonly string ClassName = "PluginBinaryStorage"; - // We should not initialize API in static constructor because it will create another API instance - private static IPublicAPI api = null; - private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService(); - public PluginBinaryStorage(string cacheName, string cacheDirectory) { DirectoryPath = cacheDirectory; @@ -30,7 +25,7 @@ namespace Flow.Launcher.Infrastructure.Storage } catch (System.Exception e) { - API.LogException(ClassName, $"Failed to save plugin caches to path: {FilePath}", e); + Log.Exception(ClassName, $"Failed to save plugin caches to path: {FilePath}", e); } } @@ -42,7 +37,7 @@ namespace Flow.Launcher.Infrastructure.Storage } catch (System.Exception e) { - API.LogException(ClassName, $"Failed to save plugin caches to path: {FilePath}", e); + Log.Exception(ClassName, $"Failed to save plugin caches to path: {FilePath}", e); } } } diff --git a/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs index e8cbd70fb..147152949 100644 --- a/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs @@ -1,8 +1,7 @@ using System.IO; using System.Threading.Tasks; -using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; -using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedCommands; namespace Flow.Launcher.Infrastructure.Storage @@ -14,10 +13,6 @@ namespace Flow.Launcher.Infrastructure.Storage private static readonly string ClassName = "PluginJsonStorage"; - // We should not initialize API in static constructor because it will create another API instance - private static IPublicAPI api = null; - private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService(); - public PluginJsonStorage() { // C# related, add python related below @@ -42,7 +37,7 @@ namespace Flow.Launcher.Infrastructure.Storage } catch (System.Exception e) { - API.LogException(ClassName, $"Failed to save plugin settings to path: {FilePath}", e); + Log.Exception(ClassName, $"Failed to save plugin settings to path: {FilePath}", e); } } @@ -54,7 +49,7 @@ namespace Flow.Launcher.Infrastructure.Storage } catch (System.Exception e) { - API.LogException(ClassName, $"Failed to save plugin settings to path: {FilePath}", e); + Log.Exception(ClassName, $"Failed to save plugin settings to path: {FilePath}", e); } } }