Skip to content

Commit f9576e7

Browse files
committed
Eliminated IJ4JLogger in favor of a simple message collector.
1 parent ad301c3 commit f9576e7

File tree

9 files changed

+56
-38
lines changed

9 files changed

+56
-38
lines changed

Binder.Tests/support/BaseTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected void ValidateConfiguration<TParsed>()
6363
where TParsed : class, new()
6464
{
6565
var configBuilder = new ConfigurationBuilder();
66-
configBuilder.AddJ4JCommandLine( Options!, TestConfig!.CommandLine, _allocator! );
66+
configBuilder.AddJ4JCommandLine( TestConfig!.CommandLine, out var options, out var logger);
6767
var config = configBuilder.Build();
6868

6969
TParsed? parsed = null;

Binder/Binder.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<ProjectReference Include="..\..\J4JLogging\J4JLogging\J4JLogging.csproj" />
26+
<Folder Include="options\" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
2731
</ItemGroup>
2832

2933
</Project>

Binder/CommandLineLogger.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace J4JSoftware.CommandLine
8+
{
9+
public class CommandLineLogger
10+
{
11+
private readonly List<string> _messages = new List<string>();
12+
13+
public void Log( string mesg ) => _messages.Add( mesg );
14+
}
15+
}

Binder/J4JCommandLineExtensions.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
using Microsoft.Extensions.Configuration;
1+
using System.Runtime.InteropServices.ComTypes;
2+
using Microsoft.Extensions.Configuration;
23

34
namespace J4JSoftware.CommandLine
45
{
56
public static class J4JCommandLineExtensions
67
{
7-
public static IConfigurationBuilder AddJ4JCommandLine(
8-
this IConfigurationBuilder builder,
8+
public static IConfigurationBuilder AddJ4JCommandLine(
9+
this IConfigurationBuilder builder,
10+
string cmdLine,
911
OptionCollection options,
10-
string cmdLine,
11-
IAllocator allocator)
12+
out CommandLineLogger logger,
13+
IAllocator? allocator = null )
1214
{
15+
var masterText = new MasterTextCollection();
16+
logger = new CommandLineLogger();
17+
18+
allocator ??= new Allocator( new ElementTerminator( masterText, logger ),
19+
new KeyPrefixer( masterText, logger ), logger );
20+
1321
builder.Add( new J4JCommandLineSource( options, cmdLine, allocator ) );
1422

1523
return builder;

Binder/J4JCommandLineSource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Threading.Tasks;
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.Primitives;
8-
using Twilio.Rest.Api.V2010.Account.Sip.Domain.AuthTypes.AuthTypeCalls;
98

109
namespace J4JSoftware.CommandLine
1110
{
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Linq.Expressions;
66
using System.Reflection;
77
using System.Text;
8-
using J4JSoftware.Logging;
98

109
namespace J4JSoftware.CommandLine
1110
{
@@ -34,16 +33,15 @@ public int GetHashCode(ITypeBoundOption obj)
3433

3534
public OptionCollection(
3635
MasterTextCollection masterText,
37-
IJ4JLogger logger
36+
CommandLineLogger logger
3837
)
3938
{
4039
MasterText = masterText;
4140

4241
Logger = logger;
43-
Logger.SetLoggedType( GetType() );
4442
}
4543

46-
protected IJ4JLogger Logger { get; }
44+
protected CommandLineLogger Logger { get; }
4745
protected MasterTextCollection MasterText { get; }
4846
protected List<IOption> Options { get; } = new();
4947

@@ -105,7 +103,7 @@ public bool Bind<TTarget, TProp>(
105103

106104
if( !ValidateProperty( propInfo, bindNonPublic, out var curStyle ) )
107105
{
108-
Logger.Error<string>( "Property '{0}' is invalid", propInfo.Name );
106+
Logger.Log( $"Property '{propInfo.Name}' is invalid");
109107
return false;
110108
}
111109

@@ -125,7 +123,7 @@ public bool Bind<TTarget, TProp>(
125123

126124
if (!ValidateProperty(propInfo2, bindNonPublic, out var curStyle2))
127125
{
128-
Logger.Error<string>("Property '{0}' is invalid", propInfo2.Name);
126+
Logger.Log($"Property '{propInfo2.Name}' is invalid");
129127
return false;
130128
}
131129

@@ -168,7 +166,7 @@ public bool UsesContextPath( string contextPath )
168166
{
169167
if( !MasterText.IsValid )
170168
{
171-
Logger.Error( "MasterTextCollection is not initialized" );
169+
Logger.Log( "MasterTextCollection is not initialized" );
172170
return false;
173171
}
174172

@@ -182,7 +180,7 @@ public IOption? this[ string key ]
182180
{
183181
if (!MasterText.IsValid)
184182
{
185-
Logger.Error("MasterTextCollection is not initialized");
183+
Logger.Log("MasterTextCollection is not initialized");
186184
return null;
187185
}
188186

@@ -238,7 +236,7 @@ private bool ValidateGenericType(Type genType, out OptionStyle? style)
238236

239237
if (genType.GenericTypeArguments.Length != 1)
240238
{
241-
Logger.Error("Generic type '{0}' does not have just one generic Type argument", genType);
239+
Logger.Log($"Generic type '{genType.Name}' does not have just one generic Type argument");
242240
return false;
243241
}
244242

@@ -247,7 +245,7 @@ private bool ValidateGenericType(Type genType, out OptionStyle? style)
247245

248246
if (!typeof(List<>).MakeGenericType(genType.GenericTypeArguments[0]).IsAssignableFrom(genType))
249247
{
250-
Logger.Error("Generic type '{0}' is not a List<> type", genType);
248+
Logger.Log($"Generic type '{genType}' is not a List<> type");
251249
return false;
252250
}
253251

@@ -269,7 +267,7 @@ private bool ValidateType(Type toCheck)
269267
|| toCheck.GetConstructors().Any( c => c.GetParameters().Length == 0 ) )
270268
return true;
271269

272-
Logger.Error("Unsupported type '{0}'", toCheck);
270+
Logger.Log($"Unsupported type '{toCheck}'");
273271

274272
return false;
275273
}
@@ -278,19 +276,19 @@ private bool ValidateAccessMethod(MethodInfo? methodInfo, bool bindNonPublic, st
278276
{
279277
if (methodInfo == null)
280278
{
281-
Logger.Error<string>("Property '{0}' does not have a get or set method", propName);
279+
Logger.Log($"Property '{propName}' does not have a get or set method");
282280
return false;
283281
}
284282

285283
if (!methodInfo.IsPublic && !bindNonPublic)
286284
{
287-
Logger.Error<string>("Property '{0}::{1}' is not bindable", propName, methodInfo.Name);
285+
Logger.Log($"Property '{propName}::{methodInfo.Name}' is not bindable");
288286
return false;
289287
}
290288

291289
if (methodInfo.GetParameters().Length > allowedParams)
292290
{
293-
Logger.Error<string>("Property '{0}::{1}' is indexed", propName, methodInfo.Name);
291+
Logger.Log($"Property '{propName}::{methodInfo.Name}' is indexed");
294292
return false;
295293
}
296294

Binder/allocating/Allocator.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Runtime.CompilerServices;
44
using System.Runtime.InteropServices.ComTypes;
55
using System.Text;
6-
using J4JSoftware.Logging;
76
using Microsoft.VisualBasic;
87

98
#pragma warning disable 8618
@@ -14,19 +13,18 @@ public class Allocator : IAllocator
1413
{
1514
private readonly IElementTerminator _terminator;
1615
private readonly IKeyPrefixer _prefixer;
17-
private readonly IJ4JLogger _logger;
16+
private readonly CommandLineLogger _logger;
1817

1918
public Allocator(
2019
IElementTerminator terminator,
2120
IKeyPrefixer prefixer,
22-
IJ4JLogger logger
21+
CommandLineLogger logger
2322
)
2423
{
2524
_terminator = terminator;
2625
_prefixer = prefixer;
2726

2827
_logger = logger;
29-
_logger.SetLoggedType( GetType() );
3028
}
3129

3230
public AllocationResult AllocateCommandLine( string cmdLine, OptionCollection options )
@@ -72,7 +70,7 @@ public AllocationResult AllocateCommandLine( string cmdLine, OptionCollection op
7270
else
7371
{
7472
retVal.UnknownKeys.Add( element );
75-
_logger.Error<string>("Unknown key '{0}'", element);
73+
_logger.Log($"Unknown key '{element}'");
7674
}
7775
}
7876
else

Binder/allocating/ElementTerminator.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Collections.ObjectModel;
44
using System.Linq;
55
using System.Text;
6-
using J4JSoftware.Logging;
76

87
#pragma warning disable 8618
98

@@ -12,24 +11,23 @@ namespace J4JSoftware.CommandLine
1211
public class ElementTerminator : IElementTerminator
1312
{
1413
private readonly MasterTextCollection _masterText;
15-
private readonly IJ4JLogger _logger;
14+
private readonly CommandLineLogger _logger;
1615

1716
public ElementTerminator(
1817
MasterTextCollection masterText,
19-
IJ4JLogger logger
18+
CommandLineLogger logger
2019
)
2120
{
2221
_masterText = masterText;
2322

2423
_logger = logger;
25-
_logger.SetLoggedType( GetType() );
2624
}
2725

2826
public int GetMaxTerminatorLength( string text, bool isKey )
2927
{
3028
if( !_masterText.IsValid )
3129
{
32-
_logger.Error("MasterTextCollection is not initialized" );
30+
_logger.Log("MasterTextCollection is not initialized" );
3331
throw new TypeInitializationException( "MasterTextCollection is not initialized", null );
3432
}
3533

Binder/allocating/KeyPrefixer.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Linq;
3-
using J4JSoftware.Logging;
43

54
#pragma warning disable 8618
65

@@ -9,23 +8,22 @@ namespace J4JSoftware.CommandLine
98
public class KeyPrefixer : IKeyPrefixer
109
{
1110
private readonly MasterTextCollection _masterText;
12-
private readonly IJ4JLogger _logger;
11+
private readonly CommandLineLogger _logger;
1312

1413
public KeyPrefixer(
1514
MasterTextCollection masterText,
16-
IJ4JLogger logger
15+
CommandLineLogger logger
1716
)
1817
{
1918
_masterText = masterText;
2019

2120
_logger = logger;
22-
_logger.SetLoggedType(GetType());
2321

2422
if( _masterText[ TextUsageType.Prefix ].Any() ) return;
2523

2624
var mesg =
2725
$"{nameof(masterText)} ({typeof(MasterTextCollection)}) does not define any {nameof(TextUsageType.Prefix)} entries";
28-
_logger.Fatal( mesg );
26+
_logger.Log( mesg );
2927

3028
throw new ArgumentException( mesg );
3129
}
@@ -34,7 +32,7 @@ public int GetMaxPrefixLength( string text )
3432
{
3533
if (!_masterText.IsValid)
3634
{
37-
_logger.Error("MasterTextCollection is not initialized");
35+
_logger.Log("MasterTextCollection is not initialized");
3836
throw new TypeInitializationException("MasterTextCollection is not initialized", null);
3937
}
4038

0 commit comments

Comments
 (0)