This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Report.cs
244 lines (215 loc) · 5.12 KB
/
Report.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
namespace Mono.Debugger
{
[Flags]
public enum DebugFlags {
None = 0,
JitSymtab = 1,
MethodAddress = 2,
Threads = 4,
Signals = 8,
EventLoop = 16,
Wait = 32,
SSE = 64,
Notification = 128,
Mutex = 256,
SymbolTable = 512,
SourceFiles = 1024,
DwarfReader = 2048,
Remoting = 4096,
NUnit = 8192,
GUI = 16384,
CLI = 32768
}
public static class Report
{
static ReportWriter writer;
public static ReportWriter ReportWriter {
get { return writer; }
}
public static void Initialize ()
{
writer = new ReportWriter ();
}
public static void Initialize (ReportWriter the_writer)
{
writer = the_writer;
}
public static void Initialize (string file, DebugFlags flags)
{
writer = new ReportWriter (file, flags);
}
public static bool ParseDebugFlags (string value, out DebugFlags flags)
{
int int_flags = 0;
if (Int32.TryParse (value, out int_flags)) {
flags = (DebugFlags) int_flags;
return true;
}
flags = DebugFlags.None;
foreach (string flag in value.Split (',')) {
switch (flag) {
case "jit":
flags |= DebugFlags.JitSymtab;
break;
case "address":
flags |= DebugFlags.MethodAddress;
break;
case "threads":
flags |= DebugFlags.Threads;
break;
case "signals":
flags |= DebugFlags.Signals;
break;
case "eventloop":
flags |= DebugFlags.EventLoop;
break;
case "wait":
flags |= DebugFlags.Wait;
break;
case "sse":
flags |= DebugFlags.SSE;
break;
case "notification":
flags |= DebugFlags.Notification;
break;
case "mutex":
flags |= DebugFlags.Mutex;
break;
case "symtab":
flags |= DebugFlags.SymbolTable;
break;
case "sources":
flags |= DebugFlags.SourceFiles;
break;
case "dwarf":
flags |= DebugFlags.DwarfReader;
break;
case "remoting":
flags |= DebugFlags.Remoting;
break;
case "nunit":
flags |= DebugFlags.NUnit;
break;
case "gui":
flags |= DebugFlags.GUI;
break;
case "cli":
flags |= DebugFlags.CLI;
break;
default:
return false;
}
}
return true;
}
[Conditional("DEBUG")]
public static void Debug (DebugFlags category, object argument)
{
Debug (category, "{0}", argument);
}
[Conditional("DEBUG")]
public static void Debug (DebugFlags category, string message, params object[] args)
{
string formatted = String.Format (message, args);
ReportWriter.Debug (category, formatted);
}
public static void Print (string message, params object[] args)
{
string formatted = String.Format (message, args);
ReportWriter.Print (formatted);
}
public static void Error (string message, params object[] args)
{
string formatted = String.Format (message, args);
ReportWriter.Error (formatted);
}
public static string ReadLine ()
{
return ReportWriter.ReadLine ();
}
}
public class ReportWriter : DebuggerMarshalByRefObject
{
DebugFlags flags;
string file;
DateTime start_time;
bool print_to_console = true;
StreamWriter writer;
public ReportWriter ()
{
string var = Environment.GetEnvironmentVariable ("MDB_DEBUG_FLAGS");
if (var != null) {
int pos = var.IndexOf (':');
if (pos > 0) {
file = var.Substring (0, pos);
var = var.Substring (pos + 1);
}
if (!Report.ParseDebugFlags (var, out flags))
Console.WriteLine ("Invalid `MDB_DEBUG_FLAGS' environment variable.");
}
if (file != null)
writer = new StreamWriter (file, true);
else
writer = new StreamWriter (Console.OpenStandardError ());
writer.AutoFlush = true;
start_time = DateTime.Now;
}
public ReportWriter (string file, DebugFlags flags)
{
this.file = file;
this.flags = flags;
if (file != null)
writer = new StreamWriter (file, true);
else
writer = new StreamWriter (Console.OpenStandardError ());
writer.AutoFlush = true;
}
public DebugFlags DebugFlags {
get { return flags; }
}
public string FileName {
get { return file; }
}
public bool PrintToConsole {
get { return print_to_console; }
set { print_to_console = value; }
}
protected void DoWriteLine (string message)
{
if (file != null) {
TimeSpan diff = DateTime.Now - start_time;
string timestamp = new DateTime (diff.Ticks).ToString ("mm:ss:fffffff", CultureInfo.InvariantCulture);
writer.WriteLine ("[{0}] {1}", timestamp, message);
} else
writer.WriteLine ("{0}", message);
}
public void Debug (DebugFlags category, string message)
{
if (((int) category & (int) flags) == 0)
return;
DoWriteLine (message);
}
public void Print (string message)
{
writer.Write (message);
if (print_to_console && (file != null))
Console.Write (message);
}
public void Error (string message)
{
DoWriteLine (message);
if (print_to_console && (file != null))
Console.WriteLine (message);
}
public string ReadLine ()
{
return Console.ReadLine ();
}
}
}