-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildLogParser.pas
More file actions
430 lines (382 loc) · 11.4 KB
/
Copy pathBuildLogParser.pas
File metadata and controls
430 lines (382 loc) · 11.4 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
unit BuildLogParser;
{
Parst Delphi-Compiler-Ausgaben (dcc32/dcc64) aus dem Build-Log
und liefert eine formatierte Statistik.
Unterstuetzte Zeilenformate:
[dcc64 Warnung] datei.pas(42): W1000 Symbol '...' ist veraltet
[dcc64 Hinweis] datei.pas(42): H2443 Inline-Funktion ...
[dcc64 Fehler] datei.pas(42): E2003 Undeklarierter Bezeichner
Erzeugen von rhd.dproj (Debug, Win64)
Vergangene Zeit 00:00:17.43
605 Warnung(en)
0 Fehler
}
interface
uses
System.SysUtils, System.Classes,
System.Generics.Collections, System.Generics.Defaults,
System.RegularExpressions, System.StrUtils, System.Math;
type
TMessageKind = (mkFehler, mkWarnung, mkHinweis);
TBuildMessage = record
Kind: TMessageKind;
FileName: string;
Line: Integer;
Code: string;
Text: string;
RawLine: string;
end;
TBuildStats = class
private
FMessages: TList<TBuildMessage>;
FProjectName: string;
FConfig: string;
FPlatform: string;
FBuildTime: string;
FSuccess: Boolean;
FTotalErrors: Integer;
FTotalWarnings: Integer;
FTotalHints: Integer;
procedure ParseLine(const ALine: string);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure ParseFile(const AFileName: string);
procedure ParseLines(ALines: TStrings);
function FormatReport: string;
property ProjectName: string read FProjectName;
property Config: string read FConfig;
property Platform: string read FPlatform;
property BuildTime: string read FBuildTime;
property Success: Boolean read FSuccess;
property TotalErrors: Integer read FTotalErrors;
property TotalWarnings: Integer read FTotalWarnings;
property TotalHints: Integer read FTotalHints;
property Messages: TList<TBuildMessage> read FMessages;
end;
implementation
{ --------------------------------------------------------------------------- }
constructor TBuildStats.Create;
begin
inherited;
FMessages := TList<TBuildMessage>.Create;
FSuccess := True;
end;
destructor TBuildStats.Destroy;
begin
FMessages.Free;
inherited;
end;
procedure TBuildStats.Clear;
begin
FMessages.Clear;
FProjectName := '';
FConfig := '';
FPlatform := '';
FBuildTime := '';
FSuccess := True;
FTotalErrors := 0;
FTotalWarnings := 0;
FTotalHints := 0;
end;
{ --------------------------------------------------------------------------- }
procedure TBuildStats.ParseLine(const ALine: string);
const
RX_DCC = '^\s*\[dcc\d+\s+(Warnung|Hinweis|Fehler|Fatal)\]\s+(.+?)\((\d+)\):\s+([WHEF]\d+)\s+(.*)$';
RX_PROJ = '^\s*Erzeugen von\s+(.+?\.dproj)\s+\(([^,]+),\s*([^)]+)\)';
RX_TIME = '^\s*Vergangene Zeit\s+(\d+:\d+:\d+\.\d+)';
RX_WARN = '^\s*(\d+)\s+Warnung\(en\)';
RX_ERR = '^\s*(\d+)\s+Fehler\s*$';
var
M: TMatch;
Msg: TBuildMessage;
begin
M := TRegEx.Match(ALine, RX_DCC, [roIgnoreCase]);
if M.Success then
begin
Msg.Code := M.Groups[4].Value;
Msg.FileName := ExtractFileName(M.Groups[2].Value);
Msg.Line := StrToIntDef(M.Groups[3].Value, 0);
Msg.Text := M.Groups[5].Value;
Msg.RawLine := Trim(ALine);
if SameText(M.Groups[1].Value, 'Warnung') then
Msg.Kind := mkWarnung
else if SameText(M.Groups[1].Value, 'Hinweis') then
Msg.Kind := mkHinweis
else
begin
Msg.Kind := mkFehler;
FSuccess := False;
end;
FMessages.Add(Msg);
Exit;
end;
M := TRegEx.Match(ALine, RX_PROJ);
if M.Success then
begin
FProjectName := M.Groups[1].Value;
FConfig := Trim(M.Groups[2].Value);
FPlatform := Trim(M.Groups[3].Value);
Exit;
end;
M := TRegEx.Match(ALine, RX_TIME);
if M.Success then
begin
FBuildTime := M.Groups[1].Value;
Exit;
end;
M := TRegEx.Match(ALine, RX_WARN);
if M.Success then
begin
FTotalWarnings := StrToIntDef(M.Groups[1].Value, FTotalWarnings);
Exit;
end;
M := TRegEx.Match(ALine, RX_ERR);
if M.Success then
begin
FTotalErrors := StrToIntDef(M.Groups[1].Value, FTotalErrors);
if FTotalErrors > 0 then FSuccess := False;
end;
end;
{ --------------------------------------------------------------------------- }
procedure TBuildStats.ParseFile(const AFileName: string);
var
Lines: TStringList;
begin
Clear;
Lines := TStringList.Create;
try
{ UTF-8 bevorzugen (IDE-/MSBuild-Logs), bei Dekodierfehlern auf das
automatisch erkannte Encoding (BOM/ANSI) zurueckfallen. }
try
Lines.LoadFromFile(AFileName, TEncoding.UTF8);
except
Lines.LoadFromFile(AFileName);
end;
ParseLines(Lines);
finally
Lines.Free;
end;
end;
procedure TBuildStats.ParseLines(ALines: TStrings);
var
I: Integer;
Msg: TBuildMessage;
begin
for I := 0 to ALines.Count - 1 do
ParseLine(ALines[I]);
{ Hinweise immer selbst zaehlen (kein eigener Footer-Eintrag) }
FTotalHints := 0;
for I := 0 to FMessages.Count - 1 do
begin
Msg := FMessages[I];
if Msg.Kind = mkHinweis then
Inc(FTotalHints);
end;
{ Falls keine Footer-Zusammenfassung gefunden, alles selbst zaehlen }
if (FTotalWarnings = 0) and (FTotalErrors = 0) then
begin
for I := 0 to FMessages.Count - 1 do
begin
Msg := FMessages[I];
case Msg.Kind of
mkWarnung: Inc(FTotalWarnings);
mkFehler: Inc(FTotalErrors);
end;
end;
end
else
{ Footer-Wert = Warnungen + Hinweise zusammen; Warnungen = Gesamt - Hinweise }
FTotalWarnings := FTotalWarnings - FTotalHints;
end;
{ --------------------------------------------------------------------------- }
type
TCountPair = TPair<string, Integer>;
function ComparePairsDesc(const A, B: TCountPair): Integer;
begin
Result := B.Value - A.Value;
end;
function TBuildStats.FormatReport: string;
const
LINE = '---------------------------------------------';
TOP_N = 10;
function PadRight(const S: string; W: Integer): string;
begin
Result := S + StringOfChar(' ', Max(0, W - Length(S)));
end;
procedure SortedPairs(Dict: TDictionary<string, Integer>;
out Pairs: TArray<TCountPair>);
begin
Pairs := Dict.ToArray;
TArray.Sort<TCountPair>(Pairs,
TComparer<TCountPair>.Construct(ComparePairsDesc));
end;
procedure AppendTop(SL: TStringList; Dict: TDictionary<string, Integer>;
DescDict: TDictionary<string, string>; FileW: Integer);
var
Pairs: TArray<TCountPair>;
I: Integer;
Desc: string;
begin
SortedPairs(Dict, Pairs);
for I := 0 to Min(TOP_N - 1, High(Pairs)) do
begin
Desc := '';
if Assigned(DescDict) then
begin
if not DescDict.TryGetValue(Pairs[I].Key, Desc) then Desc := '';
if Length(Desc) > 38 then Desc := Copy(Desc, 1, 38) + '...';
end;
if FileW > 0 then
SL.Add(Format(' %-40s %4dx', [PadRight(Pairs[I].Key, 40), Pairs[I].Value]))
else
SL.Add(Format(' %s %4dx %s', [PadRight(Pairs[I].Key, 6), Pairs[I].Value, Desc]));
end;
end;
var
SL: TStringList;
CodeCount: TDictionary<string, Integer>;
CodeDesc: TDictionary<string, string>;
FileWarn: TDictionary<string, Integer>;
FileHint: TDictionary<string, Integer>;
SymCount: TDictionary<string, Integer>;
I: Integer;
Msg: TBuildMessage;
TimeStr: string;
Parts: TArray<string>;
SP: TArray<string>;
H, Min_, S_, Ms: Integer;
Secs: Double;
P1, P2: Integer;
Sym: string;
begin
SL := TStringList.Create;
CodeCount := TDictionary<string, Integer>.Create;
CodeDesc := TDictionary<string, string>.Create;
FileWarn := TDictionary<string, Integer>.Create;
FileHint := TDictionary<string, Integer>.Create;
SymCount := TDictionary<string, Integer>.Create;
try
for I := 0 to FMessages.Count - 1 do
begin
Msg := FMessages[I];
{ Code-Zaehler }
if CodeCount.ContainsKey(Msg.Code) then
CodeCount[Msg.Code] := CodeCount[Msg.Code] + 1
else
begin
CodeCount.Add(Msg.Code, 1);
CodeDesc.Add(Msg.Code, Msg.Text);
end;
{ Datei-Zaehler }
if Msg.Kind = mkWarnung then
begin
if FileWarn.ContainsKey(Msg.FileName) then
FileWarn[Msg.FileName] := FileWarn[Msg.FileName] + 1
else
FileWarn.Add(Msg.FileName, 1);
end
else if Msg.Kind = mkHinweis then
begin
if FileHint.ContainsKey(Msg.FileName) then
FileHint[Msg.FileName] := FileHint[Msg.FileName] + 1
else
FileHint.Add(Msg.FileName, 1);
end;
{ Veraltete Symbole aus W1000-Zeilen extrahieren }
if Msg.Code = 'W1000' then
begin
P1 := Pos('''', Msg.Text);
if P1 > 0 then
begin
Inc(P1);
P2 := Pos('''', Msg.Text, P1);
if P2 > P1 then
begin
Sym := Copy(Msg.Text, P1, P2 - P1);
if SymCount.ContainsKey(Sym) then
SymCount[Sym] := SymCount[Sym] + 1
else
SymCount.Add(Sym, 1);
end;
end;
end;
end;
{ --- Kopfzeile --- }
SL.Add('BUILD LOG STATISTIK');
SL.Add('===========================================');
SL.Add('');
SL.Add('Projekt: ' + IfThen(FProjectName <> '', FProjectName, '(unbekannt)'));
SL.Add('Konfiguration: ' + IfThen(FConfig <> '', FConfig, '-'));
SL.Add('Plattform: ' + IfThen(FPlatform <> '', FPlatform, '-'));
TimeStr := FBuildTime;
if TimeStr <> '' then
begin
Parts := TimeStr.Split([':']);
if Length(Parts) = 3 then
begin
H := StrToIntDef(Parts[0], 0);
Min_ := StrToIntDef(Parts[1], 0);
SP := Parts[2].Split(['.']);
if Length(SP) >= 2 then
begin
S_ := StrToIntDef(SP[0], 0);
Ms := StrToIntDef(SP[1], 0);
Secs := H * 3600 + Min_ * 60 + S_ + Ms / 100.0;
TimeStr := Format('%.2f s (%s)', [Secs, FBuildTime]);
end;
end;
end;
SL.Add('Bauzeit: ' + IfThen(TimeStr <> '', TimeStr, '-'));
SL.Add('Ergebnis: ' + IfThen(FSuccess, 'ERFOLG', 'FEHLER'));
{ --- Meldungszahlen --- }
SL.Add('');
SL.Add('MELDUNGEN');
SL.Add(LINE);
SL.Add(Format(' Fehler: %d', [FTotalErrors]));
SL.Add(Format(' Warnungen: %d', [FTotalWarnings]));
SL.Add(Format(' Hinweise: %d', [FTotalHints]));
SL.Add(Format(' Gesamt: %d', [FTotalErrors + FTotalWarnings + FTotalHints]));
{ --- Top Codes --- }
SL.Add('');
SL.Add('HAEUFIGSTE CODES');
SL.Add(LINE);
AppendTop(SL, CodeCount, CodeDesc, 0);
{ --- Veraltete Symbole --- }
if SymCount.Count > 0 then
begin
SL.Add('');
SL.Add('VERALTETE SYMBOLE (W1000)');
SL.Add(LINE);
AppendTop(SL, SymCount, nil, 0);
end;
{ --- Top Dateien Warnungen --- }
if FileWarn.Count > 0 then
begin
SL.Add('');
SL.Add('DATEIEN MIT MEISTEN WARNUNGEN');
SL.Add(LINE);
AppendTop(SL, FileWarn, nil, 1);
end;
{ --- Top Dateien Hinweise --- }
if FileHint.Count > 0 then
begin
SL.Add('');
SL.Add('DATEIEN MIT MEISTEN HINWEISEN');
SL.Add(LINE);
AppendTop(SL, FileHint, nil, 1);
end;
SL.Add('');
SL.Add('===========================================');
Result := SL.Text;
finally
SL.Free;
CodeCount.Free;
CodeDesc.Free;
FileWarn.Free;
FileHint.Free;
SymCount.Free;
end;
end;
end.