feat: color lines in LogWindow

This commit is contained in:
goat 2020-06-05 14:32:55 +02:00
parent 3cf46089cf
commit 8f3077d337
2 changed files with 23 additions and 13 deletions

View file

@ -7,13 +7,14 @@ using System.Threading.Tasks;
using Dalamud.Game.Command; using Dalamud.Game.Command;
using ImGuiNET; using ImGuiNET;
using Serilog; using Serilog;
using Serilog.Events;
namespace Dalamud.Interface namespace Dalamud.Interface
{ {
class DalamudLogWindow : IDisposable { class DalamudLogWindow : IDisposable {
private readonly CommandManager commandManager; private readonly CommandManager commandManager;
private bool autoScroll = true; private bool autoScroll = true;
private string logText = string.Empty; private List<(string line, Vector4 color)> logText = new List<(string line, Vector4 color)>();
private string commandText = string.Empty; private string commandText = string.Empty;
@ -26,17 +27,28 @@ namespace Dalamud.Interface
SerilogEventSink.Instance.OnLogLine -= Serilog_OnLogLine; SerilogEventSink.Instance.OnLogLine -= Serilog_OnLogLine;
} }
private void Serilog_OnLogLine(object sender, string e) private void Serilog_OnLogLine(object sender, (string line, LogEventLevel level) logEvent)
{ {
AddLog(e + "\n"); var color = logEvent.level switch
{
LogEventLevel.Error => new Vector4(1f, 0f, 0f, 1f),
LogEventLevel.Verbose => new Vector4(1f, 1f, 1f, 1f),
LogEventLevel.Debug => new Vector4(0.878f, 0.878f, 0.878f, 1f),
LogEventLevel.Information => new Vector4(1f, 1f, 1f, 1f),
LogEventLevel.Warning => new Vector4(1f, 0.709f, 0f, 1f),
LogEventLevel.Fatal => new Vector4(1f, 0f, 0f, 1f),
_ => throw new ArgumentOutOfRangeException()
};
AddLog(logEvent.line, color);
} }
public void Clear() { public void Clear() {
this.logText = string.Empty; this.logText.Clear();
} }
public void AddLog(string line) { public void AddLog(string line, Vector4 color) {
this.logText += line; this.logText.Add((line, color));
} }
public bool Draw() { public bool Draw() {
@ -86,7 +98,9 @@ namespace Dalamud.Interface
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0, 0)); ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0, 0));
ImGui.TextUnformatted(this.logText); foreach (var valueTuple in this.logText) {
ImGui.TextColored(valueTuple.color, valueTuple.line);
}
ImGui.PopStyleVar(); ImGui.PopStyleVar();

View file

@ -17,7 +17,7 @@ namespace Dalamud.Interface
public static SerilogEventSink Instance; public static SerilogEventSink Instance;
public event EventHandler<(string line, Vector4 color)> OnLogLine; public event EventHandler<(string line, LogEventLevel level)> OnLogLine;
public SerilogEventSink(IFormatProvider formatProvider) public SerilogEventSink(IFormatProvider formatProvider)
{ {
@ -33,11 +33,7 @@ namespace Dalamud.Interface
if (logEvent.Exception != null) if (logEvent.Exception != null)
message += "\n" + logEvent.Exception; message += "\n" + logEvent.Exception;
var color = logEvent.Level switch { OnLogLine?.Invoke(this, (message, logEvent.Level));
LogEventLevel.Error => Vector4.One
};
OnLogLine?.Invoke(this, (message, ));
} }
} }