1 | #!/usr/bin/perl
|
---|
2 | use strict;
|
---|
3 | use warnings;
|
---|
4 | use Time::Local;
|
---|
5 |
|
---|
6 | if ($#ARGV != 0) { die "Give the VirtualBox log file in the command line\n"; }
|
---|
7 | open(LOG, $ARGV[0]) or die "Unable to open $ARGV[0] ($!)\n";
|
---|
8 |
|
---|
9 | # extract log timestamp from VBox.log
|
---|
10 | my $line = 0;
|
---|
11 | my ($dummy, $start);
|
---|
12 | my $continuation = 0;
|
---|
13 | while (<LOG>)
|
---|
14 | {
|
---|
15 | chomp;
|
---|
16 | $line++;
|
---|
17 | next if not /^.*Log opened|started.*/;
|
---|
18 | if ($line ge 3) { die "Cannot find timestamp in $ARGV[0]\n"; }
|
---|
19 | ($dummy,$start)=split(/.*?Log opened|started /);
|
---|
20 | $continuation = 1 if /^.*Log started.*/;
|
---|
21 | last;
|
---|
22 | }
|
---|
23 |
|
---|
24 | # compute perl time value corresponding to timestamp
|
---|
25 | my ($year,$month,$day,$hh,$mm,$ss,$frac);
|
---|
26 | if ($start =~ s/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d+)Z/ /) {
|
---|
27 | ($year,$month,$day,$hh,$mm,$ss,$frac) = ($1,$2-1,$3,$4,$5,$6,$7);
|
---|
28 | $frac = "0.$frac";
|
---|
29 | }
|
---|
30 | else
|
---|
31 | {
|
---|
32 | die "Timestamp $start cannot be parsed\n";
|
---|
33 | }
|
---|
34 | my $logstamp = timegm($ss,$mm,$hh,$day,$month,$year)+$frac;
|
---|
35 |
|
---|
36 | # print entire log with absolute timestamps in local time
|
---|
37 | seek(LOG, 0, 0);
|
---|
38 | my $firstrel;
|
---|
39 | # Note that for continuations we're slightly inaccurate, as we have no idea
|
---|
40 | # about the time difference between the start of the process and the start of
|
---|
41 | # logging as documented by the timestamp. Usually a couple milliseconds.
|
---|
42 | if ($continuation) { $firstrel = 0; }
|
---|
43 | while (<LOG>)
|
---|
44 | {
|
---|
45 | my ($time,$msg) = split('(?<=\s)(.*)');
|
---|
46 | my ($h,$m,$s,$ms) = split(':|\.', $time);
|
---|
47 | my $reltime = $h * 3600 + $m * 60 + $s + "0.$ms";
|
---|
48 | if (!defined $firstrel) { $firstrel = $reltime; }
|
---|
49 | $reltime -= $firstrel;
|
---|
50 | my $abstime = $logstamp + $reltime;
|
---|
51 | $ms = int(($abstime - int($abstime)) * 1000);
|
---|
52 | # msec rounding paranoia
|
---|
53 | if ($ms gt 999) { $ms = 999 };
|
---|
54 | $abstime = int($abstime);
|
---|
55 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($abstime);
|
---|
56 | printf("%04d-%02d-%02d %02d:%02d:%02d.%03d %s\n", $year + 1900, $mon + 1, $mday, $hour, $min, $sec, $ms, $msg);
|
---|
57 | }
|
---|
58 | close(LOG);
|
---|