Merge pull request #317 from gnh1201/dev
Some checks are pending
CodeQL / Analyze (javascript) (push) Waiting to run
Deploy Jekyll with GitHub Pages dependencies preinstalled / build (push) Waiting to run
Deploy Jekyll with GitHub Pages dependencies preinstalled / deploy (push) Blocked by required conditions

Add ICompatibleLogger interface and integrate logging
This commit is contained in:
Namhyeon Go 2025-08-17 11:24:05 +09:00 committed by GitHub
commit f413a5c4fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 17 deletions

View File

@ -0,0 +1,17 @@
// ICompatibleLogger.cs (WelsonJS.Launcher)
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2025 Namhyeon Go <gnh1201@catswords.re.kr>, Catswords OSS and WelsonJS Contributors
// https://github.com/gnh1201/welsonjs
//
// We use the ICompatibleLogger interface to maintain a BCL-first style.
// This allows for later replacement with logging libraries such as ILogger or Log4Net.
//
namespace WelsonJS.Launcher
{
public interface ICompatibleLogger
{
void Info(string message);
void Warn(string message);
void Error(string message);
}
}

View File

@ -5,7 +5,6 @@
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
@ -30,27 +29,28 @@ namespace WelsonJS.Launcher
private string _prefix;
private string _resourceName;
private List<IResourceTool> _tools = new List<IResourceTool>();
private BlobConfig _blobConfig;
private readonly ICompatibleLogger _logger;
private static readonly HttpClient _httpClient = new HttpClient();
private static readonly string _defaultMimeType = "application/octet-stream";
private static BlobConfig _blobConfig;
static ResourceServer()
{
// Set timeout
int timeout = int.TryParse(Program.GetAppConfig("HttpClientTimeout"), out timeout) ? timeout : 90;
_httpClient.Timeout = TimeSpan.FromSeconds(timeout);
}
public ResourceServer(string prefix, string resourceName, ICompatibleLogger logger = null)
{
_logger = logger;
_prefix = prefix;
_listener = new HttpListener();
_resourceName = resourceName;
// Fetch a blob config from Internet
FetchBlobConfig();
}
public ResourceServer(string prefix, string resourceName)
{
_prefix = prefix;
_listener = new HttpListener();
_listener.Prefixes.Add(prefix);
_resourceName = resourceName;
// Add resource tools
_tools.Add(new ResourceTools.Completion(this, _httpClient));
@ -60,6 +60,9 @@ namespace WelsonJS.Launcher
_tools.Add(new ResourceTools.CitiQuery(this, _httpClient));
_tools.Add(new ResourceTools.Tfa(this, _httpClient));
_tools.Add(new ResourceTools.Whois(this, _httpClient));
// Register the prefix
_listener.Prefixes.Add(prefix);
}
public string GetPrefix()
@ -110,7 +113,7 @@ namespace WelsonJS.Launcher
catch (Exception ex)
{
if (token.IsCancellationRequested || !_isRunning) break;
MessageBox.Show($"Error: {ex.Message}");
_logger?.Error($"Error: {ex.Message}");
}
}
}
@ -169,7 +172,7 @@ namespace WelsonJS.Launcher
if (!response.IsSuccessStatusCode)
{
Trace.TraceError($"Failed to serve blob. URL: {url}, Status: {response.StatusCode}");
_logger?.Error($"Failed to serve blob. URL: {url}, Status: {response.StatusCode}");
return false;
}
@ -184,7 +187,7 @@ namespace WelsonJS.Launcher
}
catch (Exception ex)
{
Trace.TraceError($"Failed to serve blob. URL: {url}, Exception: {ex.Message}");
_logger?.Error($"Failed to serve blob. URL: {url}, Exception: {ex.Message}");
return false;
}
}
@ -284,7 +287,7 @@ namespace WelsonJS.Launcher
}
catch (Exception ex)
{
Trace.TraceError($"Cache Read Error: {ex.Message}");
_logger?.Error($"Cache Read Error: {ex.Message}");
}
data = null;
@ -329,7 +332,7 @@ namespace WelsonJS.Launcher
}
catch (Exception ex)
{
Trace.TraceError($"Error: {ex.Message}");
_logger?.Error($"Error: {ex.Message}");
return false;
}
}
@ -427,7 +430,7 @@ namespace WelsonJS.Launcher
}
}
private static async void FetchBlobConfig()
private async void FetchBlobConfig()
{
try
{
@ -444,7 +447,7 @@ namespace WelsonJS.Launcher
}
catch (Exception ex)
{
Trace.TraceError($"Failed to fetch a blob config. Exception: {ex.Message}");
_logger?.Error($"Failed to fetch a blob config. Exception: {ex.Message}");
}
}
}

View File

@ -0,0 +1,19 @@
// TraceLogger.cs (WelsonJS.Launcher)
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2025 Namhyeon Go <gnh1201@catswords.re.kr>, Catswords OSS and WelsonJS Contributors
// https://github.com/gnh1201/welsonjs
//
// We use the ICompatibleLogger interface to maintain a BCL-first style.
// This allows for later replacement with logging libraries such as ILogger or Log4Net.
//
using System.Diagnostics;
namespace WelsonJS.Launcher
{
public class TraceLogger : ICompatibleLogger
{
public void Info(string message) => Trace.TraceInformation(message);
public void Warn(string message) => Trace.TraceWarning(message);
public void Error(string message) => Trace.TraceError(message);
}
}

View File

@ -86,6 +86,7 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="ICompatibleLogger.cs" />
<Compile Include="IResourceTool.cs" />
<Compile Include="ResourceTools\CitiQuery.cs" />
<Compile Include="ResourceTools\Settings.cs" />
@ -121,6 +122,7 @@
<DependentUpon>GlobalSettingsForm.cs</DependentUpon>
</Compile>
<Compile Include="ResourceServer.cs" />
<Compile Include="TraceLogger.cs" />
<Compile Include="WebSocketManager.cs" />
<EmbeddedResource Include="EnvForm.resx">
<DependentUpon>EnvForm.cs</DependentUpon>