Skip to content

Commit 9a83b26

Browse files
committed
Add logging to FastDataGenerator
1 parent b207d4f commit 9a83b26

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Genbox.FastData.Enums;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace Genbox.FastData;
5+
6+
public partial class FastDataGenerator
7+
{
8+
[LoggerMessage(LogLevel.Information, "There are {Count} unique items")]
9+
internal static partial void LogUniqueItems(ILogger logger, int count);
10+
11+
[LoggerMessage(LogLevel.Information, "Data consists of {DataType}")]
12+
internal static partial void LogDataType(ILogger logger, DataType dataType);
13+
14+
[LoggerMessage(LogLevel.Information, "Min value: {MinValue}, Max value: {MaxValue}")]
15+
internal static partial void LogMinMaxValues(ILogger logger, object minValue, object maxValue);
16+
17+
[LoggerMessage(LogLevel.Information, "Min length: {MinLength}, Max length: {MaxLength}")]
18+
internal static partial void LogMinMaxLength(ILogger logger, uint minLength, uint maxLength);
19+
20+
[LoggerMessage(LogLevel.Information, "User selected structure type {Type}")]
21+
internal static partial void LogUserStructureType(ILogger logger, StructureType type);
22+
23+
[LoggerMessage(LogLevel.Information, "Trying data structure {Name}")]
24+
internal static partial void LogCandidateAttempt(ILogger logger, string name);
25+
26+
[LoggerMessage(LogLevel.Information, "Data structure {Name} succeeded")]
27+
internal static partial void LogCandidateSuccess(ILogger logger, string name);
28+
29+
[LoggerMessage(LogLevel.Information, "Data structure {Name} failed")]
30+
internal static partial void LogCandidateFailed(ILogger logger, string name);
31+
}

Src/FastData/FastDataGenerator.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
using Genbox.FastData.Misc;
1212
using Genbox.FastData.StringHash;
1313
using Microsoft.Extensions.Logging;
14+
using Microsoft.Extensions.Logging.Abstractions;
1415

1516
namespace Genbox.FastData;
1617

17-
public static class FastDataGenerator
18+
public static partial class FastDataGenerator
1819
{
1920
internal const StringComparison DefaultStringComparison = StringComparison.Ordinal;
2021

@@ -38,7 +39,7 @@ public static class FastDataGenerator
3839

3940
public static bool TryGenerate<T>(T[] data, FastDataConfig fdCfg, ICodeGenerator generator, out string? source, ILoggerFactory? factory = null)
4041
{
41-
// factory ??= NullLoggerFactory.Instance;
42+
factory ??= NullLoggerFactory.Instance;
4243

4344
//Validate that we only have unique data
4445
HashSet<T> uniq = new HashSet<T>();
@@ -49,8 +50,20 @@ public static bool TryGenerate<T>(T[] data, FastDataConfig fdCfg, ICodeGenerator
4950
throw new InvalidOperationException($"Duplicate data found: {val}");
5051
}
5152

53+
ILogger logger = factory.CreateLogger(typeof(FastDataGenerator));
54+
LogUniqueItems(logger, uniq.Count);
55+
5256
DataProperties<T> props = DataProperties<T>.Create(data);
5357

58+
LogDataType(logger, props.DataType);
59+
60+
if (props.FloatProps != null)
61+
LogMinMaxValues(logger, props.FloatProps.MinValue, props.FloatProps.MaxValue);
62+
else if (props.IntProps != null)
63+
LogMinMaxValues(logger, props.IntProps.MinValue, props.IntProps.MaxValue);
64+
else if (props.StringProps != null)
65+
LogMinMaxLength(logger, props.StringProps.LengthData.Min, props.StringProps.LengthData.Max);
66+
5467
IStringHash? stringHash = null;
5568
if (data is string[])
5669
stringHash = /*analysisEnabled ? GetBestHash(stringArr, props.StringProps!, fdCfg.SimulatorConfig, factory) :*/ new DefaultStringHash();
@@ -65,16 +78,31 @@ public static bool TryGenerate<T>(T[] data, FastDataConfig fdCfg, ICodeGenerator
6578

6679
IContext? context = null;
6780

81+
LogUserStructureType(logger, fdCfg.StructureType);
6882
foreach (object candidate in GetDataStructureCandidates(data, fdCfg, props))
6983
{
84+
LogCandidateAttempt(logger, candidate.GetType().Name);
85+
7086
if (candidate is IHashStructure<T> hs)
7187
{
7288
if (hs.TryCreate(data, hashFunc, out context))
89+
{
90+
LogCandidateSuccess(logger, candidate.GetType().Name);
7391
break;
92+
}
93+
94+
LogCandidateFailed(logger, candidate.GetType().Name);
7495
}
7596
else if (candidate is IStructure<T> s)
97+
{
7698
if (s.TryCreate(data, out context))
99+
{
100+
LogCandidateSuccess(logger, candidate.GetType().Name);
77101
break;
102+
}
103+
104+
LogCandidateFailed(logger, candidate.GetType().Name);
105+
}
78106
}
79107

80108
if (context == null)

0 commit comments

Comments
 (0)