diff --git a/OpenRA.Game/Support/PerfTimer.cs b/OpenRA.Game/Support/PerfTimer.cs index c674d55fe5..b574e8243b 100755 --- a/OpenRA.Game/Support/PerfTimer.cs +++ b/OpenRA.Game/Support/PerfTimer.cs @@ -9,6 +9,7 @@ #endregion using System; +using System.Linq; namespace OpenRA.Support { @@ -16,27 +17,53 @@ namespace OpenRA.Support { readonly Stopwatch sw = new Stopwatch(); readonly string Name; + + // Hacks to give the output a tree-like structure static System.Threading.ThreadLocal depth = new System.Threading.ThreadLocal(); + static System.Threading.ThreadLocal prevHeader = new System.Threading.ThreadLocal(); public PerfTimer(string name) { + if (prevHeader.Value != null) + { + Log.Write("perf", prevHeader.Value); + prevHeader.Value = null; + } + this.Name = name; + + prevHeader.Value = string.Format("{0}{1}", Indentation, this.Name); depth.Value++; } + private static string Indentation + { + get + { + var d = depth.Value; + if (d == 1) + return "| "; + else if (d <= 0) + return string.Empty; + else + return string.Concat(Enumerable.Repeat("| ", depth.Value)); + } + } + public void Dispose() { - string indentation; - - if (--depth.Value >= 0) - indentation = new string('\t', depth.Value); + string format; + if (prevHeader.Value == null) + { + format = "{0}: {2} ms"; + } else { - depth.Value = 0; - indentation = string.Empty; + format = "{0}{1}: {2} ms"; + prevHeader.Value = null; } - - Log.Write("perf", "{0}{1}: {2} ms", indentation, this.Name, Math.Round(this.sw.Elapsed.TotalMilliseconds)); + depth.Value--; + Log.Write("perf", format, Indentation, this.Name, Math.Round(this.sw.Elapsed.TotalMilliseconds)); } } }