Files
MSCO/MscoLog.cs
T

47 lines
1.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace MSCO
{
/// <summary>
/// Zentrales Log der Mod: alle Meldungen landen in der MSCO-Konsole (F7), nicht in der MSCLoader-Konsole.
/// Zusätzlich gehen sie per Debug.Log in die output_log.txt des Spiels (zur Fehlersuche), ohne dass sich die
/// MSCLoader-Konsole öffnet. Thread-sicher: Meldungen aus Hintergrund-Threads werden im Hauptthread übernommen.
/// </summary>
public static class MscoLog
{
const string Prefix = "[MSCO] ";
static readonly Queue<string> pending = new Queue<string>();
static readonly object gate = new object();
public static void Info(string text) { Add(text, ""); }
public static void Warning(string text) { Add(text, "<color=#f2c21d>! </color>"); }
public static void Error(string text) { Add(text, "<color=#ee5a5e>ERROR </color>"); }
static void Add(string text, string marker)
{
string line = Strip(text ?? "");
Debug.Log(Prefix + line); // output_log.txt
lock (gate)
{
pending.Enqueue(marker + line);
while (pending.Count > 500) pending.Dequeue(); // falls die Konsole (noch) nicht abgeholt hat
}
}
/// <summary>Neue Meldungen abholen (Hauptthread, vom MultiplayerManager jedes Frame).</summary>
public static void Drain(System.Action<string> sink)
{
lock (gate)
{
while (pending.Count > 0) sink(pending.Dequeue());
}
}
static string Strip(string text)
{
return text.StartsWith(Prefix) ? text.Substring(Prefix.Length) : text;
}
}
}