-
Notifications
You must be signed in to change notification settings - Fork 8
/
Log.cs
140 lines (104 loc) · 4.03 KB
/
Log.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
[Flags]
public enum LogType : byte {
Debug = (1 << 0),
Info = (1 << 1),
Warning = (1 << 2),
Error = (1 << 3),
// Set the Log type to Stream to avoid overriding any colours from
// the incoming stream.
Stream = (1 << 4),
};
public class Log {
// All events enabled by default
private static LogType SubscribedEvents = LogType.Debug | LogType.Info | LogType.Warning | LogType.Error | LogType.Stream;
// buffered output
private static ConsoleColor originalColor = Console.ForegroundColor; // not sure whether that works here
private static byte[] byteCache = new byte[ 256 ];
private static int byteCount = 0;
private static int currentlyFlushing = 0;
private static Timer flushTimer;
public static void SetLevel(LogType logTypes) {
SubscribedEvents = logTypes | LogType.Stream;
}
private static void SetColorByLogType( LogType inType ) {
if ( inType == LogType.Stream ){
return;
}
ConsoleColor logColor;
switch ( inType ) {
case LogType.Debug: logColor = ConsoleColor.DarkGreen; break;
case LogType.Info: logColor = ConsoleColor.White; break;
case LogType.Warning: logColor = ConsoleColor.DarkYellow; break;
case LogType.Error: logColor = ConsoleColor.DarkRed; break;
default: return;
}
Console.ForegroundColor = logColor;
}
//public static void Write( string inMessage = "", LogType inType = LogType.Info ) {
// ConsoleColor originalColor = Console.ForegroundColor;
// if ( (SubscribedEvents & inType) == 0 )
// return;
// SetColorByLogType( inType );
// Console.Write( inMessage );
// if ( (inType & LogType.Stream) == 0 ){
// Console.ForegroundColor = originalColor;
// }
//}
// buffered output version, force flushed on timeout (only Write for now)
public static void Write( string inMessage = "", LogType inType = LogType.Info ) {
if ( (SubscribedEvents & inType) == 0 )
return;
SetColorByLogType( inType );
byte[] messageBytes = Encoding.ASCII.GetBytes( inMessage ); // hmm
int messageLength = messageBytes.Length;
while ( currentlyFlushing == 1 ) {
Thread.Sleep( 1 );
}
for ( int i = 0; i < messageLength; i++ ) {
byteCache[ byteCount++ ] = messageBytes[ i ];
if ( byteCount == 256 ) {
Console.Write( Encoding.ASCII.GetString( byteCache ) );
byteCount = 0;
}
}
if ( (inType & LogType.Stream) == 0 ) {
Console.ForegroundColor = originalColor;
}
ResetFlushTimer();
}
private static void FlushBuffer( object state ) {
if ( byteCount > 0 ) {
currentlyFlushing = 1;
Console.Write( Encoding.ASCII.GetString( byteCache, 0, byteCount ) );
currentlyFlushing = 0;
byteCount = 0;
}
}
private static void ResetFlushTimer() {
flushTimer?.Dispose(); // Dispose previous timer instance if it exists
// force flush every 15 millis, stays a bit under a PSX vsync interval
flushTimer = new Timer( FlushBuffer, null, 15, Timeout.Infinite );
}
// END AI stuff
public static void WriteLine( string inMessage = "", LogType inType = LogType.Info ) {
ConsoleColor originalColor = Console.ForegroundColor;
if ( (SubscribedEvents & inType) == 0 )
return;
SetColorByLogType(inType);
Console.WriteLine( inMessage );
if ( (inType & LogType.Stream) == 0 ) {
Console.ForegroundColor = originalColor;
}
}
public static void TestMessage() {
WriteLine( "Debug", LogType.Debug );
WriteLine( "Info", LogType.Info );
WriteLine( "Warning", LogType.Warning );
WriteLine( "Error", LogType.Error );
}
}