From: Jan "Yenya" Kasprzak Date: Fri, 11 Feb 2011 17:01:56 +0000 (+0100) Subject: Log file replayer. X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=3f750ba20e8f4d09cb01a08b937da1563e8d1bf1;p=slotcarman.git Log file replayer. --- diff --git a/replay_log b/replay_log new file mode 100755 index 0000000..c10ecfe --- /dev/null +++ b/replay_log @@ -0,0 +1,52 @@ +#!/usr/bin/perl -w + +use strict; + +use Getopt::Std; +use FileHandle; +use Time::HiRes qw(time usleep); + +my %opt; +if (!getopts('b:e:fp', \%opt) || !$ARGV[0]) { + die "Usage: $0 [-b starttime] [-e endtime] [-f] [-p] logfile\n" + . "-f ... replay as fast as possible, without pausing\n" + . "-p ... at the end, want for a keypress\n"; +} + +my $logfile = shift @ARGV; + +open my $logfh, '<', $logfile + or die "Can't open $logfile: $!"; + +my $start_prog_time = time; +my $start_log_time; + +while (my $line = <$logfh>) { + chomp $line; + $line =~ s/\A\s+//xms; + my ($l_time, @data) = split(/\s+/, $line); + + next if ($opt{b} && $opt{b} > $l_time); + next if ($opt{e} && $opt{e} < $l_time); + + print pack("C*", map { hex $_ } @data); + flush STDOUT; + + if (!$opt{f}) { + $start_log_time ||= $l_time; + + my $log_delay = $l_time - $start_log_time; + my $real_delay = time - $start_prog_time; + if ($real_delay < $log_delay) { + print STDERR "usleep for ", $log_delay - $real_delay, "\n"; + usleep(1000_000 * ($log_delay - $real_delay)); + } + } +} + +if ($opt{p}) { + print STDERR "Replay log finished - press a key: "; + flush STDERR; + $_ = ; +} +