Adding profile shims to IcdStopwatch

This commit is contained in:
Chris Cameron
2018-02-01 15:23:14 -05:00
parent 203ce7d66c
commit 06a82513ed
4 changed files with 209 additions and 1 deletions

View File

@@ -0,0 +1,108 @@
using System;
namespace ICD.Common.Utils
{
public enum eConsoleColor
{
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
YellowOnRed
}
public static class ConsoleColorExtensions
{
public const string CONSOLE_RED = "\x1B[31;1m";
public const string CONSOLE_GREEN = "\x1B[32;1m";
public const string CONSOLE_YELLOW = "\x1B[33;1m";
public const string CONSOLE_BLUE = "\x1B[34;1m";
public const string CONSOLE_MAGENTA = "\x1B[35;1m";
public const string CONSOLE_CYAN = "\x1B[36;1m";
public const string CONSOLE_WHITE = "\x1B[37;1m";
public const string CONSOLE_YELLOW_ON_RED_BACKGROUND = "\x1B[93;41m";
public const string CONSOLE_RESET = "\x1B[0m";
public static string FormatAnsi(this eConsoleColor extends, string data)
{
return string.Format("{0}{1}{2}", extends.ToAnsiPrefix(), data, CONSOLE_RESET);
}
public static string ToAnsiPrefix(this eConsoleColor extends)
{
switch (extends)
{
case eConsoleColor.Red:
return CONSOLE_RED;
case eConsoleColor.Green:
return CONSOLE_GREEN;
case eConsoleColor.Yellow:
return CONSOLE_YELLOW;
case eConsoleColor.Blue:
return CONSOLE_BLUE;
case eConsoleColor.Magenta:
return CONSOLE_MAGENTA;
case eConsoleColor.Cyan:
return CONSOLE_CYAN;
case eConsoleColor.White:
return CONSOLE_WHITE;
case eConsoleColor.YellowOnRed:
return CONSOLE_YELLOW_ON_RED_BACKGROUND;
default:
throw new ArgumentOutOfRangeException("extends");
}
}
#if STANDARD
public static ConsoleColor ToForegroundConsoleColor(this eConsoleColor extends)
{
switch (extends)
{
case eConsoleColor.Red:
return ConsoleColor.Red;
case eConsoleColor.Green:
return ConsoleColor.Green;
case eConsoleColor.Yellow:
case eConsoleColor.YellowOnRed:
return ConsoleColor.Yellow;
case eConsoleColor.Blue:
return ConsoleColor.Blue;
case eConsoleColor.Magenta:
return ConsoleColor.Magenta;
case eConsoleColor.Cyan:
return ConsoleColor.Cyan;
case eConsoleColor.White:
return ConsoleColor.White;
default:
throw new ArgumentOutOfRangeException("extends");
}
}
public static ConsoleColor ToBackgroundConsoleColor(this eConsoleColor extends)
{
switch (extends)
{
case eConsoleColor.Red:
case eConsoleColor.Green:
case eConsoleColor.Yellow:
case eConsoleColor.Blue:
case eConsoleColor.Magenta:
case eConsoleColor.Cyan:
case eConsoleColor.White:
return ConsoleColor.Black;
case eConsoleColor.YellowOnRed:
return ConsoleColor.Red;
default:
throw new ArgumentOutOfRangeException("extends");
}
}
#endif
}
}

View File

@@ -75,6 +75,7 @@
<ItemGroup>
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
<Compile Include="Attributes\Rpc\RpcAttribute.cs" />
<Compile Include="ConsoleColor.cs" />
<Compile Include="EventArguments\BoolEventArgs.cs" />
<Compile Include="EventArguments\CharEventArgs.cs" />
<Compile Include="EventArguments\DateTimeEventArgs.cs" />

View File

@@ -67,6 +67,24 @@ namespace ICD.Common.Utils
PrintLine(message);
}
public static void PrintLine(eConsoleColor color, string message)
{
#if SIMPLSHARP
PrintLine(color.FormatAnsi(message));
#else
System.Console.ForegroundColor = color.ToForegroundConsoleColor();
System.Console.BackgroundColor = color.ToBackgroundConsoleColor();
System.Console.Error.WriteLine(message);
System.Console.ResetColor();
#endif
}
public static void PrintLine(eConsoleColor color, string message, params object[] args)
{
message = string.Format(message, args);
PrintLine(color, message);
}
public static void Print(string message)
{
#if SIMPLSHARP
@@ -82,6 +100,24 @@ namespace ICD.Common.Utils
Print(message);
}
public static void Print(eConsoleColor color, string message)
{
#if SIMPLSHARP
Print(color.FormatAnsi(message));
#else
System.Console.ForegroundColor = color.ToForegroundConsoleColor();
System.Console.BackgroundColor = color.ToBackgroundConsoleColor();
System.Console.Error.Write(message);
System.Console.ResetColor();
#endif
}
public static void Print(eConsoleColor color, string message, params object[] args)
{
message = string.Format(message, args);
Print(color, message);
}
public static bool SendControlSystemCommand(string command, ref string result)
{
#if SIMPLSHARP

View File

@@ -1,4 +1,6 @@
#if SIMPLSHARP
using System;
using ICD.Common.Properties;
#if SIMPLSHARP
using Crestron.SimplSharp;
#else
using System.Diagnostics;
@@ -8,6 +10,10 @@ namespace ICD.Common.Utils.Timers
{
public sealed class IcdStopwatch
{
// Used with profiling
private const long YELLOW_MILLISECONDS = 100;
private const long RED_MILLISECONDS = 400;
private readonly Stopwatch m_Stopwatch;
#region Properties
@@ -80,5 +86,62 @@ namespace ICD.Common.Utils.Timers
}
#endregion
#region Profiling
/// <summary>
/// Executes the action and prints the duration to the console.
/// </summary>
/// <param name="action"></param>
/// <param name="name"></param>
[PublicAPI]
public static void Profile(Action action, string name)
{
if (action == null)
throw new ArgumentNullException("action");
IcdStopwatch stopwatch = StartNew();
action();
PrintProfile(stopwatch, name);
}
/// <summary>
/// Executes the function and prints the duration to the console.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="func"></param>
/// <param name="name"></param>
/// <returns></returns>
[PublicAPI]
public static T Profile<T>(Func<T> func, string name)
{
if (func == null)
throw new ArgumentNullException("func");
T output = default(T);
Profile(() =>
{
output = func();
}, name);
return output;
}
private static void PrintProfile(IcdStopwatch stopwatch, string name)
{
long elapsed = stopwatch.ElapsedMilliseconds;
eConsoleColor color = eConsoleColor.Green;
if (elapsed >= YELLOW_MILLISECONDS)
color = eConsoleColor.Yellow;
if (elapsed >= RED_MILLISECONDS)
color = eConsoleColor.Red;
IcdConsole.Print(color, "{0}ms", elapsed);
IcdConsole.PrintLine(" to execute {0}", name);
}
#endregion
}
}