Skip to content

Instantly share code, notes, and snippets.

@bitm0de
Last active October 15, 2018 07:08
Show Gist options
  • Save bitm0de/9674636bb8793473e12940e4760e4b60 to your computer and use it in GitHub Desktop.
Save bitm0de/9674636bb8793473e12940e4760e4b60 to your computer and use it in GitHub Desktop.
Crestron Debug Class
/*
* Copyright (c) 2017 Troy Garner
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
// ReSharper disable UnusedMember.Global
using System;
using System.Diagnostics;
using Crestron.SimplSharp;
using Crestron.SimplSharp.Reflection;
using Crestron.SimplSharp.CrestronLogger;
using System.Text;
namespace CoreUtilities
{
/// <summary>
/// Class for all diagnostic debugging utilities where most debug methods are conditionally
/// enabled by the DEBUG compiler directive. (Any fatal or more severe indicator methods are
/// not disabled for RELEASE builds such as exception messages.)
/// </summary>
internal static class Debug
{
/// <summary>Debug flag to set in the project settings for enabling debug output</summary>
public const string DEBUG_FLAG_DIRECTIVE = "DEBUG";
/// <summary>Flag for enabling debug trace messages</summary>
public static bool EnableTrace { get; set; }
/// <summary>Current executing assembly name</summary>
private static readonly string _assemblyPrefix;
/// <summary>Initializes the debug class</summary>
static Debug()
{
try
{
var assemblyName = Assembly.GetExecutingAssembly().GetName();
_assemblyPrefix = string.Format("{0} {1}", assemblyName.Name, assemblyName.Version);
CrestronLogger.Initialize(1, false, LoggerModeEnum.DEFAULT);
}
catch (Exception ex)
{
CrestronConsole.PrintLine(ex.Message);
throw;
}
}
/// <summary>
/// Throws the specified exception with the exception message written to console as an error
/// </summary>
/// <typeparam name="T">Exception type</typeparam>
/// <param name="exception">Exception</param>
public static void ThrowWithMessage<T>(T exception) where T : Exception
{
WriteException(exception.Message);
LogException(exception.Message);
throw exception;
}
/// <summary>
/// Private helper method to return formatted message string prefixed with the assembly identifier.
/// </summary>
/// <param name="message">Message to be written to the console</param>
/// <param name="args">Format arguments for the message</param>
private static string InternalFormat(string message, params object[] args)
{
return string.Format("[{0}]:{1} - {2}", _assemblyPrefix, InitialParametersClass.ApplicationNumber, string.Format(message, args));
}
/// <summary>
/// Writes a message to the error log
/// </summary>
/// <param name="message">Message to be written to the console</param>
/// <param name="args">Format arguments for the message</param>
public static void ErrorLog(string message, params object[] args)
{
CrestronLogger.WriteToLog(InternalFormat(message, args), 1);
}
/// <summary>
/// Writes an exception debug message to the console with a newline.
/// </summary>
/// <param name="message">Format message to output to the console</param>
/// <param name="args">Format message string arguments (if applicable)</param>
public static void LogException(string message, params object[] args)
{
ErrorLog("NOTICE: [Exception] {0}", string.Format(message, args));
WriteException(message, args); // Exceptions should to be visible everywhere
}
public static void LogException(Exception ex) { LogException("{0}: {1}", ex.Message, ex.StackTrace); }
/// <summary>
/// Private helper method to write formatted message to console prefixed with the assembly identifier.
/// </summary>
/// <param name="message">Message to be written to the console</param>
/// <param name="args">Format arguments for the message</param>
private static void InternalWrite(string message, params object[] args)
{
CrestronConsole.Print(InternalFormat(message, args));
}
/// <summary>
/// Writes a trace message to the console if the trace option is enabled.
/// </summary>
/// <param name="fmt">Format string used to write to the console</param>
/// <param name="args">Format arguments for the message</param>
public static void TraceWrite(string fmt, params object[] args)
{
if (!EnableTrace) { return; }
Write(string.Format("\x00fa\x00e0{0}{1}\x00fb", (byte)Crestron.SimplSharp.InitialParametersClass.ApplicationNumber, fmt), args);
}
/// <summary>
/// Writes a raw debug message to the console without a newline.
/// </summary>
/// <param name="message">Format message to output to the console</param>
/// <param name="args">Format message string arguments (if applicable)</param>
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void Write(string message, params object[] args)
{
InternalWrite(message, args);
}
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void Write(object obj) { Write("{0}", obj); }
/// <summary>
/// Writes a raw debug message to the console with a newline.
/// </summary>
/// <param name="message">Format message to output to the console</param>
/// <param name="args">Format message string arguments (if applicable)</param>
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void WriteLine(string message, params object[] args)
{
Write("{0}\n", string.Format(message, args));
}
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void WriteLine(object obj) { WriteLine("{0}", obj); }
/// <summary>
/// Writes an error debug message to the console with a newline.
/// </summary>
/// <param name="message">Format message to output to the console</param>
/// <param name="args">Format message string arguments (if applicable)</param>
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void WriteError(string message, params object[] args)
{
WriteLine("Error: {0}", string.Format(message, args));
}
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void WriteError(object obj) { WriteError("{0}", obj); }
/// <summary>
/// Writes an exception debug message to the console with a newline.
/// </summary>
/// <param name="message">Format message to output to the console</param>
/// <param name="args">Format message string arguments (if applicable)</param>
public static void WriteException(string message, params object[] args)
{
WriteLine("NOTICE: [Exception] {0}", string.Format(message, args));
}
public static void WriteException(Exception obj) { WriteException("{0}", obj); }
/// <summary>
/// Writes a warning debug message to the console with a newline.
/// </summary>
/// <param name="message">Format message to output to the console</param>
/// <param name="args">Format message string arguments (if applicable)</param>
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void WriteWarning(string message, params object[] args)
{
WriteLine("Warning: {0}", string.Format(message, args));
}
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void WriteWarning(object obj) { WriteWarning("{0}", obj); }
/// <summary>
/// Writes an info debug message to the console with a newline.
/// </summary>
/// <param name="message">Format message to output to the console</param>
/// <param name="args">Format message string arguments (if applicable)</param>
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void WriteInfo(string message, params object[] args)
{
WriteLine("Info: {0}", string.Format(message, args));
}
[Conditional(DEBUG_FLAG_DIRECTIVE)]
public static void WriteInfo(object obj) { WriteInfo("{0}", obj); }
/// <summary>
/// Conditionally write using the specified Debug Write method delegate type.
/// </summary>
/// <param name="condition">Condition that must evaluate as true before the write method is invoked</param>
/// <param name="method">Debug write method delegate type</param>
/// <param name="message">Message to write</param>
/// <param name="args">Message arguments</param>
public static void ConditionalWrite(bool condition, Action<string, object[]> method, string message, params object[] args)
{
if (condition && method != null) method.Invoke(message, args);
}
/// <summary>
/// Conditionally write using the specified Debug Write method delegate type.
/// </summary>
/// <param name="condition">Condition that must evaluate as true before the write method is invoked</param>
/// <param name="method">Debug write method delegate type</param>
/// <param name="obj">Object to write</param>
/// <typeparam name="T">Type of params to write</typeparam>
public static void ConditionalWrite<T>(bool condition, Action<T> method, T obj)
{
if (condition && method != null) method.Invoke(obj);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment