common/tools/raptor/preprocess_log.pl
changeset 228 71d24b4fa162
parent 180 1d9c60a4e308
equal deleted inserted replaced
227:51e429810aba 228:71d24b4fa162
       
     1 #!perl -w
     1 # Copyright (c) 2009 Symbian Foundation Ltd
     2 # Copyright (c) 2009 Symbian Foundation Ltd
     2 # This component and the accompanying materials are made available
     3 # This component and the accompanying materials are made available
     3 # under the terms of the License "Eclipse Public License v1.0"
     4 # under the terms of the License "Eclipse Public License v1.0"
     4 # which accompanies this distribution, and is available
     5 # which accompanies this distribution, and is available
     5 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
    14 
    15 
    15 use strict;
    16 use strict;
    16 
    17 
    17 use Getopt::Long;
    18 use Getopt::Long;
    18 
    19 
    19 my $infile = '';
       
    20 my $outfile = '';
       
    21 my $basedir = '';
       
    22 my $help = 0;
    20 my $help = 0;
    23 GetOptions((
    21 GetOptions(
    24 	'in:s' => \$infile,
    22 	'help!' => \$help,
    25 	'out:s' => \$outfile,
    23 );
    26 	'help!' => \$help
       
    27 ));
       
    28 
       
    29 $help = 1 if (!$infile);
       
    30 
    24 
    31 if ($help)
    25 if ($help)
    32 {
    26 {
    33 	print "Preprocess a raptor log, trying to countermeasure a list of known anomalies\n";
    27 	warn <<"EOF";
    34 	print "Usage: perl preprocess_log.pl --in=INFILE --out=OUTFILE\n";
    28 Preprocess a raptor log, trying to countermeasure a list of known anomalies
       
    29 
       
    30 Usage: perl preprocess_log.pl < INFILE > OUTFILE
       
    31 EOF
    35 	exit(0);
    32 	exit(0);
    36 }
    33 }
    37 
    34 
    38 open(INFILE, $infile);
    35 while (my $line = <>)
    39 open(OUTFILE, ">$outfile");
       
    40 
       
    41 for my $line (<INFILE>)
       
    42 {
    36 {
    43 	if ($line =~ m,<[^<^>]+>.*&.*</[^<^>]+>,)
    37 	if ($line =~ m{<[^<^>]+>.*&.*</[^<^>]+>})
    44 	{
    38 	{
    45 		$line = escape_ampersand($line);
    39 		$line = escape_ampersand($line);
    46 	}
    40 	}
    47 	elsif ($line =~ m,<\?xml\s.*encoding=.*\".*\?>,)
    41 	elsif ($line =~ m{<\?xml\s.*encoding=.*\".*\?>})
    48 	{
    42 	{
    49 		$line = set_encoding_utf8($line);
    43 		$line = set_encoding_utf8($line);
    50 	}
    44 	}
       
    45 	elsif ($line =~ m{<archive.*?[^/]>})
       
    46 	{
       
    47 		$line = unterminated_archive_tag($line, scalar <>, $.)
       
    48 	}
    51 	
    49 	
    52 	print OUTFILE $line;
    50 	print $line;
    53 }
    51 }
    54 
       
    55 close(OUTFILE);
       
    56 close(INFILE);
       
    57 
       
    58 
    52 
    59 sub escape_ampersand
    53 sub escape_ampersand
    60 {
    54 {
    61 	my ($line) = @_;
    55 	my ($line) = @_;
    62 	
    56 	
    63 	print "escape_ampersand\n";
    57 	warn "escape_ampersand\n";
    64 	print "in: $line";
    58 	warn "in: $line";
    65 	
    59 	
    66 	$line =~ s,&,&amp;,g;
    60 	$line =~ s,&,&amp;,g;
    67 	
    61 	
    68 	print "out: $line";
    62 	warn "out: $line";
    69 	return $line;
    63 	return $line;
    70 }
    64 }
    71 
    65 
    72 sub set_encoding_utf8
    66 sub set_encoding_utf8
    73 {
    67 {
    74 	my ($line) = @_;
    68 	my ($line) = @_;
    75 	
    69 	
    76 	print "set_encoding_utf8\n";
    70 	warn "set_encoding_utf8\n";
    77 	print "in: $line";
    71 	warn "in: $line";
    78 	
    72 	
    79 	$line =~ s,encoding=".*",encoding="utf-8",;
    73 	$line =~ s,encoding=".*",encoding="utf-8",;
    80 	
    74 	
    81 	print "out: $line";
    75 	warn "out: $line";
    82 	return $line;
    76 	return $line;
    83 }
    77 }
       
    78 
       
    79 sub unterminated_archive_tag
       
    80 {
       
    81 	my $line = shift;
       
    82 	my $nextLine = shift;
       
    83 	my $lineNum = shift;
       
    84 	
       
    85 	if ($nextLine !~ m{(<member>)|(</archive>)})
       
    86 	{
       
    87 		warn "unterminated_archive_tag\n";
       
    88 		warn "in: $line";
       
    89 		$line =~ s{>}{/>};
       
    90 		warn "out: $line";
       
    91 	}
       
    92 	
       
    93 	return $line . $nextLine;
       
    94 }