-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v 2 adds async and ability to set the textwriter
- Loading branch information
byronap
committed
Feb 7, 2019
1 parent
693309e
commit 0b50462
Showing
2 changed files
with
58 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,74 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Coinigy.Common | ||
public static class OutputConsole | ||
{ | ||
public static class OutputConsole | ||
public static TextWriter Writer { get; set; } | ||
public static bool IsWriterConsole { get; set; } | ||
|
||
private static readonly BlockingCollection<Tuple<string, ConsoleColor?, ConsoleColor?>> MQueue = new BlockingCollection<Tuple<string, ConsoleColor?, ConsoleColor?>>(); | ||
|
||
static OutputConsole() | ||
{ | ||
private static readonly BlockingCollection<Tuple<string, ConsoleColor?, ConsoleColor?>> MQueue = new BlockingCollection<Tuple<string, ConsoleColor?, ConsoleColor?>>(); | ||
Writer = Console.Out; | ||
IsWriterConsole = true; | ||
|
||
static OutputConsole() | ||
try | ||
{ | ||
try | ||
{ | ||
var thread = new Thread( | ||
() => | ||
var thread = new Thread( | ||
() => | ||
{ | ||
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); | ||
while (true) | ||
{ | ||
while (true) | ||
var (message, foreColor, backColor) = MQueue.Take(); | ||
try | ||
{ | ||
var item = MQueue.Take(); | ||
try | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
if (item.Item2 != null) | ||
Console.ForegroundColor = (ConsoleColor) item.Item2; | ||
if (item.Item3 != null) | ||
Console.BackgroundColor = (ConsoleColor) item.Item3; | ||
} | ||
} | ||
catch | ||
if (isWindows && IsWriterConsole) | ||
{ | ||
// ignore | ||
} | ||
|
||
Console.WriteLine(item.Item1); | ||
try | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
Console.ResetColor(); | ||
} | ||
catch | ||
{ | ||
// ignore | ||
if (foreColor != null) | ||
Console.ForegroundColor = (ConsoleColor)foreColor; | ||
if (backColor != null) | ||
Console.BackgroundColor = (ConsoleColor)backColor; | ||
} | ||
} | ||
catch | ||
{ | ||
// ignore | ||
} | ||
|
||
// ReSharper disable once FunctionNeverReturns | ||
}) | ||
{IsBackground = true}; | ||
thread.Start(); | ||
} | ||
catch (Exception) | ||
{ | ||
// ignored | ||
} | ||
Writer.WriteLine(message); | ||
try | ||
{ | ||
if (isWindows && IsWriterConsole) | ||
Console.ResetColor(); | ||
} | ||
catch | ||
{ | ||
// ignore | ||
} | ||
} | ||
}) | ||
{ IsBackground = true }; | ||
thread.Start(); | ||
} | ||
|
||
public static void WriteLine(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null) | ||
catch (Exception) | ||
{ | ||
MQueue.Add(new Tuple<string, ConsoleColor?, ConsoleColor?>(value, foregroundColor, backgroundColor)); | ||
// ignored | ||
} | ||
} | ||
|
||
public static void WriteLine(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null) | ||
{ | ||
MQueue.Add(new Tuple<string, ConsoleColor?, ConsoleColor?>(value, foregroundColor, backgroundColor)); | ||
} | ||
|
||
public static Task WriteLineAsync(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null) | ||
{ | ||
return Task.Run(() => { MQueue.Add(new Tuple<string, ConsoleColor?, ConsoleColor?>(value, foregroundColor, backgroundColor)); }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters