Skip to content

Commit

Permalink
Merge pull request #91 from Cysharp/hadashiA/fix-state
Browse files Browse the repository at this point in the history
Fix bugs for LogState
  • Loading branch information
neuecc authored Oct 20, 2023
2 parents 79898b5 + d4229c5 commit 5bd9207
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/ZLogger.MessagePack/MessagePackZLoggerFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Buffers;
using MessagePack;
using Microsoft.Extensions.Logging;
Expand Down
8 changes: 3 additions & 5 deletions src/ZLogger/LogStates/InterpolatedStringLogState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public InterpolatedStringLogState(MessageSequence messageSequence, MagicalBox ma

this.parameters = ArrayPool<InterpolatedStringParameter>.Shared.Rent(parameters.Length);
parameters.CopyTo(this.parameters);
ParameterCount = parameters.Length;

this.messageSequence = messageSequence;
this.magicalBox = new MagicalBox(magicalBoxStorage, magicalBox.Written);
Expand All @@ -34,7 +35,7 @@ public InterpolatedStringLogState(MessageSequence messageSequence, MagicalBox ma
public IZLoggerEntry CreateEntry(LogInfo info)
{
// state needs clone.
var newState = new InterpolatedStringLogState(messageSequence, this.magicalBox, this.parameters);
var newState = new InterpolatedStringLogState(messageSequence, this.magicalBox, this.parameters.AsSpan(0, ParameterCount));

// Create Entry with cloned state(state will dispose when entry was disposed)
return ZLoggerEntry<InterpolatedStringLogState>.Create(info, newState);
Expand Down Expand Up @@ -112,10 +113,7 @@ public string GetParameterKeyAsString(int index)
{
return value;
}
else
{
return (T?)p.BoxedValue;
}
return (T?)p.BoxedValue;
}

public Type GetParameterType(int index)
Expand Down
36 changes: 36 additions & 0 deletions src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public void AppendFormatted<T>(T value, int alignment = 0, string? format = null
{
this.innerHandler.AppendFormatted(value, alignment, format, argumentName);
}

public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? format = null, [CallerArgumentExpression("value")] string? argumentName = null)
where T : struct
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}
}

[InterpolatedStringHandler]
Expand All @@ -42,6 +48,12 @@ public void AppendFormatted<T>(T value, int alignment = 0, string? format = null
{
this.innerHandler.AppendFormatted(value, alignment, format, argumentName);
}

public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? format = null, [CallerArgumentExpression("value")] string? argumentName = null)
where T : struct
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}
}

[InterpolatedStringHandler]
Expand All @@ -62,6 +74,12 @@ public void AppendFormatted<T>(T value, int alignment = 0, string? format = null
{
this.innerHandler.AppendFormatted(value, alignment, format, argumentName);
}

public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? format = null, [CallerArgumentExpression("value")] string? argumentName = null)
where T : struct
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}
}

[InterpolatedStringHandler]
Expand All @@ -82,6 +100,12 @@ public void AppendFormatted<T>(T value, int alignment = 0, string? format = null
{
this.innerHandler.AppendFormatted(value, alignment, format, argumentName);
}

public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? format = null, [CallerArgumentExpression("value")] string? argumentName = null)
where T : struct
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}
}

[InterpolatedStringHandler]
Expand All @@ -102,6 +126,12 @@ public void AppendFormatted<T>(T value, int alignment = 0, string? format = null
{
this.innerHandler.AppendFormatted(value, alignment, format, argumentName);
}

public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? format = null, [CallerArgumentExpression("value")] string? argumentName = null)
where T : struct
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}
}

[InterpolatedStringHandler]
Expand All @@ -122,5 +152,11 @@ public void AppendFormatted<T>(T value, int alignment = 0, string? format = null
{
this.innerHandler.AppendFormatted(value, alignment, format, argumentName);
}

public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? format = null, [CallerArgumentExpression("value")] string? argumentName = null)
where T : struct
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}
}

6 changes: 6 additions & 0 deletions src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public ref struct ZLogger<#= logLevel #>InterpolatedStringHandler
{
this.innerHandler.AppendFormatted(value, alignment, format, argumentName);
}

public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? format = null, [CallerArgumentExpression("value")] string? argumentName = null)
where T : struct
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}
}

<# } #>

0 comments on commit 5bd9207

Please sign in to comment.