mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
fix: Logic for determining if there is a console output window, don't try to write to console if no console window
This commit is contained in:
@@ -25,9 +25,47 @@ namespace ICD.Common.Utils
|
|||||||
public static event EventHandler<StringEventArgs> OnConsolePrint;
|
public static event EventHandler<StringEventArgs> OnConsolePrint;
|
||||||
|
|
||||||
private static readonly SafeCriticalSection s_Section;
|
private static readonly SafeCriticalSection s_Section;
|
||||||
|
|
||||||
private static readonly Regex s_NewLineRegex;
|
private static readonly Regex s_NewLineRegex;
|
||||||
|
|
||||||
|
private static bool? s_IsConsoleApp;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if the application is being run from an interactive console,
|
||||||
|
/// false if the application is being run as a headless service.
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public static bool IsConsoleApp
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (s_IsConsoleApp == null)
|
||||||
|
{
|
||||||
|
#if SIMPLSHARP
|
||||||
|
s_IsConsoleApp = true;
|
||||||
|
#else
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Hack
|
||||||
|
int unused = Console.WindowHeight;
|
||||||
|
s_IsConsoleApp = true;
|
||||||
|
|
||||||
|
if (Console.Title.Length > 0)
|
||||||
|
s_IsConsoleApp = true;
|
||||||
|
|
||||||
|
if (!Environment.UserInteractive)
|
||||||
|
s_IsConsoleApp = false;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
s_IsConsoleApp = false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return s_IsConsoleApp.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Static constructor.
|
/// Static constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -92,6 +130,8 @@ namespace ICD.Common.Utils
|
|||||||
CrestronConsole.PrintLine(fixedMessage);
|
CrestronConsole.PrintLine(fixedMessage);
|
||||||
#else
|
#else
|
||||||
Trace.WriteLine(AnsiUtils.StripAnsi(fixedMessage));
|
Trace.WriteLine(AnsiUtils.StripAnsi(fixedMessage));
|
||||||
|
|
||||||
|
if (IsConsoleApp)
|
||||||
Console.WriteLine(fixedMessage);
|
Console.WriteLine(fixedMessage);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -134,6 +174,8 @@ namespace ICD.Common.Utils
|
|||||||
CrestronConsole.Print(fixedMessage);
|
CrestronConsole.Print(fixedMessage);
|
||||||
#else
|
#else
|
||||||
Trace.Write(AnsiUtils.StripAnsi(fixedMessage));
|
Trace.Write(AnsiUtils.StripAnsi(fixedMessage));
|
||||||
|
|
||||||
|
if (IsConsoleApp)
|
||||||
Console.Write(fixedMessage);
|
Console.Write(fixedMessage);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user