-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebug.cs
174 lines (153 loc) · 4.78 KB
/
Debug.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
namespace AI
{
/// <summary>
/// Displays errors real-time.
/// </summary>
class Debug
{
F f;
Form form;
TextBox textbox;
public string version = "1.3";
Collection<ErrorData> errors = new Collection<ErrorData>();
bool disposed;
//make a collectiong of strings. each error string will
//have a counter (to prevent flooding).
//create a form and list all the errors in a textbox.
//optionaly save the error log to a file.
public Debug()
{
f = new F();
//initialise form
form = new System.Windows.Forms.Form();
form.Name = "Form";
form.Text = "Debug";
form.Size = new System.Drawing.Size(500, 400);
form.Disposed += new EventHandler(form_Disposed);
disposed = false;
form.Visible = false;
//initialise textbox
textbox = new TextBox();
textbox.Parent = form;
textbox.Text = "";
textbox.Multiline = true;
textbox.ScrollBars = ScrollBars.Both;
textbox.WordWrap = false;
textbox.Dock = DockStyle.Fill;
}
void form_Disposed(object sender, EventArgs e)
{
disposed = true;
}
public void NewError(ErrorData errorData)
{
if (disposed)
{
return;
}
//check if error exists
int c = errors.Count;
if (c != 0)
{
for (int i = 0; i < c; i++)
{
if (errorData.exception.Message == errors[i].exception.Message)
{
//error exists, add count
if (errors[i].count == 100)
{
errors[i].count = int.MaxValue;
Update();
return;
}
else if (errors[i].count > 100)
{
return;
}
else
{
errors[i].count++;
Update();
return;
}
}
}
}
//write log
string log, tab;
tab = " ";
//decription
log = DateTime.Now + ". Version " + version + Environment.NewLine;
log += tab + errorData.description + Environment.NewLine;
//exception
if (errorData.exception != null)
{
if (errorData.exception.Message != null)
{
log += tab + errorData.exception.Message + Environment.NewLine;
}
log += tab + errorData.exception.ToString() + Environment.NewLine;
if (errorData.exception.HelpLink != null)
{
log += tab + errorData.exception.HelpLink + Environment.NewLine;
}
}
//data
c = errorData.stringArrays.Count;
if (c != 0)
{
for (int i = 0; i < c; i++)
{
log += tab + f.ToString(errorData.stringArrays[i], "|") + Environment.NewLine;
}
}
c = errorData.strings.Count;
if (c != 0)
{
for (int i = 0; i < c; i++)
{
log += tab + errorData.strings[i] + Environment.NewLine;
}
}
//portal.AppendData(log, file_error);
errorData.log = log;
errors.Add(errorData);
//-------
Update();
}
private void Update()
{
if (disposed)
{
return;
}
int c = errors.Count;
if (c == 0)
{
form.Visible = false;
return;
}
string str = "";
//list all the errors' logs
for (int i = 0; i < c; i++)
{
if (errors[i].count == int.MaxValue)
{
str += "x Infinity" + Environment.NewLine;
}
else
{
str += "x" + errors[i].count + Environment.NewLine;
}
str += errors[i].log;
}
textbox.Text = str;
form.Visible = true;
}
}
}