Skip to content

Commit

Permalink
Add more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dibarbet committed Oct 11, 2022
1 parent e899cb7 commit 2f4e117
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected override async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsWork
var documentDiagnostics = await diagnosticAnalyzerService.GetDiagnosticsForIdsAsync(Document.Project.Solution, Document.Project.Id, Document.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
if (Document.FilePath?.Contains("Test.txt") == true && documentDiagnostics.IsEmpty)
{
context.TraceInformation("What analyzers? " + string.Join(",", Document.Project.Solution.AnalyzerReferences.Select(s => s.Display)));
context.TraceInformation("What analyzers? " + string.Join(",", Document.Project.Solution.AnalyzerReferences.SelectMany(s => s.GetAnalyzersForAllLanguages()).Select(a => a.GetType().Name)));
//Debugger.Launch();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class AdditionalFileDiagnosticsTests : AbstractPullDiagnosticTestsBase
public AdditionalFileDiagnosticsTests(ITestOutputHelper testOutputHelper)
{
_testOutputLogger = new TestOutputLspLogger(testOutputHelper);
Console.SetOut(_testOutputLogger);
}

[Theory]
Expand Down Expand Up @@ -56,6 +57,8 @@ public async Task TestWorkspaceDiagnosticsReportsAdditionalFileDiagnostic([Combi
Assert.Null(results2[1].Diagnostics);
Assert.Equal(results[1].ResultId, results2[1].ResultId);
Assert.Null(results2[2].Diagnostics);

Assert.False(true);
}

[Theory, CombinatorialData]
Expand Down Expand Up @@ -147,13 +150,20 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(_descriptor);

public override void Initialize(AnalysisContext context)
=> context.RegisterCompilationStartAction(CreateAnalyzerWithinCompilation);
{
Console.WriteLine("Initializing mockdiagnosticanalyzer");
context.RegisterCompilationStartAction(CreateAnalyzerWithinCompilation);
}

public void CreateAnalyzerWithinCompilation(CompilationStartAnalysisContext context)
=> context.RegisterAdditionalFileAction(AnalyzeCompilation);

public void AnalyzeCompilation(AdditionalFileAnalysisContext context)
=> context.ReportDiagnostic(Diagnostic.Create(_descriptor,
location: Location.Create(context.AdditionalFile.Path, Text.TextSpan.FromBounds(0, 0), new Text.LinePositionSpan(new Text.LinePosition(0, 0), new Text.LinePosition(0, 0))), "args"));
{
Console.WriteLine($"analyze compilation");
Console.WriteLine($"analyzing additional file: {context.AdditionalFile.Path}");
context.ReportDiagnostic(Diagnostic.Create(_descriptor,
location: Location.Create(context.AdditionalFile.Path, Text.TextSpan.FromBounds(0, 0), new Text.LinePositionSpan(new Text.LinePosition(0, 0), new Text.LinePosition(0, 0))), "args"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,37 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests;
internal class TestOutputLspLogger : ILspServiceLogger
internal class TestOutputLspLogger : TextWriter, ILspServiceLogger
{
private readonly ITestOutputHelper _testOutputHelper;
public TestOutputLspLogger(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

public override Encoding Encoding => Encoding.UTF8;

public override void WriteLine(string message)
{
Log("Console", message);
}
public override void WriteLine(string format, params object[] args)
{
Log("Console", format, args);
}

public override void Write(char value)
{
throw new NotSupportedException("This text writer only supports WriteLine(string) and WriteLine(string, params object[]).");
}

public void LogEndContext(string message, params object[] @params) => Log("End", message, @params);

public void LogError(string message, params object[] @params) => Log("Error", message, @params);
Expand Down

0 comments on commit 2f4e117

Please sign in to comment.