Improve PerfTimer output

This commit is contained in:
Pavlos Touboulidis
2014-04-23 03:42:27 +03:00
parent 60732bd9bd
commit c44d73d581

View File

@@ -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<int> depth = new System.Threading.ThreadLocal<int>();
static System.Threading.ThreadLocal<string> prevHeader = new System.Threading.ThreadLocal<string>();
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));
}
}
}