/
LogParser.cs
357 lines (306 loc) · 13.1 KB
/
LogParser.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
namespace ES_DKP_Utils
{
public class LogParser
{
public enum LogLineType { TELL, WHO, LOOT, NA }
#region Declarations
private FileStream logStream;
private long logPointer;
private long logSize;
private FileSystemWatcher logWatcher;
private DataTable namesTiers;
private DataTable alts;
private string log;
private bool changed;
private System.Timers.Timer logTimer;
public Dictionary<String, ArrayList> ItemTells { get; set; }
public bool TellsOn { get; set; }
public bool AttendanceOn { get; set; }
public bool LootOn { get; set; }
private frmMain owner;
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
#region Constructor
public LogParser(frmMain owner, string log)
{
logger.Debug("Begin Method: LogParser.LogParser(frmMain,log) (" + owner.ToString() + "," + log.ToString() + ")");
this.owner = owner;
this.log = log;
// TODO: Was going to replace complicated logic with something like this, but will take time to test.
//try
//{
// using (FileStream fs = new FileStream(log, FileMode.OpenOrCreate, FileAccess.Read))
// {
// using (StreamReader sr = new StreamReader(fs)) {
// while (changed)
// {
// while (!sr.EndOfStream)
// Parse(sr.ReadLine())
// while (sr.EndOfStream)
// Thread.Sleep(100)
// }
// }
// }
//}
try
{
logStream = new FileStream(log,FileMode.OpenOrCreate,FileAccess.Read);
logPointer = logSize = logStream.Length;
logStream.Close();
}
catch (Exception ex)
{
logger.Error("Failed to initialize log parser: " + ex.Message);
MessageBox.Show("Error initializing log parser.\n\n" + ex.Message,"Error");
}
try
{
logWatcher = new FileSystemWatcher(Path.GetDirectoryName(log),Path.GetFileName(log));
logWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.Size;
logWatcher.Changed += new System.IO.FileSystemEventHandler(OnChanged);
logWatcher.EnableRaisingEvents = true;
}
catch (Exception ex)
{
logger.Error("Failed to initialize log watcher: " + ex.Message);
MessageBox.Show("Error initializing file system watcher.\n\n" + ex.Message,"Error");
}
TellsOn = false;
AttendanceOn = false;
ItemTells = new Dictionary<string, ArrayList>();
logTimer = new System.Timers.Timer(5000);
logTimer.Start();
logTimer.Elapsed += new System.Timers.ElapsedEventHandler(logTimer_Elapsed);
LoadNamesTiers();
logger.Debug("End Method: LogParser.LogParser()");
}
#endregion
#region Methods
public void LoadNamesTiers()
{
logger.Debug("Begin Method: LogParser.LoadNamesTiers()");
OleDbConnection dbConnect = null;
namesTiers = new DataTable();
try
{
dbConnect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + owner.DBString);
string query = "Select NamesTiers.Name, NamesTiers.Tier, NamesTiers.TPercent, Sum(DKS.PTS) as SumOfPTS From (NamesTiers INNER JOIN DKS ON NamesTiers.Name = DKS.Name) GROUP BY NamesTiers.Name, NamesTiers.Tier, NamesTiers.TPercent";
OleDbDataAdapter dkpDA = new OleDbDataAdapter(query,dbConnect);
dbConnect.Open();
dkpDA.Fill(namesTiers);
}
catch (Exception ex)
{
logger.Error("Failed to load NamesTiers table: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")","Error");
}
finally { dbConnect.Close(); }
alts = new DataTable();
try
{
dbConnect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + owner.DBString);
string query = "SELECT AltName, Field3 FROM Alts";
OleDbDataAdapter altDA = new OleDbDataAdapter(query, dbConnect);
dbConnect.Open();
altDA.Fill(alts);
}
catch (Exception ex)
{
logger.Error("Failed to load Alts table: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
logger.Debug("End Method: LogParser.LoadNamesTiers()");
}
private string getLine(byte[] data, int offset)
{
//logger.Debug("Begin Method: LogParser.getLine()");
int i = 0;
string s = "";
while ( (i+offset)<data.Length && (char)data[i+offset] != '\n')
{
s += (char)data[i+offset];
i++;
}
owner.LineCount++;
//logger.Debug("End Method: LogParser.getLine(), returning " + s);
return s;
}
public void OnChanged(object source, FileSystemEventArgs e)
{
logger.Debug("Begin Method: OnChanged(object,FileSystemEventArgs) ("
+ source.ToString() + "," + e.ToString() + ")");
changed = true;
logger.Debug("End Method: OnChanged()");
}
private bool TellExists(string message, string person)
{
bool tellFound = false;
if (ItemTells.ContainsKey(message))
{
foreach (Raider raider in ItemTells[message])
{
tellFound = person == raider.Person;
}
}
return tellFound;
}
public LogLineType Parse(string s)
{
if (TellsOn)
{
Regex r = new Regex(@"\[.*\] (?<name>\S+) (tells|told) you, '(?<message>.*)'");
Match m = r.Match(s);
string tellType = "N/A";
string[] keywords = { "main", "alt", "guest", "app" };
if (m.Success)
{
logger.Debug(s + " matches tell regex.");
owner.ParseCount++;
string name = m.Groups["name"].ToString();
string message = m.Groups["message"].ToString();
foreach (string keyword in keywords)
{
if (message.ToLower().StartsWith(keyword))
{
tellType = keyword;
message = message.Remove(0, keyword.Length).TrimStart();
}
}
if (!ItemTells.ContainsKey(message))
{
ItemTells[message] = new ArrayList();
}
if (!TellExists(message, name))
{
DataRow[] raider = namesTiers.Select("Name='" + name + "'");
DataRow[] alt = alts.Select("AltName='" + name + "'");
if (alt.Length > 0 && raider.Length <= 0)
{
raider = namesTiers.Select("Name='" + (string)alt[0]["Field3"] + "'");
}
if (raider.Length>0)
{
ItemTells[message].Add(new Raider(owner, (string)raider[0]["Name"], (string)raider[0]["Tier"], (double)raider[0]["SumOfPTS"], (double)Decimal.ToDouble((decimal)raider[0]["TPercent"]), tellType, message));
}
else
{
ItemTells[message].Add(new Raider(owner, name, Raider.NOTIER, Raider.NODKP, Raider.NOATTENDANCE, tellType, message));
}
// This is the tell sorting here
foreach (string key in ItemTells.Keys)
{
ItemTells[key].Sort();
}
}
owner.RefreshTells = true;
return LogLineType.TELL;
}
}
if (AttendanceOn)
{
string zones = owner.Zones;
Regex r = new Regex(@"\[.*\] \[.*\] (?<name>\S+).*<(?<guild>.*)> ZONE: (?<zone>.*)$");
Match m = r.Match(s.Trim());
if (m.Success)
{
string _guild = m.Groups["guild"].ToString();
string _zone = m.Groups["zone"].ToString();
string _name = m.Groups["name"].ToString();
if ( zones.Contains(_zone) && owner.GuildNames.Contains(_guild))
{
logger.Debug(_name + " <" + _guild + "> is in " + _zone + " which is in attendance zone array, adding");
owner.CurrentRaid.InputPerson(_name);
}
owner.ParseCount++;
return LogLineType.WHO;
}
}
return LogLineType.NA;
}
#endregion
#region Events
void logTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
logger.Debug("Begin Method: logTimer_Elapsed(object,ElapsedEventArgs) (" + sender.ToString() + "," + e.ToString() + ")");
if (!changed) return;
changed = false;
if (!(TellsOn || AttendanceOn)) return;
byte[] newData = null;
string line;
int newSize = 0;
int toRead = 0;
FileStream logStream = null;
logSize = new FileInfo(log).Length;
try
{
logStream = new FileStream(log, FileMode.OpenOrCreate, FileAccess.Read);
}
catch (Exception ex)
{
logger.Error("Failed to open log file: " + ex.Message);
}
try
{
if (logStream != null)
{
newData = new byte[logStream.Length - logPointer + 1];
logStream.Seek(logPointer, SeekOrigin.Begin);
logStream.Read(newData, 0, (int)(logStream.Length - logPointer));
toRead = newSize = (int)(logStream.Length - logPointer);
logPointer = logStream.Length;
owner.StatusMessage = "Log changes detected, parsing " + newSize + " bytes.";
owner.PBMin = 0;
owner.PBMax = newSize;
owner.PBVal = 0;
}
}
catch (Exception ex)
{
logger.Error("Failed to load new log data into memory: " + ex.Message);
}
finally
{
if (logStream != null)
{
logStream.Close();
}
}
int tells = 0;
int whos = 0;
int lines = 0;
while (toRead > 0)
{
line = getLine(newData, newSize - toRead);
if (TellsOn || AttendanceOn)
{
switch (Parse(line))
{
case LogLineType.TELL:
tells++;
break;
case LogLineType.WHO:
whos++;
break;
case LogLineType.NA:
break;
}
lines++;
}
toRead -= line.Length+1;
owner.PBVal += line.Length + 1;
}
owner.StatusMessage = "New lines: " + lines + ", " + tells + " tells, " + whos + " who results";
logger.Debug("End Method: logTimer_Elapsed()");
}
#endregion
}
}