From c8ae393c07d527ea3e934b714acfd57d6c335c2d Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Fri, 1 Aug 2025 01:45:45 +1000 Subject: [PATCH] Terminal logger --- .../Features/Build/BuildService.cs | 6 +++- .../Features/Logging/InMemoryLogger.cs | 36 +++++++++++++++++++ .../Components/TerminalOutputDisplay.razor | 21 +++++++++++ src/SharpIDE.Photino/Layout/MainLayout.razor | 1 + 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/SharpIDE.Application/Features/Logging/InMemoryLogger.cs create mode 100644 src/SharpIDE.Photino/Components/TerminalOutputDisplay.razor diff --git a/src/SharpIDE.Application/Features/Build/BuildService.cs b/src/SharpIDE.Application/Features/Build/BuildService.cs index 6e9f864..1f2575b 100644 --- a/src/SharpIDE.Application/Features/Build/BuildService.cs +++ b/src/SharpIDE.Application/Features/Build/BuildService.cs @@ -1,7 +1,9 @@ using System.Diagnostics; +using System.Threading.Channels; using Microsoft.Build.Execution; using Microsoft.Build.Framework; using Microsoft.Build.Logging; +using SharpIDE.Application.Features.Logging; namespace SharpIDE.Application.Features.Build; @@ -14,6 +16,7 @@ public enum BuildType } public class BuildService { + public Channel BuildOutputChannel { get; } = Channel.CreateUnbounded(); public async Task MsBuildSolutionAsync(string solutionFilePath, BuildType buildType = BuildType.Build) { var buildParameters = new BuildParameters @@ -21,7 +24,8 @@ public class BuildService Loggers = [ //new BinaryLogger { Parameters = "msbuild.binlog" }, - new ConsoleLogger(LoggerVerbosity.Quiet), + new ConsoleLogger(LoggerVerbosity.Normal, message => BuildOutputChannel.Writer.TryWrite(message), s => { }, () => { }), + //new InMemoryLogger(LoggerVerbosity.Normal) ], }; string[] targetsToBuild = buildType switch diff --git a/src/SharpIDE.Application/Features/Logging/InMemoryLogger.cs b/src/SharpIDE.Application/Features/Logging/InMemoryLogger.cs new file mode 100644 index 0000000..e42e3a8 --- /dev/null +++ b/src/SharpIDE.Application/Features/Logging/InMemoryLogger.cs @@ -0,0 +1,36 @@ +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace SharpIDE.Application.Features.Logging; + +public sealed class InMemoryLogger : Logger +{ + public InMemoryLogger(LoggerVerbosity verbosity) + { + Verbosity = verbosity; + } + public override void Initialize(IEventSource eventSource) + { + //Register for the ProjectStarted, TargetStarted, and ProjectFinished events + eventSource.ProjectStarted += new ProjectStartedEventHandler(eventSource_ProjectStarted); + eventSource.TargetStarted += new TargetStartedEventHandler(eventSource_TargetStarted); + eventSource.ProjectFinished += new ProjectFinishedEventHandler(eventSource_ProjectFinished); + } + + void eventSource_ProjectStarted(object sender, ProjectStartedEventArgs e) + { + Console.WriteLine("Project Started: " + e.ProjectFile); + } + + void eventSource_ProjectFinished(object sender, ProjectFinishedEventArgs e) + { + Console.WriteLine("Project Finished: " + e.ProjectFile); + } + void eventSource_TargetStarted(object sender, TargetStartedEventArgs e) + { + if (Verbosity == LoggerVerbosity.Detailed) + { + Console.WriteLine("Target Started: " + e.TargetName); + } + } +} diff --git a/src/SharpIDE.Photino/Components/TerminalOutputDisplay.razor b/src/SharpIDE.Photino/Components/TerminalOutputDisplay.razor new file mode 100644 index 0000000..3e37b44 --- /dev/null +++ b/src/SharpIDE.Photino/Components/TerminalOutputDisplay.razor @@ -0,0 +1,21 @@ +@using SharpIDE.Application.Features.Build +@inject BuildService BuildService + +
+ @foreach(var line in _outputLines) + { + @line + } +
+ +@code { + private List _outputLines = []; + protected override async Task OnInitializedAsync() + { + await foreach (var logLine in BuildService.BuildOutputChannel.Reader.ReadAllAsync()) + { + _outputLines.Add(logLine); + await InvokeAsync(StateHasChanged); + } + } +} diff --git a/src/SharpIDE.Photino/Layout/MainLayout.razor b/src/SharpIDE.Photino/Layout/MainLayout.razor index 2479682..59736d9 100644 --- a/src/SharpIDE.Photino/Layout/MainLayout.razor +++ b/src/SharpIDE.Photino/Layout/MainLayout.razor @@ -35,6 +35,7 @@ } +