#!/usr/bin/perl -w

use Data::Dumper;

my $file = $ARGV[0] || "";

if ($file eq "") {
    print "Missing file to parse\n";
    exit 1;
} else {
    print "Parsing file '".$file."'\n";
}

################################################################################

my $output_data = {};

my $log_level = 0;
my $log_component = "";
my $source_line = "";
my $entry_size = 0;

################################################################################

sub ParseFileLine ($) {
    my $ref_line = shift;

    if ($$ref_line !~ /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} /) {
	return;
    }

    $entry_size = length ($$ref_line);

    $$ref_line =~ s/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} <(\d)> [^ ]+ \[([^]]+)\] ([^ ]+)//;
    $log_level = $1 || 0;
    $log_component = $2 || "";
    $source_line = $3 || do {
	warn "No source line in ".$$ref_line."\n";
    };

    $source_line =~ s/:\d+$//;
    # zypp functions
    $source_line =~ s/\(.*//;

    # print $log_level."|".$log_component."|".$source_line." (".$entry_size.")\n";

    if (! defined $data->{$log_component}->{$source_line}->{$log_level}->{'count'}) {
	$data->{$log_component}->{$source_line}->{$log_level}->{'count'} = 0;
    }
    if (! defined $data->{$log_component}->{$source_line}->{$log_level}->{'size'}) {
	$data->{$log_component}->{$source_line}->{$log_level}->{'size'} = 0;
    }

    $data->{$log_component}->{$source_line}->{$log_level}->{'count'} += 1;
    $data->{$log_component}->{$source_line}->{$log_level}->{'size'} += $entry_size;
}

################################################################################

open (FILE, $file) || do {
    print "Cannot open file '".$file."': ".$!."\n";
    exit 2;
};

foreach my $line (<FILE>) {
    chop $line;
    ParseFileLine(\$line);
}

close (FILE);

print Dumper ($data);

exit 0;

################################################################################