[53918] | 1 | #!/usr/bin/perl
|
---|
[69490] | 2 | # $Id: vboxlogabstime.pl 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
| 3 | ## @file
|
---|
| 4 | # ???
|
---|
| 5 | #
|
---|
| 6 |
|
---|
| 7 | #
|
---|
[98103] | 8 | # Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[69490] | 9 | #
|
---|
[96407] | 10 | # This file is part of VirtualBox base platform packages, as
|
---|
| 11 | # available from https://www.virtualbox.org.
|
---|
[69490] | 12 | #
|
---|
[96407] | 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 | #
|
---|
[69490] | 28 |
|
---|
[53918] | 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;
|
---|
[69490] | 40 | while (<LOG>)
|
---|
[53918] | 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; }
|
---|
[69490] | 70 | while (<LOG>)
|
---|
[53918] | 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);
|
---|