-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogParser.java
315 lines (279 loc) · 12 KB
/
LogParser.java
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
// Copyright distributed.net 1997-2002 - All Rights Reserved
// For use in distributed.net projects only.
// Any other distribution or use of this source violates copyright.
//
import java.lang.*;
import java.text.*;
import java.io.*;
import java.util.*;
class LogParser
{
private BufferedReader logfile;
private Vector logdata;
// Past logging formats of the client (older to newer):
// [03/18/98 19:59:39 GMT] Completed block 00002752:E8100000 (2097152 keys)
// 00:00:03.75 - [517825.30 keys/sec]
// ---
// [May 31 23:24:19 GMT] Completed RC5 block 687C9CC2:40000000 (1073741824 keys)
// 0.00:19:29.52 - [918103.14 keys/sec]
// ---
// [Jul 18 03:00:57 GMT] Completed RC5 block 6DE46FD9:00000000 (2147483648 keys)
// [Jul 18 03:00:57 GMT] 0.01:59:18.82 - [299,977.15 keys/sec]
// ---
// [Dec 16 03:25:59 UTC] Completed CSC packet 00205AE7:80000000 (4*2^28 keys)
// 0.00:22:46.15 - [786,534.65 keys/sec]
// ---
// [Jul 21 10:01:55 UTC] Completed OGR stub 24/2-9-13-29-15 (9,743,881,734 nodes)
// 0.00:45:22.11 - [3,579,527.43 nodes/sec]
// ---
// [Nov 09 03:38:02 UTC] RC5: Completed (1.00 stats units)
// 0.00:02:43.43 - [176,486 keys/s]
// ---
// [Dec 24 02:43:22 UTC] RC5-72: Completed CA:40749399:00000000 (1.00 stats units)
// 0.02:49:40.68 - [354,351 keys/s]
public LogParser(BufferedReader br, Vector list)
{
logfile = br;
logdata = list;
}
public void run()
{
GraphEntry ge = new GraphEntry();
String s1 = null, s2 = null;
try {
s1 = logfile.readLine();
while(logfile.ready())
{
s2 = logfile.readLine();
if (ParseLogEntry(s1, s2, ge)) {
logdata.addElement(ge);
ge = new GraphEntry();
}
s1 = s2;
}
}
catch (IOException e) {
System.out.println("LogParser.run(): " + e);
}
}
// Implements a custom decimal string parser for the StringCharacterIterator
// class, that allows our caller to easily continue parsing after the
// last character in the parsed string.
private int ConvertDecimalInteger(StringCharacterIterator sci)
throws ParseException
// sci is modified by reference.
{
int value = 0;
char ch = sci.next();
if (ch < 0x0030 || ch > 0x0039) {
sci.previous();
throw new ParseException("invalid digit " + ch, sci.getIndex());
}
for (;;) {
value = (value * 10) + (ch - 0x0030);
ch = sci.next();
if (ch == CharacterIterator.DONE) break;
else if (ch == ',') ch = sci.next();
if (ch < 0x0030 || ch > 0x0039) { sci.previous(); break; }
}
return value;
}
// Implements a custom decimal string parser for the StringCharacterIterator
// class, that allows our caller to easily continue parsing after the
// last character in the parsed string.
private float ConvertDecimalFloat(StringCharacterIterator sci)
throws ParseException, NumberFormatException
// sci is modified by reference.
{
String str = "";
char ch = sci.next();
if (ch != '.' && (ch < 0x0030 || ch > 0x0039)) {
sci.previous();
throw new ParseException("invalid digit2 " + ch, sci.getIndex());
}
for (;;) {
str += ch;
ch = sci.next();
if (ch == CharacterIterator.DONE) break;
else if (ch == ',') ch = sci.next();
if (ch != '.' && (ch < 0x0030 || ch > 0x0039)) { sci.previous(); break; }
}
return (float) Float.valueOf(str).floatValue();
}
// returns the matching month name. raises exception on error.
private int ConvertMonthName(String month)
throws ParseException
{
if (month.compareTo("Jan") == 0) return 1;
if (month.compareTo("Feb") == 0) return 2;
if (month.compareTo("Mar") == 0) return 3;
if (month.compareTo("Apr") == 0) return 4;
if (month.compareTo("May") == 0) return 5;
if (month.compareTo("Jun") == 0) return 6;
if (month.compareTo("Jul") == 0) return 7;
if (month.compareTo("Aug") == 0) return 8;
if (month.compareTo("Sep") == 0) return 9;
if (month.compareTo("Oct") == 0) return 10;
if (month.compareTo("Nov") == 0) return 11;
if (month.compareTo("Dec") == 0) return 12;
throw new ParseException("invalid month " + month, 0);
}
// returns parsed timestamp. raises exception on error.
private long ParseTimestamp(String stamp)
throws ParseException
{
int tm_mon, tm_mday, tm_year, tm_hour, tm_min, tm_sec;
StringCharacterIterator sci = new StringCharacterIterator(stamp);
if (Character.isDigit(sci.first()))
{
// Parse a timestamp of format "%u/%u/%u %u:%u:%u"
tm_mon = ConvertDecimalInteger(sci);
if (sci.next() != '/') throw new ParseException("bad separator", sci.getIndex());
tm_mday = ConvertDecimalInteger(sci);
if (sci.next() != '/') throw new ParseException("bad separator", sci.getIndex());
tm_year = ConvertDecimalInteger(sci);
if (sci.next() != ' ') throw new ParseException("bad separator", sci.getIndex());
tm_hour = ConvertDecimalInteger(sci);
if (sci.next() != ':') throw new ParseException("bad separator", sci.getIndex());
tm_min = ConvertDecimalInteger(sci);
if (sci.next() != ':') throw new ParseException("bad separator", sci.getIndex());
tm_sec = ConvertDecimalInteger(sci);
}
else
{
// Convert a timestamp of format "%3s %u %u:%u:%u"
tm_mon = ConvertMonthName(stamp.substring(0, 3));
if (sci.setIndex(3) != ' ') throw new ParseException("bad separator1", sci.getIndex());
tm_mday = ConvertDecimalInteger(sci);
if (sci.next() != ' ') throw new ParseException("bad separator2", sci.getIndex());
tm_hour = ConvertDecimalInteger(sci);
if (sci.next() != ':') throw new ParseException("bad separator3", sci.getIndex());
tm_min = ConvertDecimalInteger(sci);
if (sci.next() != ':') throw new ParseException("bad separator4", sci.getIndex());
tm_sec = ConvertDecimalInteger(sci);
tm_year = 1998; // hard coded for now.
}
// correct the date to a full 4-digit year
if (tm_year < 0) throw new ParseException("bad year", sci.getIndex());
else if (tm_year < 70) tm_year += 2000;
else if (tm_year < 100) tm_year += 1900;
// validate all fields
if (tm_mon < 1 || tm_mon > 12 ||
tm_mday < 1 || tm_mday > 31 ||
tm_year < 1970 || tm_year >= 2038 ||
tm_hour < 0 || tm_hour > 23 ||
tm_min < 0 || tm_min > 59 ||
tm_sec < 0 || tm_sec > 59)
throw new ParseException("bad field value", sci.getIndex());
// Convert to seconds past epoc.
// This uses a deprecated API, but it is still useful.
return Date.UTC(tm_year - 1900, tm_mon - 1, tm_mday, tm_hour, tm_min, tm_sec) / 100;
}
// returns parsed duration. throws exception on error.
public float ParseDuration(StringCharacterIterator sci)
throws ParseException
{
int days, hours, mins;
float secs;
days = ConvertDecimalInteger(sci);
switch (sci.next()) {
case ':':
hours = days; days = 0; break;
case '.':
hours = ConvertDecimalInteger(sci);
if (sci.next() != ':')
throw new ParseException("bad duration separator", sci.getIndex());
break;
default:
throw new ParseException("bad duration separator", sci.getIndex());
}
mins = ConvertDecimalInteger(sci);
if (sci.next() != ':')
throw new ParseException("bad duration separator", sci.getIndex());
secs = ConvertDecimalFloat(sci);
return (float) ((24.0 * days + hours) * (float) 3600.0 +
(float) mins * (float) 60.0 + (float) secs);
}
public int ParseProject (StringCharacterIterator sci)
throws ParseException
{
String project = "";
int projectcode;
projectcode = 0;
project += sci.next();
project += sci.next();
project += sci.next();
if (project.compareTo("RC5") == 0) projectcode = 1;
else if (project.compareTo("DES") == 0) projectcode = 2;
else if (project.compareTo("CSC") == 0) projectcode = 3;
else if (project.compareTo("OGR") == 0) projectcode = 4;
else if (project.compareTo("72:") == 0) projectcode = 8;
return (int) projectcode;
}
long lastTimeStamp = 0;
long addValue = 0;
// returns true if successfully parsed.
public boolean ParseLogEntry(String logline1, String logline2, GraphEntry ge)
// ge is modified, by reference.
{
try
{
if (!logline1.startsWith("[")) return false;
int compoffset = logline1.indexOf("Completed ");
if (compoffset < 0) return false;
boolean bContestFirst = (logline1.charAt(compoffset - 2) == ':');
// parse timestamp.
ge.timestamp = ParseTimestamp(logline1.substring(1))+addValue;
if ((ge.timestamp-lastTimeStamp) < -8640000)
{
// 1 Year
addValue += 864000*365;
ge.timestamp += 864000*365;
//System.out.println("Year Roll-Over Time : "+new Date(ge.timestamp*100)+" "+logdata.size());
//System.out.println("line " + logline1.substring(1));
}
//System.out.println("got timestamp " + ge.timestamp);
lastTimeStamp = ge.timestamp;
// parse project.
if (bContestFirst) {
// "RC5: Completed"
ge.project = ParseProject(new StringCharacterIterator(logline1, compoffset-5));
} else {
// "Completed RC5"
ge.project = ParseProject(new StringCharacterIterator(logline1, compoffset+9));
}
//System.out.println("got project " + ge.project);
// parse workunit count (keycount).
int keyoffset = logline1.indexOf('(');
if (keyoffset < 0) return false;
if (bContestFirst) {
float workfloat = ConvertDecimalFloat(new StringCharacterIterator(logline1, keyoffset));
if (ge.project == 1 || ge.project == 2 || ge.project == 3) {
ge.keycount = (long) ((float) 0x10000000 * workfloat);
} else {
ge.keycount = (long) workfloat;
}
//System.out.println("got keycount1 " + ge.keycount);
} else {
ge.keycount = (long) ConvertDecimalInteger(new StringCharacterIterator(logline1, keyoffset));
//System.out.println("got keycount2 " + ge.keycount);
}
// parse the keyrate.
int rateoffset = logline2.lastIndexOf('[');
if (rateoffset < 0) return false;
ge.rate = (float) ConvertDecimalFloat(new StringCharacterIterator(logline2, rateoffset));
//System.out.println("got keyrate " + ge.rate);
// parse duration.
int duroffset = logline2.lastIndexOf(' ', rateoffset - 4);
if (duroffset < 0) return false;
ge.duration = ParseDuration(new StringCharacterIterator(logline2, duroffset));
//System.out.println("got duration " + ge.duration);
// successful parse complete.
return true;
}
catch (Exception E) {
System.out.println("parse exception " + E);
return false;
}
}
}