Compare commits
No commits in common. "ee20c9c5ecc15a7b133e28fe3f4e38e661744733" and "448161239e4fbfd09b4bf40e77066128058fc2ce" have entirely different histories.
ee20c9c5ec
...
448161239e
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib.Web</PackageId>
|
<PackageId>DTLib.Web</PackageId>
|
||||||
<Version>1.3.0</Version>
|
<Version>1.2.2</Version>
|
||||||
<Authors>Timerix</Authors>
|
<Authors>Timerix</Authors>
|
||||||
<Description>HTTP Server with simple routing</Description>
|
<Description>HTTP Server with simple routing</Description>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
@ -25,6 +25,6 @@
|
|||||||
<ProjectReference Include="..\DTLib\DTLib.csproj" />
|
<ProjectReference Include="..\DTLib\DTLib.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
||||||
<PackageReference Include="DTLib" Version="1.7.1" />
|
<PackageReference Include="DTLib" Version="1.6.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
namespace DTLib.Web.Routes;
|
namespace DTLib.Web.Routes;
|
||||||
|
|
||||||
public class DelegateRouteHandler(
|
public class DelegateRouteHandler(Func<HttpListenerContext, Task<HttpStatusCode>> routeHandler) : RouteHandler
|
||||||
Func<HttpListenerContext, ContextLogger, Task<HttpStatusCode>> routeHandler
|
|
||||||
) : IRouteHandler
|
|
||||||
{
|
{
|
||||||
public Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx, ContextLogger requestLogger)
|
public override Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx) => routeHandler(ctx);
|
||||||
=> routeHandler(ctx, requestLogger);
|
|
||||||
}
|
}
|
||||||
@ -1,6 +0,0 @@
|
|||||||
namespace DTLib.Web.Routes;
|
|
||||||
|
|
||||||
public interface IRouteHandler
|
|
||||||
{
|
|
||||||
Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx, ContextLogger requestLogger);
|
|
||||||
}
|
|
||||||
@ -2,5 +2,5 @@ namespace DTLib.Web.Routes;
|
|||||||
|
|
||||||
public interface IRouter
|
public interface IRouter
|
||||||
{
|
{
|
||||||
Task<HttpStatusCode> Resolve(HttpListenerContext ctx, ContextLogger requestLogger);
|
Task<HttpStatusCode> Resolve(HttpListenerContext ctx);
|
||||||
}
|
}
|
||||||
6
DTLib.Web/Routes/RouteHandler.cs
Normal file
6
DTLib.Web/Routes/RouteHandler.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace DTLib.Web.Routes;
|
||||||
|
|
||||||
|
public abstract class RouteHandler
|
||||||
|
{
|
||||||
|
public abstract Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx);
|
||||||
|
}
|
||||||
@ -1,8 +1,8 @@
|
|||||||
namespace DTLib.Web.Routes;
|
namespace DTLib.Web.Routes;
|
||||||
|
|
||||||
public class ServeFilesRouteHandler(IOPath _publicDir, string _homePageUrl = "index.html") : IRouteHandler
|
public class ServeFilesRouteHandler(IOPath _publicDir, string _homePageUrl = "index.html") : RouteHandler
|
||||||
{
|
{
|
||||||
public async Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx, ContextLogger requestLogger)
|
public override async Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx)
|
||||||
{
|
{
|
||||||
if (ctx.Request.HttpMethod != "GET")
|
if (ctx.Request.HttpMethod != "GET")
|
||||||
return HttpStatusCode.BadRequest;
|
return HttpStatusCode.BadRequest;
|
||||||
|
|||||||
@ -3,23 +3,22 @@ namespace DTLib.Web.Routes;
|
|||||||
public class SimpleRouter : IRouter
|
public class SimpleRouter : IRouter
|
||||||
{
|
{
|
||||||
/// route for any url that doesn't have its own handler
|
/// route for any url that doesn't have its own handler
|
||||||
public IRouteHandler? DefaultRoute { get; set; }
|
public RouteHandler? DefaultRoute { get; set; }
|
||||||
|
|
||||||
private readonly Dictionary<string, IRouteHandler> _routes = new();
|
private readonly Dictionary<string, RouteHandler> _routes = new();
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
public SimpleRouter(ILogger logger)
|
public SimpleRouter(ILogger logger)
|
||||||
{
|
{
|
||||||
_logger = new ContextLogger(nameof(SimpleRouter), logger);
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MapRoute(string url, HttpMethod method, IRouteHandler route) => _routes.Add($"{url}:{method}", route);
|
public void MapRoute(string url, HttpMethod method, RouteHandler route) => _routes.Add($"{url}:{method}", route);
|
||||||
|
|
||||||
public void MapRoute(string url, HttpMethod method,
|
public void MapRoute(string url, HttpMethod method, Func<HttpListenerContext, Task<HttpStatusCode>> route)
|
||||||
Func<HttpListenerContext, ContextLogger, Task<HttpStatusCode>> route)
|
|
||||||
=> MapRoute(url, method, new DelegateRouteHandler(route));
|
=> MapRoute(url, method, new DelegateRouteHandler(route));
|
||||||
|
|
||||||
public async Task<HttpStatusCode> Resolve(HttpListenerContext ctx, ContextLogger requestLogger)
|
public async Task<HttpStatusCode> Resolve(HttpListenerContext ctx)
|
||||||
{
|
{
|
||||||
|
|
||||||
string? requestPath = ctx.Request.Url?.AbsolutePath;
|
string? requestPath = ctx.Request.Url?.AbsolutePath;
|
||||||
@ -34,7 +33,7 @@ public class SimpleRouter : IRouter
|
|||||||
_logger.LogWarn(nameof(SimpleRouter), $"couldn't resolve request path {requestPath}");
|
_logger.LogWarn(nameof(SimpleRouter), $"couldn't resolve request path {requestPath}");
|
||||||
status = HttpStatusCode.NotFound;
|
status = HttpStatusCode.NotFound;
|
||||||
}
|
}
|
||||||
else status = await route.HandleRequest(ctx, requestLogger);
|
else status = await route.HandleRequest(ctx);
|
||||||
|
|
||||||
ctx.Response.StatusCode = (int)status;
|
ctx.Response.StatusCode = (int)status;
|
||||||
await ctx.Response.OutputStream.FlushAsync();
|
await ctx.Response.OutputStream.FlushAsync();
|
||||||
|
|||||||
@ -41,7 +41,7 @@ public class WebApp
|
|||||||
while (!_stopToken.IsCancellationRequested)
|
while (!_stopToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
var ctx = await server.GetContextAsync().AsCancellable(_stopToken);
|
var ctx = await server.GetContextAsync().AsCancellable(_stopToken);
|
||||||
HandleRequestAsync(ctx, new ContextLogger($"Request-{requestId++}", _logger));
|
HandleRequestAsync(ctx, requestId++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
@ -51,20 +51,22 @@ public class WebApp
|
|||||||
_logger.LogInfo("server stopped");
|
_logger.LogInfo("server stopped");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void HandleRequestAsync(HttpListenerContext ctx, ContextLogger requestLogger)
|
// ReSharper disable once AsyncVoidMethod
|
||||||
|
private async void HandleRequestAsync(HttpListenerContext ctx, long requestId)
|
||||||
{
|
{
|
||||||
|
string logContext = $"Request-{requestId}";
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
requestLogger.LogInfo($"{ctx.Request.HttpMethod} {ctx.Request.RawUrl} from {ctx.Request.RemoteEndPoint}...");
|
_logger.LogInfo(logContext, $"[{ctx.Request.HttpMethod}] {ctx.Request.RawUrl} from {ctx.Request.RemoteEndPoint}...");
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
var status = await _router.Resolve(ctx, requestLogger);
|
var status = await _router.Resolve(ctx);
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
requestLogger.LogInfo($"responded {(int)status} ({status}) in {stopwatch.ElapsedMilliseconds}ms");
|
_logger.LogInfo(logContext, $"responded {(int)status} ({status}) in {stopwatch.ElapsedMilliseconds}ms");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
requestLogger.LogWarn(ex);
|
_logger.LogWarn(logContext, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,9 +14,6 @@ public static class ColoredConsole
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void ResetColor() => System.Console.ResetColor();
|
public static void ResetColor() => System.Console.ResetColor();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Clears console buffer and resets color
|
|
||||||
/// </summary>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void Clear()
|
public static void Clear()
|
||||||
{
|
{
|
||||||
@ -24,15 +21,26 @@ public static class ColoredConsole
|
|||||||
System.Console.Clear();
|
System.Console.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void Write(char c) => System.Console.Write(c);
|
||||||
|
|
||||||
public static void Write(string msg, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
public static void Write(string msg, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
{
|
{
|
||||||
if(fg != null) Fg(fg.Value);
|
if(fg != null) Fg(fg.Value);
|
||||||
if(bg != null) Bg(bg.Value);
|
if(bg != null) Bg(bg.Value);
|
||||||
System.Console.Write(msg);
|
#if NETSTANDARD2_0
|
||||||
|
var chars = msg.ToCharArray();
|
||||||
|
#else
|
||||||
|
var chars = msg.AsSpan();
|
||||||
|
#endif
|
||||||
|
for (int i = 0; i < chars.Length; ++i)
|
||||||
|
{
|
||||||
|
Write(chars[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void WriteLine() => System.Console.Write('\n');
|
public static void WriteLine() => Write('\n');
|
||||||
|
|
||||||
public static void WriteLine(string msg, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
public static void WriteLine(string msg, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
{
|
{
|
||||||
@ -40,15 +48,18 @@ public static class ColoredConsole
|
|||||||
WriteLine();
|
WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ReadLine(ConsoleColor? fg = null, ConsoleColor? bg = null)
|
public static string? ReadLine(string query, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
{
|
{
|
||||||
if(fg != null) Fg(fg.Value);
|
Write(query, fg, bg);
|
||||||
if(bg != null) Bg(bg.Value);
|
Write(':');
|
||||||
return System.Console.ReadLine() ?? string.Empty;
|
Write(' ');
|
||||||
|
return System.Console.ReadLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteHLine(char c, ConsoleColor? fg = null, ConsoleColor? bg = null) =>
|
public static void WriteHLine(char c, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
|
{
|
||||||
WriteLine(c.Multiply(Width - 1), fg, bg);
|
WriteLine(c.Multiply(Width - 1), fg, bg);
|
||||||
|
}
|
||||||
|
|
||||||
public static void WriteTitle(string title, char spacing = '-',
|
public static void WriteTitle(string title, char spacing = '-',
|
||||||
string left_framing = "[", string right_framing = "]",
|
string left_framing = "[", string right_framing = "]",
|
||||||
@ -72,5 +83,7 @@ public static class ColoredConsole
|
|||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void WriteCentred(string title, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
public static void WriteCentred(string title, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
=> WriteTitle(title, ' ', "", "", fg, bg);
|
{
|
||||||
|
WriteTitle(title, ' ', "", "", fg, bg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,96 +1,64 @@
|
|||||||
namespace DTLib.Console;
|
namespace DTLib.Console;
|
||||||
|
|
||||||
public record LaunchArgument
|
#nullable enable
|
||||||
|
public class LaunchArgument
|
||||||
{
|
{
|
||||||
public struct Param
|
public string[] Aliases;
|
||||||
{
|
public string Description;
|
||||||
public readonly string Name;
|
protected string? ParamName1;
|
||||||
public string? Value { get; internal set; } = null;
|
protected string? ParamName2;
|
||||||
|
public Action? Handler;
|
||||||
|
public Action<string>? HandlerWithArg1;
|
||||||
|
public Action<string, string>? HandlerWithArg2;
|
||||||
|
public int RequiredArgsCount;
|
||||||
|
public int Priority;
|
||||||
|
|
||||||
public Param(string name)
|
private LaunchArgument(string[] aliases, string description, int priority)
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly string[] Aliases;
|
|
||||||
public readonly string Description;
|
|
||||||
public readonly int Priority;
|
|
||||||
public readonly Param[] Params;
|
|
||||||
|
|
||||||
private readonly Action? Handler;
|
|
||||||
private readonly Action<string>? Handler1Param;
|
|
||||||
private readonly Action<string, string>? Handler2Params;
|
|
||||||
|
|
||||||
public LaunchArgument(string[] aliases, string description,
|
|
||||||
Action handler, int priority = 0)
|
|
||||||
{
|
{
|
||||||
Aliases = aliases;
|
Aliases = aliases;
|
||||||
Description = description;
|
Description = description;
|
||||||
Priority = priority;
|
Priority = priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LaunchArgument(string[] aliases, string description,
|
||||||
|
Action handler, int priority = 0)
|
||||||
|
: this(aliases, description, priority)
|
||||||
|
{
|
||||||
Handler = handler;
|
Handler = handler;
|
||||||
Params = [];
|
RequiredArgsCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LaunchArgument(string[] aliases, string description,
|
public LaunchArgument(string[] aliases, string description,
|
||||||
Action<string> handler, string paramName1, int priority=0)
|
Action<string> handler, string paramName1, int priority=0)
|
||||||
|
: this(aliases, description, priority)
|
||||||
{
|
{
|
||||||
Aliases = aliases;
|
HandlerWithArg1 = handler;
|
||||||
Description = description;
|
ParamName1 = paramName1;
|
||||||
Priority = priority;
|
RequiredArgsCount = 1;
|
||||||
|
|
||||||
Handler1Param = handler;
|
|
||||||
Params = [
|
|
||||||
new Param(paramName1)
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
public LaunchArgument(string[] aliases, string description,
|
public LaunchArgument(string[] aliases, string description,
|
||||||
Action<string, string> handler, string paramName1, string paramName2, int priority=0)
|
Action<string, string> handler, string paramName1, string paramName2, int priority=0)
|
||||||
|
: this(aliases, description, priority)
|
||||||
{
|
{
|
||||||
Aliases = aliases;
|
HandlerWithArg2 = handler;
|
||||||
Description = description;
|
ParamName1 = paramName1;
|
||||||
Priority = priority;
|
ParamName2 = paramName2;
|
||||||
|
RequiredArgsCount = 2;
|
||||||
Handler2Params = handler;
|
|
||||||
Params = [
|
|
||||||
new Param(paramName1),
|
|
||||||
new Param(paramName2),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal StringBuilder AppendHelpInfo(StringBuilder b)
|
public StringBuilder AppendHelpInfo(StringBuilder b)
|
||||||
{
|
{
|
||||||
b.Append(Aliases[0]);
|
b.Append(Aliases[0]);
|
||||||
for (int i = 1; i < Aliases.Length; i++)
|
for (int i = 1; i < Aliases.Length; i++)
|
||||||
b.Append(", ").Append(Aliases[i]);
|
b.Append(", ").Append(Aliases[i]);
|
||||||
foreach (var param in Params)
|
if (!string.IsNullOrEmpty(ParamName1))
|
||||||
b.Append(" [").Append(param.Name).Append("]");
|
b.Append(" [").Append(ParamName1).Append("] ");
|
||||||
b.Append(" - ").Append(Description);
|
if (!string.IsNullOrEmpty(ParamName2))
|
||||||
|
b.Append(" [").Append(ParamName2).Append("] ");
|
||||||
|
b.Append("- ").Append(Description);
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Handle()
|
public override string ToString() =>
|
||||||
{
|
$"{{{{{Aliases.MergeToString(", ")}}}, Handler: {Handler is null}, HandlerWithArg: {HandlerWithArg1 is null}}}";
|
||||||
switch (Params.Length)
|
|
||||||
{
|
|
||||||
default:
|
|
||||||
throw new ArgumentOutOfRangeException(Params.Length.ToString());
|
|
||||||
case 0:
|
|
||||||
Handler!.Invoke();
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
if (Params[0].Value is null)
|
|
||||||
throw new NullReferenceException($"Argument '{Aliases[0]}' hasnt got Param[0] value");
|
|
||||||
Handler1Param!.Invoke(Params[0].Value!);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
if (Params[0].Value is null)
|
|
||||||
throw new NullReferenceException($"Argument '{Aliases[0]}' hasnt got Param[0] value");
|
|
||||||
if (Params[1].Value is null)
|
|
||||||
throw new NullReferenceException($"Argument '{Aliases[0]}' hasnt got Param[1] value");
|
|
||||||
Handler2Params!.Invoke(Params[0].Value!, Params[1].Value!);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -2,10 +2,9 @@ namespace DTLib.Console;
|
|||||||
|
|
||||||
public class LaunchArgumentParser
|
public class LaunchArgumentParser
|
||||||
{
|
{
|
||||||
public bool IsAllowedNoArguments;
|
private Dictionary<string, LaunchArgument> argDict = new();
|
||||||
|
private List<LaunchArgument> argList = new();
|
||||||
private readonly Dictionary<string, LaunchArgument> argDict = new();
|
public bool ExitIfNoArgs = true;
|
||||||
private readonly List<LaunchArgument> argList = new();
|
|
||||||
|
|
||||||
public class ExitAfterHelpException : Exception
|
public class ExitAfterHelpException : Exception
|
||||||
{
|
{
|
||||||
@ -52,9 +51,9 @@ public class LaunchArgumentParser
|
|||||||
Add(helpArg);
|
Add(helpArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LaunchArgumentParser AllowNoArguments()
|
public LaunchArgumentParser WithNoExit()
|
||||||
{
|
{
|
||||||
IsAllowedNoArguments = true;
|
ExitIfNoArgs = false;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,15 +64,15 @@ public class LaunchArgumentParser
|
|||||||
}
|
}
|
||||||
public LaunchArgumentParser(params LaunchArgument[] arguments) : this()
|
public LaunchArgumentParser(params LaunchArgument[] arguments) : this()
|
||||||
{
|
{
|
||||||
foreach (var arg in arguments)
|
for (var i = 0; i < arguments.Length; i++)
|
||||||
Add(arg);
|
Add(arguments[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(LaunchArgument arg)
|
public void Add(LaunchArgument arg)
|
||||||
{
|
{
|
||||||
argList.Add(arg);
|
argList.Add(arg);
|
||||||
foreach (string alias in arg.Aliases)
|
for(int a=0; a<arg.Aliases.Length; a++)
|
||||||
argDict.Add(alias, arg);
|
argDict.Add(arg.Aliases[a], arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LaunchArgument Parse(string argAlias)
|
public LaunchArgument Parse(string argAlias)
|
||||||
@ -94,8 +93,8 @@ public class LaunchArgumentParser
|
|||||||
/// <exception cref="ExitAfterHelpException">happens after help message is displayed</exception>
|
/// <exception cref="ExitAfterHelpException">happens after help message is displayed</exception>
|
||||||
public void ParseAndHandle(string[] args)
|
public void ParseAndHandle(string[] args)
|
||||||
{
|
{
|
||||||
// show help message and throw ExitAfterHelpException
|
// show help and throw
|
||||||
if (args.Length == 0 && !IsAllowedNoArguments)
|
if (args.Length == 0 && ExitIfNoArgs)
|
||||||
HelpHandler();
|
HelpHandler();
|
||||||
|
|
||||||
List<LaunchArgument> execQueue = new();
|
List<LaunchArgument> execQueue = new();
|
||||||
@ -103,13 +102,34 @@ public class LaunchArgumentParser
|
|||||||
for (int i = 0; i < args.Length; i++)
|
for (int i = 0; i < args.Length; i++)
|
||||||
{
|
{
|
||||||
LaunchArgument arg = Parse(args[i]);
|
LaunchArgument arg = Parse(args[i]);
|
||||||
for (int j = 0; j < arg.Params.Length; j++)
|
|
||||||
{
|
|
||||||
if (++i >= args.Length)
|
|
||||||
throw new Exception($"argument '{arg.Aliases[0]}' should have parameter '{arg.Params[j]}' after it");
|
|
||||||
arg.Params[j].Value = args[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
switch (arg.RequiredArgsCount)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
if (arg.Handler is null)
|
||||||
|
throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
if (arg.HandlerWithArg1 is null)
|
||||||
|
throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers");
|
||||||
|
if (i + 1 >= args.Length)
|
||||||
|
throw new Exception($"argument <{args[i]}> should have a parameter after it");
|
||||||
|
string arg1 = args[++i];
|
||||||
|
arg.Handler = () => arg.HandlerWithArg1(arg1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
{
|
||||||
|
if (arg.HandlerWithArg2 is null)
|
||||||
|
throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers");
|
||||||
|
if (i + 2 >= args.Length)
|
||||||
|
throw new Exception($"argument <{args[i]}> should have two params after it");
|
||||||
|
string arg1 = args[++i], arg2 = args[++i];
|
||||||
|
arg.Handler = () => arg.HandlerWithArg2(arg1, arg2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
execQueue.Add(arg);
|
execQueue.Add(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,6 +137,6 @@ public class LaunchArgumentParser
|
|||||||
execQueue.Sort((a0, a1) => a0.Priority-a1.Priority);
|
execQueue.Sort((a0, a1) => a0.Priority-a1.Priority);
|
||||||
// finally executing handlers
|
// finally executing handlers
|
||||||
foreach (var a in execQueue)
|
foreach (var a in execQueue)
|
||||||
a.Handle();
|
a.Handler!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib</PackageId>
|
<PackageId>DTLib</PackageId>
|
||||||
<Version>1.7.1</Version>
|
<Version>1.6.5</Version>
|
||||||
<Authors>Timerix</Authors>
|
<Authors>Timerix</Authors>
|
||||||
<Description>Library for all my C# projects</Description>
|
<Description>Library for all my C# projects</Description>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
|
|||||||
@ -11,8 +11,8 @@ public class CompositeLogger : ILogger
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
_debugLogEnabled = value;
|
_debugLogEnabled = value;
|
||||||
foreach (var childLogger in ChildLoggers)
|
for (int i = 0; i < _loggers.Length; i++)
|
||||||
childLogger.DebugLogEnabled = value;
|
_loggers[i].DebugLogEnabled = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,8 +22,8 @@ public class CompositeLogger : ILogger
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
_infoLogEnabled = true;
|
_infoLogEnabled = true;
|
||||||
foreach (var childLogger in ChildLoggers)
|
for (int i = 0; i < _loggers.Length; i++)
|
||||||
childLogger.InfoLogEnabled = value;
|
_loggers[i].InfoLogEnabled = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,8 +33,8 @@ public class CompositeLogger : ILogger
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
_warnLogEnabled = value;
|
_warnLogEnabled = value;
|
||||||
foreach (var childLogger in ChildLoggers)
|
for (int i = 0; i < _loggers.Length; i++)
|
||||||
childLogger.WarnLogEnabled = value;
|
_loggers[i].WarnLogEnabled = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,38 +44,46 @@ public class CompositeLogger : ILogger
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
_errorLogenabled = value;
|
_errorLogenabled = value;
|
||||||
foreach (var childLogger in ChildLoggers)
|
for (int i = 0; i < _loggers.Length; i++)
|
||||||
childLogger.ErrorLogEnabled = value;
|
_loggers[i].ErrorLogEnabled = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ILogFormat Format { get; set; }
|
public ILogFormat Format { get; set; }
|
||||||
public readonly List<ILogger> ChildLoggers;
|
|
||||||
|
|
||||||
private bool _debugLogEnabled;
|
protected ILogger[] _loggers;
|
||||||
|
private bool _debugLogEnabled =
|
||||||
|
#if DEBUG
|
||||||
|
true;
|
||||||
|
#else
|
||||||
|
false;
|
||||||
|
#endif
|
||||||
private bool _infoLogEnabled = true;
|
private bool _infoLogEnabled = true;
|
||||||
private bool _warnLogEnabled = true;
|
private bool _warnLogEnabled = true;
|
||||||
private bool _errorLogenabled = true;
|
private bool _errorLogenabled = true;
|
||||||
|
|
||||||
public CompositeLogger(ILogFormat format, params ILogger[] childLoggers)
|
public CompositeLogger(ILogFormat format, params ILogger[] loggers)
|
||||||
{
|
{
|
||||||
Format = format;
|
Format = format;
|
||||||
ChildLoggers = new List<ILogger>(childLoggers);
|
_loggers = loggers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompositeLogger(params ILogger[] childLoggers) : this(new DefaultLogFormat(), childLoggers)
|
public CompositeLogger(params ILogger[] loggers) : this(new DefaultLogFormat(), loggers)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
public void Log(string context, LogSeverity severity, object message, ILogFormat format)
|
public void Log(string context, LogSeverity severity, object message, ILogFormat format)
|
||||||
{
|
{
|
||||||
foreach (var childLogger in ChildLoggers)
|
if(!this.CheckSeverity(severity))
|
||||||
childLogger.Log(context, severity, message, format);
|
return;
|
||||||
|
|
||||||
|
for (int i = 0; i < _loggers.Length; i++)
|
||||||
|
_loggers[i].Log(context, severity, message, format);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
foreach (var childLogger in ChildLoggers)
|
for (int i = 0; i < _loggers.Length; i++)
|
||||||
childLogger.Dispose();
|
_loggers[i].Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user