blob: 37d3f43046095f2d4d59deb6a166158cd0dca54a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Tango.Logging
{
/// <summary>
/// Represents stand-alone visual console emulation logger.
/// </summary>
/// <seealso cref="Tango.Logging.ILogger" />
public class ConsoleLogger : ILogger
{
private bool _consoleOpened;
private String _consoleTitle;
private ConsoleWindow console;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="ILogger" /> is enabled.
/// </summary>
public bool Enabled { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
/// </summary>
public ConsoleLogger(String consoleTitle)
{
_consoleTitle = consoleTitle;
Enabled = true;
}
/// <summary>
/// Called when a new library trace is available.
/// </summary>
/// <param name="output">The output.</param>
public void OnLog(LogItemBase output)
{
EnsureConsoleOpen(() =>
{
switch (output.Category)
{
case LogCategory.Info:
console.SetColor(ConsoleColor.White);
break;
case LogCategory.Warning:
console.SetColor(ConsoleColor.Yellow);
break;
case LogCategory.Error:
console.SetColor(ConsoleColor.Red);
break;
case LogCategory.Critical:
console.SetColor(ConsoleColor.DarkRed);
break;
case LogCategory.Debug:
console.SetColor(ConsoleColor.Gray);
break;
}
console.WriteLine(output.ToString());
});
}
/// <summary>
/// Waits until user closes the console.
/// </summary>
/// <returns></returns>
public async Task WaitForConsoleExit()
{
if (console != null)
{
await console.WaitForUserClose();
}
}
/// <summary>
/// Ensures the console is open.
/// </summary>
private void EnsureConsoleOpen(Action post)
{
if (!_consoleOpened)
{
Thread t = new Thread(() =>
{
console = new ConsoleWindow();
console.Title += " - " + _consoleTitle;
_consoleOpened = true;
console.Show();
console.Closed += (sender2, e2) => console.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
t.IsBackground = true;
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
while (!_consoleOpened)
{
Thread.Sleep(10);
}
post();
}
}
}
|