feat: 在前端显示编译过程中的日志 (#67)

Reviewed-on: PostGuard/Canon#67
This commit is contained in:
2024-04-29 23:55:36 +08:00
parent 4d325569fa
commit 911c813996
15 changed files with 243 additions and 204 deletions

View File

@@ -0,0 +1,30 @@
using System.Text;
using Canon.Core.Abstractions;
namespace Canon.Server.Models;
public class CompilerLogger : ICompilerLogger
{
private readonly ThreadLocal<StringBuilder> _builder = new(() => new StringBuilder());
public string Build()
{
if (_builder.Value is not null)
{
string result = _builder.Value.ToString();
_builder.Value.Clear();
return result;
}
throw new InvalidOperationException();
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
if (_builder.Value is not null)
{
_builder.Value.Append(logLevel).Append(": ").Append(formatter(state, exception)).Append('\n');
}
}
}