Skip to content

Commit

Permalink
Hide TryFormat extensions under preprocessor directives for target fr…
Browse files Browse the repository at this point in the history
…ameworks greater than netstandard2.0
  • Loading branch information
aannenko committed Nov 12, 2024
1 parent b50ce1f commit 3816bc2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
28 changes: 18 additions & 10 deletions src/NReco.Logging.File/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@

namespace NReco.Logging.File.Extensions {
public static class DateTimeExtensions {
public static bool TryFormatO(this DateTime dateTime, Span<char> destination, out int charsWritten) {
public static int GetFormattedLength(this DateTime dateTime) {
const int BaseCharCountInFormatO = 27;

int charsRequired = BaseCharCountInFormatO;
var kind = dateTime.Kind;
var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime);
if (kind == DateTimeKind.Local) {
charsRequired += 6;
}
else if (kind == DateTimeKind.Utc) {
charsRequired++;
}
return BaseCharCountInFormatO + dateTime.Kind switch {
DateTimeKind.Local => 6,
DateTimeKind.Utc => 1,
_ => 0
};
}

#if NETSTANDARD2_0
public static bool TryFormat(this DateTime date, Span<char> destination, out int charsWritten) {
return TryFormatO(date, destination, out charsWritten);
}

public static bool TryFormatO(this DateTime dateTime, Span<char> destination, out int charsWritten) {
var charsRequired = dateTime.GetFormattedLength();

if (destination.Length < charsRequired) {
charsWritten = 0;
Expand Down Expand Up @@ -46,7 +51,9 @@ public static bool TryFormatO(this DateTime dateTime, Span<char> destination, ou
destination[19] = '.';
tick.WriteDigits(destination.Slice(20), 7);

var kind = dateTime.Kind;
if (kind == DateTimeKind.Local) {
var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime);
var offsetTotalMinutes = (int)(offset.Ticks / TimeSpan.TicksPerMinute);

var sign = '+';
Expand All @@ -68,5 +75,6 @@ public static bool TryFormatO(this DateTime dateTime, Span<char> destination, ou

return true;
}
#endif
}
}
2 changes: 2 additions & 0 deletions src/NReco.Logging.File/Extensions/IntExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static int GetFormattedLength(this int value) {
return value == 0 ? 1 : (int)Math.Floor(Math.Log10(Math.Abs((double)value))) + (value > 0 ? 1 : 2);
}

#if NETSTANDARD2_0
public static bool TryFormat(this int value, Span<char> destination, out int charsWritten) {
charsWritten = value.GetFormattedLength();
if (destination.Length < charsWritten) {
Expand Down Expand Up @@ -35,5 +36,6 @@ internal static void WriteDigits(this uint value, Span<char> destination, int co

destination[0] = (char)('0' + value);
}
#endif
}
}
16 changes: 8 additions & 8 deletions src/NReco.Logging.File/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
}
else {
const int MaxStackAllocatedBufferLength = 256;
var logMessageLength = CalculateLogMessageLength(eventId, message);
DateTime timeStamp = LoggerPrv.UseUtcTimestamp ? DateTime.UtcNow : DateTime.Now;
var logMessageLength = CalculateLogMessageLength(timeStamp, eventId, message);
char[] charBuffer = null;
try {
Span<char> buffer = logMessageLength <= MaxStackAllocatedBufferLength
? stackalloc char[MaxStackAllocatedBufferLength]
: (charBuffer = ArrayPool<char>.Shared.Rent(logMessageLength));

FormatLogEntryDefault(buffer, message, logLevel, eventId, exception);
FormatLogEntryDefault(buffer, timeStamp, message, logLevel, eventId, exception);
}
finally {
if (charBuffer is not null) {
Expand All @@ -92,13 +93,12 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
}
}

private void FormatLogEntryDefault(Span<char> buffer, string message, LogLevel logLevel,
EventId eventId, Exception exception) {
private void FormatLogEntryDefault(Span<char> buffer, DateTime timeStamp, string message,
LogLevel logLevel, EventId eventId, Exception exception) {
// default formatting logic
using var logBuilder = new ValueStringBuilder(buffer);
if (!string.IsNullOrEmpty(message)) {
DateTime timeStamp = LoggerPrv.UseUtcTimestamp ? DateTime.UtcNow : DateTime.Now;
timeStamp.TryFormatO(logBuilder.RemainingRawChars, out var charsWritten);
timeStamp.TryFormat(logBuilder.RemainingRawChars, out var charsWritten);
logBuilder.AppendSpan(charsWritten);
logBuilder.Append('\t');
logBuilder.Append(GetShortLogLevel(logLevel));
Expand All @@ -124,8 +124,8 @@ private void FormatLogEntryDefault(Span<char> buffer, string message, LogLevel l
LoggerPrv.WriteEntry(logBuilder.ToString());
}

private int CalculateLogMessageLength(EventId eventId, string message) {
return 33 /* timeStamp.TryFormatO */
private int CalculateLogMessageLength(DateTime timeStamp, EventId eventId, string message) {
return timeStamp.GetFormattedLength()
+ 1 /* '\t' */
+ 4 /* GetShortLogLevel */
+ 2 /* "\t[" */
Expand Down

0 comments on commit 3816bc2

Please sign in to comment.