|
1 #!perl |
|
2 # cloggercolour.pl |
|
3 # |
|
4 # Copyright (c) 2006 - 2010 Accenture. All rights reserved. |
|
5 # This component and the accompanying materials are made available |
|
6 # under the terms of the "Eclipse Public License v1.0" |
|
7 # which accompanies this distribution, and is available |
|
8 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 # |
|
10 # Initial Contributors: |
|
11 # Accenture - Initial contribution |
|
12 # |
|
13 |
|
14 use strict; |
|
15 use Getopt::Long; |
|
16 use IO::File; |
|
17 use IO::Handle; |
|
18 |
|
19 sub ParseCommandLineArgs(); |
|
20 sub GetNextColour(); |
|
21 |
|
22 my $clogFile; |
|
23 my $outFile; |
|
24 my $plainText; |
|
25 my $colour = 0; |
|
26 my $deltaTimeStamps = 0; |
|
27 my $verbose; |
|
28 |
|
29 ParseCommandLineArgs(); |
|
30 |
|
31 my $logfile; |
|
32 my $htmlfile; |
|
33 |
|
34 if (defined $clogFile) { |
|
35 print "Opening $clogFile for reading\n" if ($verbose); |
|
36 $logfile = new IO::File; |
|
37 $logfile->open("<$clogFile") or die "ERROR: Can't open $clogFile for reading: $!\n"; |
|
38 } else { |
|
39 print "Reading from STDIN\n" if ($verbose); |
|
40 #$logfile = new IO::Handle; |
|
41 #$logfile->fdopen(fileno(STDIN), "r") or die "ERROR: can't open STDIN: $!\n"; |
|
42 $logfile = *STDIN; |
|
43 } |
|
44 if (defined $outFile) { |
|
45 print "Opening $outFile for writing\n" if ($verbose); |
|
46 $htmlfile = new IO::File; |
|
47 $htmlfile->open(">$outFile") or die "ERROR: Can't open $outFile for writing: $!\n"; |
|
48 } else { |
|
49 print "Writing to STDOUT\n" if ($verbose); |
|
50 $htmlfile = *STDOUT; |
|
51 } |
|
52 unless ($plainText) { |
|
53 print $htmlfile "<html><body><pre>\n"; |
|
54 } |
|
55 |
|
56 my %tagColours = {}; |
|
57 my $colours = 0; |
|
58 my $lastMillis = -1; |
|
59 |
|
60 while (my $line = <$logfile>) { |
|
61 chomp $line; |
|
62 if ($line =~ m/^(\d{4}-\d{2}-\d{2} \d{2}\:\d{2}\:\d{2}\.\d{3})\: \[([^]]+)\] (.*)$/ ) { |
|
63 my ($time, $tag, $text) = ($1, $2, $3); |
|
64 my $deltaStr = ""; |
|
65 unless (exists $tagColours{$tag}) { |
|
66 $tagColours{$tag} = GetNextColour(); |
|
67 } |
|
68 if ($deltaTimeStamps) { |
|
69 $time =~ m|(\d{4})-(\d{2})-(\d{2}) (\d{2})\:(\d{2})\:(\d{2})\.(\d{3})|; |
|
70 my ($year, $month, $day, $hour, $minute, $second, $millisecond) = ($1, $2, $3, $4, $5, $6, $7); |
|
71 my $millis = ((((((((((($year*12)+$month)*31)+$day)*24)+$hour)*60)+$minute)*60)+$second)*1000)+$millisecond; |
|
72 my $delta = 0; |
|
73 if ($lastMillis != -1) { |
|
74 $delta = $millis - $lastMillis; |
|
75 } |
|
76 $lastMillis = $millis; |
|
77 $deltaStr = sprintf("(+% 4d)", $delta); |
|
78 } |
|
79 |
|
80 print $htmlfile "<font color=$tagColours{$tag}>" unless ($plainText) ; |
|
81 print $htmlfile "$time:$deltaStr [$tag] $text"; |
|
82 print $htmlfile "</font>" unless ($plainText); |
|
83 print $htmlfile "\n"; |
|
84 } |
|
85 else { |
|
86 print STDERR "WARNING: could not parse line $. of $clogFile\n"; |
|
87 } |
|
88 } |
|
89 |
|
90 |
|
91 unless ($plainText) { |
|
92 print $htmlfile "</pre></body></html>\n" |
|
93 } |
|
94 undef $logfile; |
|
95 undef $htmlfile; |
|
96 |
|
97 sub ParseCommandLineArgs() { |
|
98 Getopt::Long::Configure ("bundling"); |
|
99 GetOptions('v+' => \$verbose, 'c' => \$colour, 'd' => \$deltaTimeStamps, 'p'=>\$plainText); |
|
100 if ($#ARGV != -1) { |
|
101 $clogFile = shift @ARGV; |
|
102 } |
|
103 if ($#ARGV != -1) { |
|
104 $outFile = shift @ARGV; |
|
105 } else { |
|
106 if (defined $clogFile) { |
|
107 $outFile = "$clogFile.html"; |
|
108 } |
|
109 } |
|
110 } |
|
111 |
|
112 my $colours = 0; |
|
113 |
|
114 sub GetNextColour() { |
|
115 if ($colour) { |
|
116 # make a new colour |
|
117 my $pattern = $colours % 6; |
|
118 my $newColour; |
|
119 ++$pattern; |
|
120 if ($pattern > 3) { |
|
121 $newColour = "#FF"; |
|
122 $pattern-=4; |
|
123 } else { |
|
124 $newColour = "#00"; |
|
125 } |
|
126 if ($pattern > 1) { |
|
127 $newColour = "${newColour}80"; |
|
128 $pattern-=2; |
|
129 } else { |
|
130 $newColour = "${newColour}00"; |
|
131 } |
|
132 if ($pattern > 0) { |
|
133 $newColour = "${newColour}FF"; |
|
134 } else { |
|
135 $newColour = "${newColour}00"; |
|
136 } |
|
137 ++$colours; |
|
138 return $newColour; |
|
139 } else { |
|
140 return "#000000"; |
|
141 } |
|
142 } |
|
143 |
|
144 __END__ |
|
145 |
|
146 =head1 NAME |
|
147 |
|
148 cloggerproc - Post-process clogger logs to add colour or timestamps |
|
149 |
|
150 =head1 SYNOPSIS |
|
151 |
|
152 cloggerproc [options] [infile [outfile]] |
|
153 |
|
154 options: |
|
155 |
|
156 =over 4 |
|
157 |
|
158 =item -d |
|
159 |
|
160 Add a millisecond delta to timestamps |
|
161 |
|
162 =item -c |
|
163 |
|
164 Colour logging lines based on the tag |
|
165 |
|
166 =item -v |
|
167 |
|
168 verbose output |
|
169 |
|
170 =item -h |
|
171 |
|
172 Show this help |
|
173 |
|
174 =back |
|
175 |
|
176 =head1 DESCRIPTION |
|
177 |
|
178 |
|
179 =head1 KNOWN BUGS |
|
180 |
|
181 If the timestamp month or year rolls over during the log, the delta will be incorrect for the first logging line in the new month/year. |
|
182 |
|
183 =head1 COPYRIGHT |
|
184 |
|
185 Copyright (c) 2008-2010 Accenture. All rights reserved. |
|
186 |
|
187 =cut |