mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-23 16:27:44 +01:00
feat: add log debug window
This commit is contained in:
parent
fc5a8bc38e
commit
84e98e0315
5 changed files with 226 additions and 63 deletions
85
Dalamud/Interface/DalamudLogWindow.cs
Normal file
85
Dalamud/Interface/DalamudLogWindow.cs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ImGuiNET;
|
||||
|
||||
namespace Dalamud.Interface
|
||||
{
|
||||
class DalamudLogWindow : IDisposable {
|
||||
private bool autoScroll = true;
|
||||
private string logText = string.Empty;
|
||||
|
||||
public DalamudLogWindow() {
|
||||
SerilogEventSink.Instance.OnLogLine += Serilog_OnLogLine;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
SerilogEventSink.Instance.OnLogLine -= Serilog_OnLogLine;
|
||||
}
|
||||
|
||||
private void Serilog_OnLogLine(object sender, string e)
|
||||
{
|
||||
AddLog(e + "\n");
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
this.logText = string.Empty;
|
||||
}
|
||||
|
||||
public void AddLog(string line) {
|
||||
this.logText += line;
|
||||
}
|
||||
|
||||
public bool Draw() {
|
||||
ImGui.SetNextWindowSize(new Vector2(500, 400), ImGuiCond.FirstUseEver);
|
||||
|
||||
var isOpen = true;
|
||||
|
||||
if (!ImGui.Begin("Dalamud LOG", ref isOpen, ImGuiWindowFlags.NoCollapse))
|
||||
{
|
||||
ImGui.End();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Options menu
|
||||
if (ImGui.BeginPopup("Options"))
|
||||
{
|
||||
ImGui.Checkbox("Auto-scroll", ref this.autoScroll);
|
||||
ImGui.EndPopup();
|
||||
}
|
||||
|
||||
// Main window
|
||||
if (ImGui.Button("Options"))
|
||||
ImGui.OpenPopup("Options");
|
||||
ImGui.SameLine();
|
||||
var clear = ImGui.Button("Clear");
|
||||
ImGui.SameLine();
|
||||
var copy = ImGui.Button("Copy");
|
||||
ImGui.SameLine();
|
||||
|
||||
ImGui.BeginChild("scrolling", new Vector2(0, 0), false, ImGuiWindowFlags.HorizontalScrollbar);
|
||||
|
||||
if (clear)
|
||||
Clear();
|
||||
if (copy)
|
||||
ImGui.LogToClipboard();
|
||||
|
||||
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0, 0));
|
||||
|
||||
ImGui.TextUnformatted(this.logText);
|
||||
|
||||
ImGui.PopStyleVar();
|
||||
|
||||
if (this.autoScroll && ImGui.GetScrollY() >= ImGui.GetScrollMaxY())
|
||||
ImGui.SetScrollHereY(1.0f);
|
||||
|
||||
ImGui.EndChild();
|
||||
ImGui.End();
|
||||
|
||||
return isOpen;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
Dalamud/Interface/SerilogEventSink.cs
Normal file
44
Dalamud/Interface/SerilogEventSink.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Serilog;
|
||||
using Serilog.Configuration;
|
||||
using Serilog.Core;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace Dalamud.Interface
|
||||
{
|
||||
public class SerilogEventSink : ILogEventSink
|
||||
{
|
||||
private readonly IFormatProvider _formatProvider;
|
||||
|
||||
public static SerilogEventSink Instance;
|
||||
|
||||
public event EventHandler<string> OnLogLine;
|
||||
|
||||
public SerilogEventSink(IFormatProvider formatProvider)
|
||||
{
|
||||
_formatProvider = formatProvider;
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public void Emit(LogEvent logEvent)
|
||||
{
|
||||
var message = logEvent.RenderMessage(_formatProvider);
|
||||
OnLogLine?.Invoke(this, DateTimeOffset.Now.ToString() + " " + message);
|
||||
}
|
||||
}
|
||||
|
||||
public static class MySinkExtensions
|
||||
{
|
||||
public static LoggerConfiguration EventSink(
|
||||
this LoggerSinkConfiguration loggerConfiguration,
|
||||
IFormatProvider formatProvider = null)
|
||||
{
|
||||
return loggerConfiguration.Sink(new SerilogEventSink(formatProvider));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue