1 | #!/usr/bin/perl
|
---|
2 | # $Id: vboxlogabstime.pl 106061 2024-09-16 14:03:52Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # ???
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
9 | #
|
---|
10 | # This file is part of VirtualBox base platform packages, as
|
---|
11 | # available from https://www.virtualbox.org.
|
---|
12 | #
|
---|
13 | # This program is free software; you can redistribute it and/or
|
---|
14 | # modify it under the terms of the GNU General Public License
|
---|
15 | # as published by the Free Software Foundation, in version 3 of the
|
---|
16 | # License.
|
---|
17 | #
|
---|
18 | # This program is distributed in the hope that it will be useful, but
|
---|
19 | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
21 | # General Public License for more details.
|
---|
22 | #
|
---|
23 | # You should have received a copy of the GNU General Public License
|
---|
24 | # along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
25 | #
|
---|
26 | # SPDX-License-Identifier: GPL-3.0-only
|
---|
27 | #
|
---|
28 |
|
---|
29 | use strict;
|
---|
30 | use warnings;
|
---|
31 | use Time::Local;
|
---|
32 |
|
---|
33 | if ($#ARGV != 0) { die "Give the VirtualBox log file in the command line\n"; }
|
---|
34 | open(LOG, $ARGV[0]) or die "Unable to open $ARGV[0] ($!)\n";
|
---|
35 |
|
---|
36 | # extract log timestamp from VBox.log
|
---|
37 | my $line = 0;
|
---|
38 | my ($dummy, $start);
|
---|
39 | my $continuation = 0;
|
---|
40 | while (<LOG>)
|
---|
41 | {
|
---|
42 | chomp;
|
---|
43 | $line++;
|
---|
44 | next if not /^.*Log opened|started.*/;
|
---|
45 | if ($line ge 3) { die "Cannot find timestamp in $ARGV[0]\n"; }
|
---|
46 | ($dummy,$start)=split(/.*?Log opened|started /);
|
---|
47 | $continuation = 1 if /^.*Log started.*/;
|
---|
48 | last;
|
---|
49 | }
|
---|
50 |
|
---|
51 | # compute perl time value corresponding to timestamp
|
---|
52 | my ($year,$month,$day,$hh,$mm,$ss,$frac);
|
---|
53 | if ($start =~ s/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d+)Z/ /) {
|
---|
54 | ($year,$month,$day,$hh,$mm,$ss,$frac) = ($1,$2-1,$3,$4,$5,$6,$7);
|
---|
55 | $frac = "0.$frac";
|
---|
56 | }
|
---|
57 | else
|
---|
58 | {
|
---|
59 | die "Timestamp $start cannot be parsed\n";
|
---|
60 | }
|
---|
61 | my $logstamp = timegm($ss,$mm,$hh,$day,$month,$year)+$frac;
|
---|
62 |
|
---|
63 | # print entire log with absolute timestamps in local time
|
---|
64 | seek(LOG, 0, 0);
|
---|
65 | my $firstrel;
|
---|
66 | # Note that for continuations we're slightly inaccurate, as we have no idea
|
---|
67 | # about the time difference between the start of the process and the start of
|
---|
68 | # logging as documented by the timestamp. Usually a couple milliseconds.
|
---|
69 | if ($continuation) { $firstrel = 0; }
|
---|
70 | while (<LOG>)
|
---|
71 | {
|
---|
72 | my ($time,$msg) = split('(?<=\s)(.*)');
|
---|
73 | my ($h,$m,$s,$ms) = split(':|\.', $time);
|
---|
74 | my $reltime = $h * 3600 + $m * 60 + $s + "0.$ms";
|
---|
75 | if (!defined $firstrel) { $firstrel = $reltime; }
|
---|
76 | $reltime -= $firstrel;
|
---|
77 | my $abstime = $logstamp + $reltime;
|
---|
78 | $ms = int(($abstime - int($abstime)) * 1000);
|
---|
79 | # msec rounding paranoia
|
---|
80 | if ($ms gt 999) { $ms = 999 };
|
---|
81 | $abstime = int($abstime);
|
---|
82 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($abstime);
|
---|
83 | printf("%04d-%02d-%02d %02d:%02d:%02d.%03d %s\n", $year + 1900, $mon + 1, $mday, $hour, $min, $sec, $ms, $msg);
|
---|
84 | }
|
---|
85 | close(LOG);
|
---|