Skip to content

Commit de501be

Browse files
committed
Incorporated IJ4JLogger into Binder
1 parent fa42fd3 commit de501be

18 files changed

+195
-134
lines changed

Binder.Tests/MiscTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ public void StringHandling( CommandLineStyle style, string cmdLine, params strin
1515

1616
var option = Options.Bind<MiscTarget, string?>( x => x.AStringValue, "x" );
1717
option.Should().NotBeNull();
18-
Options.Log.HasMessages().Should().BeFalse();
1918

20-
var parser = new Parser(Options, Logger);
19+
var parser = new Parser(Options, LoggerFactory);
2120
parser.Parse( cmdLine ).Should().BeTrue();
2221

2322
Options.UnknownKeys.Should().BeEmpty();

Binder.Tests/ParserTests.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ public class ParserTests : BaseTest
1515
[MemberData(nameof(TestDataSource.GetTokenizerData), MemberType = typeof(TestDataSource))]
1616
public void Tokenizer( TokenizerConfig config )
1717
{
18-
var cmdLogger = new CommandLineLogger();
19-
2018
var tokenizer = new Tokenizer(
21-
AvailableTokens.GetDefault(CommandLineStyle.Windows, cmdLogger),
22-
cmdLogger,
23-
new ConsolidateQuotedText(StringComparison.OrdinalIgnoreCase, cmdLogger),
24-
new MergeSequentialSeparators(cmdLogger) );
19+
AvailableTokens.GetDefault(CommandLineStyle.Windows, LoggerFactory),
20+
LoggerFactory,
21+
new ConsolidateQuotedText(StringComparison.OrdinalIgnoreCase, LoggerFactory()),
22+
new MergeSequentialSeparators(LoggerFactory()) );
2523

2624
var tokens = tokenizer.Tokenize( config.CommandLine );
2725

@@ -43,8 +41,7 @@ public void Parser( TestConfig config )
4341
{
4442
Initialize(config);
4543

46-
var cmdLogger = new CommandLineLogger();
47-
var parser = new Parser( Options, cmdLogger );
44+
var parser = new Parser( Options, LoggerFactory );
4845

4946
parser.Options.CreateOptionsFromContextKeys( config.OptionConfigurations );
5047

Binder.Tests/support/BaseTest.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
using System.Linq.Expressions;
44
using FluentAssertions;
55
using J4JSoftware.Configuration.CommandLine;
6+
using J4JSoftware.Logging;
67
using Microsoft.Extensions.Configuration;
78
using Xunit;
89

910
namespace J4JSoftware.Binder.Tests
1011
{
1112
public class BaseTest
1213
{
14+
protected BaseTest()
15+
{
16+
LoggerFactory = CompositionRoot.Default.LoggerFactory;
17+
Logger = LoggerFactory();
18+
}
19+
20+
protected Func<IJ4JLogger> LoggerFactory { get; }
1321
protected TestConfig? TestConfig { get; private set; }
1422
protected IOptionCollection Options { get; private set; }
15-
protected CommandLineLogger Logger => Options.Log;
23+
protected IJ4JLogger Logger { get; }
1624

17-
protected void Initialize(CommandLineStyle style)
25+
protected void Initialize( CommandLineStyle style )
1826
{
19-
Options = new OptionCollectionNG(style);
20-
Options.Log.HasMessages().Should().BeFalse();
27+
Options = new OptionCollectionNG(style, LoggerFactory);
2128
}
2229

2330
protected void Initialize( TestConfig testConfig )
@@ -49,7 +56,7 @@ protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelect
4956

5057
protected void ValidateTokenizing()
5158
{
52-
var parser = new Parser(Options, Logger);
59+
var parser = new Parser(Options, LoggerFactory);
5360
parser.Parse( TestConfig!.CommandLine ).Should().Be( Options.UnknownKeys.Count == 0 );
5461

5562
Options.UnknownKeys.Count.Should().Be( TestConfig.UnknownKeys );
@@ -68,8 +75,6 @@ protected void ValidateConfiguration<TParsed>()
6875
.AddJ4JCommandLine( TestConfig!.CommandLine, Options )
6976
.Build();
7077

71-
Options.Log.HasMessages().Should().BeFalse();
72-
7378
TParsed? parsed = null;
7479

7580
if( TestConfig!.OptionConfigurations.Any( x => x.ConversionWillFail ) )

Binder.Tests/support/CompositionRoot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ private void ConfigureDependencyInjection( HostBuilderContext context, Container
3939
.As<IJ4JLoggerConfiguration>();
4040
}
4141

42-
public IJ4JLogger Logger => Host!.Services.GetRequiredService<IJ4JLogger>();
42+
public Func<IJ4JLogger> LoggerFactory => () => Host!.Services.GetRequiredService<IJ4JLogger>();
4343
}
4444
}

Binder/Binder.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<Compile Remove="CommandLineLogger.cs" />
12+
</ItemGroup>
13+
1014
<ItemGroup>
1115
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
1216
</ItemGroup>
1317

18+
<ItemGroup>
19+
<ProjectReference Include="..\..\J4JLogging\J4JLogging\J4JLogging.csproj" />
20+
</ItemGroup>
21+
1422
</Project>

Binder/J4JCommandLineSource.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using Microsoft.Extensions.Configuration;
1+
using System;
2+
using J4JSoftware.Logging;
3+
using Microsoft.Extensions.Configuration;
4+
using Serilog.Core;
25

36
namespace J4JSoftware.Configuration.CommandLine
47
{
@@ -9,29 +12,28 @@ public class J4JCommandLineSource : IConfigurationSource
912

1013
public J4JCommandLineSource(
1114
IOptionCollection options,
12-
string cmdLine
15+
string cmdLine,
16+
Func<IJ4JLogger>? loggerFactory = null
1317
)
1418
{
1519
Options = options;
1620
_cmdLine = cmdLine;
17-
Logger = new CommandLineLogger();
1821

19-
var tokenizer = new Tokenizer( Logger, options.CommandLineStyle, options.MasterText.TextComparison );
20-
var parsingTable = new ParsingTable( options, Logger );
22+
var tokenizer = new Tokenizer( options.CommandLineStyle, options.MasterText.TextComparison, loggerFactory );
23+
var parsingTable = new ParsingTable( options, loggerFactory);
2124

22-
_parser = new Parser( options, tokenizer, parsingTable, Logger );
25+
_parser = new Parser( options, tokenizer, parsingTable, loggerFactory?.Invoke() );
2326
}
2427

2528
public J4JCommandLineSource(
2629
IOptionCollection options,
2730
string[] args,
28-
CommandLineLogger logger
31+
Func<IJ4JLogger>? loggerFactory
2932
)
30-
: this( options, string.Join( " ", args ) )
33+
: this( options, string.Join( " ", args ), loggerFactory )
3134
{
3235
}
3336

34-
public CommandLineLogger Logger { get; }
3537
public IOptionCollection Options { get; }
3638

3739
public IConfigurationProvider Build( IConfigurationBuilder builder )

Binder/grammar/ConsolidateQuotedText.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using J4JSoftware.Logging;
6+
57
#pragma warning disable 8618
68

79
namespace J4JSoftware.Configuration.CommandLine
810
{
911
public class ConsolidateQuotedText : ICleanupTokens
1012
{
11-
private readonly CommandLineLogger _logger;
12-
1313
private readonly StringComparison _textComparison;
14+
private readonly IJ4JLogger? _logger;
1415

1516
public ConsolidateQuotedText(
1617
StringComparison textComparison,
17-
CommandLineLogger logger )
18+
IJ4JLogger? logger )
1819
{
1920
_textComparison = textComparison;
2021
_logger = logger;
@@ -33,8 +34,8 @@ public void Process( List<Token> tokens )
3334
// "quoted" tokens
3435
if( curPair.End == null )
3536
{
36-
_logger.LogError(
37-
$"Unclosed quoter encountered, command line truncated at token #{curPair.Start.Index}" );
37+
_logger?.Error( "Unclosed quoter encountered, command line truncated at token #{0}",
38+
curPair.Start.Index );
3839

3940
tokens.RemoveFrom( curPair.Start.Index );
4041

Binder/grammar/MergeSequentialSeparators.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Collections.Generic;
2+
using J4JSoftware.Logging;
23

34
namespace J4JSoftware.Configuration.CommandLine
45
{
56
public class MergeSequentialSeparators : ICleanupTokens
67
{
7-
private readonly CommandLineLogger _logger;
8+
private readonly IJ4JLogger? _logger;
89

9-
public MergeSequentialSeparators( CommandLineLogger logger )
10+
public MergeSequentialSeparators( IJ4JLogger? logger )
1011
{
1112
_logger = logger;
1213
}

Binder/grammar/Parser.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
namespace J4JSoftware.Configuration.CommandLine
1+
using System;
2+
using J4JSoftware.Logging;
3+
4+
namespace J4JSoftware.Configuration.CommandLine
25
{
36
public class Parser
47
{
5-
private readonly CommandLineLogger _logger;
8+
private readonly IJ4JLogger? _logger;
69
private readonly ITokenizer _tokenizer;
710

811
public Parser(
9-
CommandLineLogger logger,
10-
CommandLineStyle style = CommandLineStyle.Windows
12+
CommandLineStyle style = CommandLineStyle.Windows,
13+
Func<IJ4JLogger>? loggerFactory = null
1114
)
12-
: this( new OptionCollectionNG( style ), logger )
15+
: this( new OptionCollectionNG( style ), loggerFactory )
1316
{
1417
}
1518

1619
public Parser(
1720
IOptionCollection options,
18-
CommandLineLogger logger
21+
Func<IJ4JLogger>? loggerFactory = null
1922
)
2023
: this(
2124
options,
22-
new Tokenizer( logger, options.CommandLineStyle, options.MasterText.TextComparison ),
23-
new ParsingTable( options, logger ),
24-
logger )
25+
new Tokenizer( options.CommandLineStyle, options.MasterText.TextComparison, loggerFactory ),
26+
new ParsingTable( options, loggerFactory ),
27+
loggerFactory?.Invoke() )
2528
{
2629
}
2730

2831
public Parser(
2932
IOptionCollection options,
3033
ITokenizer tokenizer,
3134
IParsingTable parsingTable,
32-
CommandLineLogger logger
35+
IJ4JLogger? logger = null
3336
)
3437
{
3538
Options = options;
@@ -45,7 +48,7 @@ public bool Parse( string cmdLine )
4548
{
4649
if( !ParsingTable.IsValid )
4750
{
48-
_logger.LogError( "ParsingTable is invalid" );
51+
_logger?.Error( "ParsingTable is invalid" );
4952
return false;
5053
}
5154

Binder/grammar/ParsingTable.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using J4JSoftware.Logging;
45

56
namespace J4JSoftware.Configuration.CommandLine
67
{
@@ -11,9 +12,13 @@ public class ParsingTable : IParsingTable
1112
private readonly Dictionary<TokenType, Dictionary<TokenType, ParsingAction?>> _table =
1213
new();
1314

14-
public ParsingTable( IOptionCollection options, CommandLineLogger logger )
15+
private readonly IJ4JLogger? _logger;
16+
17+
public ParsingTable( IOptionCollection options, Func<IJ4JLogger>? loggerFactory = null )
1518
{
16-
Entries = new TokenEntry.TokenEntries( options, logger );
19+
_logger = loggerFactory?.Invoke();
20+
21+
Entries = new TokenEntry.TokenEntries( options, loggerFactory?.Invoke() );
1722

1823
foreach( var row in Enum.GetValues( typeof(TokenType) )
1924
.Cast<TokenType>() )

0 commit comments

Comments
 (0)