using System; using System.Collections.Generic; using System.Text; namespace NW4RTools { public class LogContext : IDisposable { // An awful, awful hack private readonly Logger Owner; public LogContext(Logger owner) { Owner = owner; } void IDisposable.Dispose() { Owner.Pop(); } } public class Logger { private List PrefixElements; private List Names; private int BlockCount; private string Prefix; public Logger() { Names = new List(); Names.Add("root"); PrefixElements = new List(); PrefixElements.Add("|"); RebuildPrefix(); } private void RebuildPrefix() { Prefix = string.Join(" ", PrefixElements.ToArray()) + "- "; } public void Send(string format, params object[] args) { if (BlockCount > 0) return; Console.Write(Prefix); Console.WriteLine(format, args); } public LogContext Push(string format, params object[] args) { string n = string.Format(format, args); Console.Write(Prefix); Console.Write("=== "); Console.WriteLine(n); Names.Add(n); PrefixElements.Add("-"); RebuildPrefix(); return new LogContext(this); } public void Pop() { PrefixElements.RemoveAt(PrefixElements.Count - 1); RebuildPrefix(); string n = Names[Names.Count - 1]; Names.RemoveAt(Names.Count - 1); Console.Write(Prefix); Console.Write("=/= end "); Console.WriteLine(n); } public void Block() { BlockCount += 1; } public void Unblock() { BlockCount -= 1; } } }