Skip to content

Commit

Permalink
Merge pull request #2173 from OmniSharp/feature/log-timing
Browse files Browse the repository at this point in the history
Include timing info in logged responses
  • Loading branch information
filipw authored Jun 12, 2021
2 parents 5b7fe24 + ac8ed49 commit da1e65b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/OmniSharp.Stdio/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using OmniSharp.Services;
using OmniSharp.Protocol;
using OmniSharp.Utilities;
using System.Globalization;

namespace OmniSharp.Stdio
{
Expand All @@ -30,6 +31,7 @@ internal class Host : IDisposable
private readonly IOmniSharpEnvironment _environment;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly CachedStringBuilder _cachedStringBuilder;
private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;

public Host(
TextReader input, ISharedTextWriter writer, IOmniSharpEnvironment environment,
Expand Down Expand Up @@ -195,6 +197,7 @@ public void Start()

private async Task HandleRequest(string json, ILogger logger)
{
var startTimestamp = Stopwatch.GetTimestamp();
var request = RequestPacket.Parse(json);
if (logger.IsEnabled(LogLevel.Debug))
{
Expand Down Expand Up @@ -243,7 +246,10 @@ private async Task HandleRequest(string json, ILogger logger)
LogRequest(json, logger, LogLevel.Warning);
}

LogResponse(response.ToString(), logger, response.Success);
var currentTimestamp = Stopwatch.GetTimestamp();
var elapsed = new TimeSpan((long)(TimestampToTicks * (currentTimestamp - startTimestamp)));

LogResponse(response.ToString(), logger, response.Success, elapsed);
}

// actually write it
Expand All @@ -266,12 +272,12 @@ void LogRequest(string json, ILogger logger, LogLevel logLevel)
}
}

void LogResponse(string json, ILogger logger, bool isSuccess)
void LogResponse(string json, ILogger logger, bool isSuccess, TimeSpan elapsed)
{
var builder = _cachedStringBuilder.Acquire();
try
{
builder.AppendLine("************ Response ************ ");
builder.AppendLine($"************ Response ({elapsed.TotalMilliseconds.ToString("0.0000", CultureInfo.InvariantCulture)}ms) ************ ");
builder.Append(JToken.Parse(json).ToString(Formatting.Indented));

if (isSuccess)
Expand Down

0 comments on commit da1e65b

Please sign in to comment.