Skip to content

Commit e31f14e

Browse files
committed
Use local reference instead
1 parent 230df80 commit e31f14e

File tree

9 files changed

+47
-70
lines changed

9 files changed

+47
-70
lines changed

Flow.Launcher.Infrastructure/Http/Http.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Net;
34
using System.Net.Http;
5+
using System.Threading;
46
using System.Threading.Tasks;
5-
using JetBrains.Annotations;
7+
using CommunityToolkit.Mvvm.DependencyInjection;
68
using Flow.Launcher.Infrastructure.Logger;
79
using Flow.Launcher.Infrastructure.UserSettings;
8-
using System;
9-
using System.Threading;
1010
using Flow.Launcher.Plugin;
11-
using CommunityToolkit.Mvvm.DependencyInjection;
11+
using JetBrains.Annotations;
1212

1313
namespace Flow.Launcher.Infrastructure.Http
1414
{
1515
public static class Http
1616
{
17+
private static readonly string ClassName = nameof(Http);
18+
1719
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
1820

1921
private static readonly HttpClient client = new();
@@ -33,7 +35,7 @@ static Http()
3335

3436
public static HttpProxy Proxy
3537
{
36-
private get { return proxy; }
38+
private get => proxy;
3739
set
3840
{
3941
proxy = value;
@@ -77,7 +79,7 @@ var userName when string.IsNullOrEmpty(userName) =>
7779
catch (UriFormatException e)
7880
{
7981
Ioc.Default.GetRequiredService<IPublicAPI>().ShowMsg("Please try again", "Unable to parse Http Proxy");
80-
Log.Exception("Flow.Launcher.Infrastructure.Http", "Unable to parse Uri", e);
82+
Log.Exception(ClassName, "Unable to parse Uri", e);
8183
}
8284
}
8385

@@ -133,7 +135,7 @@ public static async Task DownloadAsync([NotNull] string url, [NotNull] string fi
133135
}
134136
catch (HttpRequestException e)
135137
{
136-
Log.Exception("Infrastructure.Http", "Http Request Error", e, "DownloadAsync");
138+
Log.Exception(ClassName, "Http Request Error", e, "DownloadAsync");
137139
throw;
138140
}
139141
}
@@ -146,7 +148,7 @@ public static async Task DownloadAsync([NotNull] string url, [NotNull] string fi
146148
/// <returns>The Http result as string. Null if cancellation requested</returns>
147149
public static Task<string> GetAsync([NotNull] string url, CancellationToken token = default)
148150
{
149-
Log.Debug($"|Http.Get|Url <{url}>");
151+
Log.Debug(ClassName, $"Url <{url}>");
150152
return GetAsync(new Uri(url), token);
151153
}
152154

@@ -158,7 +160,7 @@ public static Task<string> GetAsync([NotNull] string url, CancellationToken toke
158160
/// <returns>The Http result as string. Null if cancellation requested</returns>
159161
public static async Task<string> GetAsync([NotNull] Uri url, CancellationToken token = default)
160162
{
161-
Log.Debug($"|Http.Get|Url <{url}>");
163+
Log.Debug(ClassName, $"Url <{url}>");
162164
using var response = await client.GetAsync(url, token);
163165
var content = await response.Content.ReadAsStringAsync(token);
164166
if (response.StatusCode != HttpStatusCode.OK)
@@ -190,7 +192,7 @@ public static Task<Stream> GetStreamAsync([NotNull] string url,
190192
public static async Task<Stream> GetStreamAsync([NotNull] Uri url,
191193
CancellationToken token = default)
192194
{
193-
Log.Debug($"|Http.Get|Url <{url}>");
195+
Log.Debug(ClassName, $"Url <{url}>");
194196
return await client.GetStreamAsync(url, token);
195197
}
196198

@@ -201,7 +203,7 @@ public static async Task<HttpResponseMessage> GetResponseAsync(string url, HttpC
201203
public static async Task<HttpResponseMessage> GetResponseAsync([NotNull] Uri url, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead,
202204
CancellationToken token = default)
203205
{
204-
Log.Debug($"|Http.Get|Url <{url}>");
206+
Log.Debug(ClassName, $"Url <{url}>");
205207
return await client.GetAsync(url, completionOption, token);
206208
}
207209

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@
77
using System.Threading.Tasks;
88
using System.Windows.Media;
99
using System.Windows.Media.Imaging;
10-
using CommunityToolkit.Mvvm.DependencyInjection;
10+
using Flow.Launcher.Infrastructure.Logger;
1111
using Flow.Launcher.Infrastructure.Storage;
12-
using Flow.Launcher.Plugin;
1312
using SharpVectors.Converters;
1413
using SharpVectors.Renderers.Wpf;
1514

1615
namespace Flow.Launcher.Infrastructure.Image
1716
{
1817
public static class ImageLoader
1918
{
20-
// We should not initialize API in static constructor because it will create another API instance
21-
private static IPublicAPI api = null;
22-
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
23-
2419
private static readonly string ClassName = nameof(ImageLoader);
2520

2621
private static readonly ImageCache ImageCache = new();
@@ -58,14 +53,14 @@ public static async Task InitializeAsync()
5853

5954
_ = Task.Run(async () =>
6055
{
61-
await API.StopwatchLogInfoAsync(ClassName, "Preload images cost", async () =>
56+
await Stopwatch.InfoAsync(ClassName, "Preload images cost", async () =>
6257
{
6358
foreach (var (path, isFullImage) in usage)
6459
{
6560
await LoadAsync(path, isFullImage);
6661
}
6762
});
68-
API.LogInfo(ClassName, $"Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
63+
Log.Info(ClassName, $"Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
6964
});
7065
}
7166

@@ -81,7 +76,7 @@ await _storage.SaveAsync(ImageCache.EnumerateEntries()
8176
}
8277
catch (System.Exception e)
8378
{
84-
API.LogException(ClassName, "Failed to save image cache to file", e);
79+
Log.Exception(ClassName, "Failed to save image cache to file", e);
8580
}
8681
finally
8782
{
@@ -176,8 +171,8 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
176171
}
177172
catch (System.Exception e2)
178173
{
179-
API.LogException(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e);
180-
API.LogException(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2);
174+
Log.Exception(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e);
175+
Log.Exception(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2);
181176

182177
ImageSource image = ImageCache[Constant.MissingImgIcon, false];
183178
ImageCache[path, false] = image;
@@ -243,7 +238,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
243238
{
244239
image = Image;
245240
type = ImageType.Error;
246-
API.LogException(ClassName, $"Failed to load image file from path {path}: {ex.Message}", ex);
241+
Log.Exception(ClassName, $"Failed to load image file from path {path}: {ex.Message}", ex);
247242
}
248243
}
249244
else
@@ -267,7 +262,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
267262
{
268263
image = Image;
269264
type = ImageType.Error;
270-
API.LogException(ClassName, $"Failed to load SVG image from path {path}: {ex.Message}", ex);
265+
Log.Exception(ClassName, $"Failed to load SVG image from path {path}: {ex.Message}", ex);
271266
}
272267
}
273268
else

Flow.Launcher.Infrastructure/Logger/Log.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,6 @@ public static void Warn(string className, string message, [CallerMemberName] str
227227
{
228228
LogInternal(LogLevel.Warn, className, message, methodName);
229229
}
230-
231-
/// Example: "|ClassName.MethodName|Message"
232-
public static void Warn(string message)
233-
{
234-
LogInternal(message, LogLevel.Warn);
235-
}
236230
}
237231

238232
public enum LOGLEVEL

Flow.Launcher.Infrastructure/Stopwatch.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23
using System.Threading.Tasks;
34
using Flow.Launcher.Infrastructure.Logger;
45

@@ -9,54 +10,50 @@ public static class Stopwatch
910
/// <summary>
1011
/// This stopwatch will appear only in Debug mode
1112
/// </summary>
12-
public static long Debug(string message, Action action)
13+
public static long Debug(string className, string message, Action action, [CallerMemberName] string methodName = "")
1314
{
1415
var stopWatch = new System.Diagnostics.Stopwatch();
1516
stopWatch.Start();
1617
action();
1718
stopWatch.Stop();
1819
var milliseconds = stopWatch.ElapsedMilliseconds;
19-
string info = $"{message} <{milliseconds}ms>";
20-
Log.Debug(info);
20+
Log.Debug(className, $"{message} <{milliseconds}ms>", methodName);
2121
return milliseconds;
2222
}
2323

2424
/// <summary>
2525
/// This stopwatch will appear only in Debug mode
2626
/// </summary>
27-
public static async Task<long> DebugAsync(string message, Func<Task> action)
27+
public static async Task<long> DebugAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "")
2828
{
2929
var stopWatch = new System.Diagnostics.Stopwatch();
3030
stopWatch.Start();
3131
await action();
3232
stopWatch.Stop();
3333
var milliseconds = stopWatch.ElapsedMilliseconds;
34-
string info = $"{message} <{milliseconds}ms>";
35-
Log.Debug(info);
34+
Log.Debug(className, $"{message} <{milliseconds}ms>", methodName);
3635
return milliseconds;
3736
}
3837

39-
public static long Normal(string message, Action action)
38+
public static long Info(string className, string message, Action action, [CallerMemberName] string methodName = "")
4039
{
4140
var stopWatch = new System.Diagnostics.Stopwatch();
4241
stopWatch.Start();
4342
action();
4443
stopWatch.Stop();
4544
var milliseconds = stopWatch.ElapsedMilliseconds;
46-
string info = $"{message} <{milliseconds}ms>";
47-
Log.Info(info);
45+
Log.Info(className, $"{message} <{milliseconds}ms>", methodName);
4846
return milliseconds;
4947
}
5048

51-
public static async Task<long> NormalAsync(string message, Func<Task> action)
49+
public static async Task<long> InfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "")
5250
{
5351
var stopWatch = new System.Diagnostics.Stopwatch();
5452
stopWatch.Start();
5553
await action();
5654
stopWatch.Stop();
5755
var milliseconds = stopWatch.ElapsedMilliseconds;
58-
string info = $"{message} <{milliseconds}ms>";
59-
Log.Info(info);
56+
Log.Info(className, $"{message} <{milliseconds}ms>", methodName);
6057
return milliseconds;
6158
}
6259
}

Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Flow.Launcher.Infrastructure.Storage
2020
/// </remarks>
2121
public class BinaryStorage<T> : ISavable
2222
{
23+
private static readonly string ClassName = "BinaryStorage";
24+
2325
protected T? Data;
2426

2527
public const string FileSuffix = ".cache";
@@ -59,7 +61,7 @@ public async ValueTask<T> TryLoadAsync(T defaultData)
5961
{
6062
if (new FileInfo(FilePath).Length == 0)
6163
{
62-
Log.Error($"|BinaryStorage.TryLoad|Zero length cache file <{FilePath}>");
64+
Log.Error(ClassName, $"Zero length cache file <{FilePath}>");
6365
Data = defaultData;
6466
await SaveAsync();
6567
}
@@ -69,7 +71,7 @@ public async ValueTask<T> TryLoadAsync(T defaultData)
6971
}
7072
else
7173
{
72-
Log.Info("|BinaryStorage.TryLoad|Cache file not exist, load default data");
74+
Log.Info(ClassName, "Cache file not exist, load default data");
7375
Data = defaultData;
7476
await SaveAsync();
7577
}

Flow.Launcher.Infrastructure/Storage/FlowLauncherJsonStorage.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using System.IO;
22
using System.Threading.Tasks;
3-
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Flow.Launcher.Infrastructure.Logger;
44
using Flow.Launcher.Infrastructure.UserSettings;
5-
using Flow.Launcher.Plugin;
65
using Flow.Launcher.Plugin.SharedCommands;
76

87
namespace Flow.Launcher.Infrastructure.Storage
@@ -11,10 +10,6 @@ namespace Flow.Launcher.Infrastructure.Storage
1110
{
1211
private static readonly string ClassName = "FlowLauncherJsonStorage";
1312

14-
// We should not initialize API in static constructor because it will create another API instance
15-
private static IPublicAPI api = null;
16-
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
17-
1813
public FlowLauncherJsonStorage()
1914
{
2015
DirectoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName);
@@ -32,7 +27,7 @@ public FlowLauncherJsonStorage()
3227
}
3328
catch (System.Exception e)
3429
{
35-
API.LogException(ClassName, $"Failed to save FL settings to path: {FilePath}", e);
30+
Log.Exception(ClassName, $"Failed to save FL settings to path: {FilePath}", e);
3631
}
3732
}
3833

@@ -44,7 +39,7 @@ public FlowLauncherJsonStorage()
4439
}
4540
catch (System.Exception e)
4641
{
47-
API.LogException(ClassName, $"Failed to save FL settings to path: {FilePath}", e);
42+
Log.Exception(ClassName, $"Failed to save FL settings to path: {FilePath}", e);
4843
}
4944
}
5045
}

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace Flow.Launcher.Infrastructure.Storage
1616
/// </summary>
1717
public class JsonStorage<T> : ISavable where T : new()
1818
{
19+
private static readonly string ClassName = "JsonStorage";
20+
1921
protected T? Data;
2022

2123
// need a new directory name
@@ -104,7 +106,7 @@ private async ValueTask<T> LoadBackupOrDefaultAsync()
104106

105107
private void RestoreBackup()
106108
{
107-
Log.Info($"|JsonStorage.Load|Failed to load settings.json, {BackupFilePath} restored successfully");
109+
Log.Info(ClassName, $"Failed to load settings.json, {BackupFilePath} restored successfully");
108110

109111
if (File.Exists(FilePath))
110112
File.Replace(BackupFilePath, FilePath, null);

Flow.Launcher.Infrastructure/Storage/PluginBinaryStorage.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.IO;
22
using System.Threading.Tasks;
3-
using CommunityToolkit.Mvvm.DependencyInjection;
4-
using Flow.Launcher.Plugin;
3+
using Flow.Launcher.Infrastructure.Logger;
54
using Flow.Launcher.Plugin.SharedCommands;
65

76
namespace Flow.Launcher.Infrastructure.Storage
@@ -10,10 +9,6 @@ namespace Flow.Launcher.Infrastructure.Storage
109
{
1110
private static readonly string ClassName = "PluginBinaryStorage";
1211

13-
// We should not initialize API in static constructor because it will create another API instance
14-
private static IPublicAPI api = null;
15-
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
16-
1712
public PluginBinaryStorage(string cacheName, string cacheDirectory)
1813
{
1914
DirectoryPath = cacheDirectory;
@@ -30,7 +25,7 @@ public PluginBinaryStorage(string cacheName, string cacheDirectory)
3025
}
3126
catch (System.Exception e)
3227
{
33-
API.LogException(ClassName, $"Failed to save plugin caches to path: {FilePath}", e);
28+
Log.Exception(ClassName, $"Failed to save plugin caches to path: {FilePath}", e);
3429
}
3530
}
3631

@@ -42,7 +37,7 @@ public PluginBinaryStorage(string cacheName, string cacheDirectory)
4237
}
4338
catch (System.Exception e)
4439
{
45-
API.LogException(ClassName, $"Failed to save plugin caches to path: {FilePath}", e);
40+
Log.Exception(ClassName, $"Failed to save plugin caches to path: {FilePath}", e);
4641
}
4742
}
4843
}

0 commit comments

Comments
 (0)