Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisit Razor logging #10641

Merged
merged 16 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.CodeAnalysis.Razor.DocumentMapping;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.CodeAnalysis.Razor.DocumentMapping;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.Razor.Workspaces;

internal static class LinePositionSpanExtensions
{
public static Range ToRange(this LinePositionSpan linePositionSpan)
=> new Range
=> new()
{
Start = linePositionSpan.Start.ToPosition(),
End = linePositionSpan.End.ToPosition()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.CodeAnalysis.Razor.Workspaces;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Microsoft.CodeAnalysis.Razor.Workspaces;

using Microsoft.AspNetCore.Razor.Language.Syntax;
using Range = VisualStudio.LanguageServer.Protocol.Range;

internal static class RazorSyntaxNodeExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.CodeAnalysis.Razor.Workspaces;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Razor;
using static System.StringExtensions;

namespace Microsoft.CodeAnalysis.Razor.Logging;

internal static partial class LogMessageFormatter
{
private readonly struct FormattedMessageState
{
// The leading whitespace matches the time space length "[hh:mm:ss.fffffff] "
private static readonly ReadOnlyMemory<char> s_leadingWhiteSpace = " ".AsMemory();

private readonly ReadOnlyMemory<char> _message;
private readonly ReadOnlyMemory<Range> _messageLineRanges;
private readonly ReadOnlyMemory<char> _exceptionText;
private readonly ReadOnlyMemory<Range> _exceptionLineRanges;
private readonly ReadOnlyMemory<char> _categoryNamePart;
private readonly ReadOnlyMemory<char> _timeStampPart;
private readonly ReadOnlyMemory<char> _newLine;
private readonly ReadOnlyMemory<char> _leadingWhiteSpace;

public ReadOnlySpan<char> MessageText => _message.Span;
public ReadOnlySpan<Range> MessageLineRanges => _messageLineRanges.Span;
public ReadOnlySpan<char> ExceptionText => _exceptionText.Span;
public ReadOnlySpan<Range> ExceptionLineRanges => _exceptionLineRanges.Span;
public ReadOnlySpan<char> CategoryNamePart => _categoryNamePart.Span;
public ReadOnlySpan<char> TimeStampPart => _timeStampPart.Span;
public ReadOnlySpan<char> NewLine => _newLine.Span;
public ReadOnlySpan<char> LeadingWhiteSpace => _leadingWhiteSpace.Span;

public int Length { get; }

private FormattedMessageState(
ReadOnlyMemory<char> messageText, ReadOnlyMemory<Range> messageLineRanges,
ReadOnlyMemory<char> exceptionText, ReadOnlyMemory<Range> exceptionLineRanges,
ReadOnlyMemory<char> categoryNamePart,
ReadOnlyMemory<char> timeStampPart,
ReadOnlyMemory<char> newLine,
ReadOnlyMemory<char> leadingWhiteSpace)
{
_message = messageText;
_messageLineRanges = messageLineRanges;
_exceptionText = exceptionText;
_exceptionLineRanges = exceptionLineRanges;
_categoryNamePart = categoryNamePart;
_timeStampPart = timeStampPart;
_newLine = newLine;
_leadingWhiteSpace = leadingWhiteSpace;

Length = ComputeLength();
}

private int ComputeLength()
{
// Calculate the length of the final formatted string.
var isFirst = true;
var length = 0;

length += CategoryNamePart.Length;

foreach (var range in MessageLineRanges)
{
if (isFirst)
{
length += TimeStampPart.Length;
isFirst = false;
}
else
{
length += NewLine.Length;
length += LeadingWhiteSpace.Length;
}

var (_, lineLength) = range.GetOffsetAndLength(MessageText.Length);
length += lineLength;
}

foreach (var range in ExceptionLineRanges)
{
length += TimeStampPart.Length;

var (_, lineLength) = range.GetOffsetAndLength(ExceptionText.Length);
length += lineLength;
}

return length;
}

public static FormattedMessageState Create(
string message,
string categoryName,
Exception? exception,
bool includeTimeStamp,
ref MemoryBuilder<Range> messageLineRangeBuilder,
ref MemoryBuilder<Range> exceptionLineRangeBuilder)
{
var messageText = message.AsMemory();
var newLine = Environment.NewLine.AsMemory();
DustinCampbell marked this conversation as resolved.
Show resolved Hide resolved

var categoryNamePart = ('[' + categoryName + "] ").AsMemory();

ReadOnlyMemory<char> timeStampPart, leadingWhiteSpace;

if (includeTimeStamp)
{
timeStampPart = ('[' + DateTime.Now.TimeOfDay.ToString("hh\\:mm\\:ss\\.fffffff") + "] ").AsMemory();
leadingWhiteSpace = s_leadingWhiteSpace;
}
else
{
timeStampPart = default;
leadingWhiteSpace = default;
}

// Collect the range of each line in the message text.
CollectLineRanges(messageText.Span, newLine.Span, ref messageLineRangeBuilder);

var exceptionText = exception is not null
? exception.ToString().AsMemory()
: default;

// If specified, Collect the range of each line in the exception text.
if (exceptionText.Length > 0)
{
CollectLineRanges(exceptionText.Span, newLine.Span, ref exceptionLineRangeBuilder);
}

return new(
messageText, messageLineRangeBuilder.AsMemory(),
exceptionText, exceptionLineRangeBuilder.AsMemory(),
categoryNamePart, timeStampPart, newLine, leadingWhiteSpace);
}

private static void CollectLineRanges(ReadOnlySpan<char> source, ReadOnlySpan<char> newLine, ref MemoryBuilder<Range> builder)
{
var startIndex = 0;

while (startIndex < source.Length)
{
// Find the index of the next new line.
var endIndex = source[startIndex..].IndexOf(newLine);

// If endIndex == -1, there isn't another new line.
// So, add the remaining range and break.
if (endIndex == -1)
{
builder.Append(startIndex..);
break;
}

var realEndIndex = startIndex + endIndex;

builder.Append(startIndex..realEndIndex);
startIndex = realEndIndex + newLine.Length;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Razor;
using static System.StringExtensions;

namespace Microsoft.CodeAnalysis.Razor.Logging;

internal static partial class LogMessageFormatter
{
public static string FormatMessage(string message, string categoryName, Exception? exception, bool includeTimeStamp = true)
{
// Note
MemoryBuilder<Range> messageLineRangeBuilder = new(initialCapacity: 4);
MemoryBuilder<Range> exceptionLineRangeBuilder = exception is not null ? new(initialCapacity: 64) : default;
try
{
var state = FormattedMessageState.Create(
message, categoryName, exception, includeTimeStamp,
ref messageLineRangeBuilder, ref exceptionLineRangeBuilder);

// Create the final string.
return CreateString(state.Length, state, static (span, state) =>
{
Write(state.CategoryNamePart, ref span);

var isFirst = true;

foreach (var range in state.MessageLineRanges)
{
if (isFirst)
{
// Write the time stamp if this is the first line.
Write(state.TimeStampPart, ref span);
isFirst = false;
}
else
{
// Otherwise, write a new line and the leading whitespace.
Write(state.NewLine, ref span);
Write(state.LeadingWhiteSpace, ref span);
}

Write(state.MessageText[range], ref span);
}

foreach (var range in state.ExceptionLineRanges)
{
Write(state.LeadingWhiteSpace, ref span);
Write(state.ExceptionText[range], ref span);
}

Debug.Assert(span.Length == 0, "We didn't fill the whole span!");

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void Write(ReadOnlySpan<char> source, ref Span<char> destination)
{
if (source.IsEmpty)
{
return;
}

source.CopyTo(destination);
destination = destination[source.Length..];

Debug.Assert(destination.Length >= 0);
}
});
}
finally
{
messageLineRangeBuilder.Dispose();
exceptionLineRangeBuilder.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<NoWarn>$(NoWarn);IDE0073</NoWarn>
</PropertyGroup>

<ItemGroup>
<Using Alias="Range" Include="Microsoft.VisualStudio.LanguageServer.Protocol.Range" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="Microsoft.CodeAnalysis.ExternalAccess.Razor" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Text.Json.Serialization;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.Razor.Protocol.Debugging;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Text.Json.Serialization;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.CodeAnalysis.Razor.Protocol.DocumentMapping;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Text.Json.Serialization;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.Razor.Protocol.DocumentMapping;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Text.Json.Serialization;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.CodeAnalysis.Razor.Workspaces.Protocol.SemanticTokens;

Expand Down
Loading