<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl

use strict;
use warnings;

#use lib '../lib';
use Getopt::Std;
use Apache::TS::BinLog;

my ( $file, $file_format );
our( $opt_f, $opt_t, $opt_l, $opt_s );

getopt('ftls');

sub VERSION_MESSAGE {
    print "$0 $Apache::TS::BinLog::VERSION (C) 2013 Yahoo! inc.\n";
}

sub HELP_MESSAGE {
    VERSION_MESSAGE();
    print &lt;&lt;EOT;
usage: $0 -[options] -f &lt;filename&gt; /path/to/squid.blog file
options:
  -t [ 'END' | &lt;timestamp&gt; ] start parsing from &lt;timestamp&gt; or from 'END' - &lt;slice_length&gt; ( 'END' implies -l ) 
  -l &lt;slice_length&gt; in seconds.
  -s &lt;format_string&gt; only output fields listed in the &lt;format_string&gt; ( "field, field, field" ) 
EOT
exit 0;
}

if ( $opt_f ) { $file = $opt_f; }
else { die "You need to give me a file to slice with -f &lt;file&gt;"; }

my $parser = Apache::TS::BinLog-&gt;new( 'file' =&gt; $file );

if ( $opt_l ) { $parser-&gt;set_length( $opt_l ); }

if ( $opt_t &amp;&amp; $opt_t ne 'END' ) { $parser-&gt;set_tstamp( $opt_t ) };
if ( $opt_t &amp;&amp; $opt_l &amp;&amp; $opt_t eq 'END' ) { $parser-&gt;set_tstamp( $parser-&gt;get_last_tstamp() - $opt_l ); }

if ( $opt_s ) {
  my @input_fields = split( ',', $opt_s );
  my @line_fields = @{ $parser-&gt;get_line_fields() };
  my $valid = 0;
  foreach my $ifield ( @input_fields ) {
    $valid = 0;
    foreach my $lfield ( @line_fields ) {
      if ( $ifield eq $lfield ) { $valid = 1; }
    }
    last unless $valid;
  }
  if ( $valid ) {
    while ( my $line = $parser-&gt;get_line() ) {
      foreach my $ifield ( @input_fields ) {
        print $line-&gt;{ $ifield }, " ";
      }
      print "\n";
    } 
  } else { 
    print "One or more field(s) provided not found in current log file\n";
    print "Valid fields are:  ";
    foreach my $lfield ( @line_fields ) { print $lfield, " "; }
    print "\n";
    die;
  }
} else {
  while ( my $line = $parser-&gt;get_ascii_line() ) {
    print $line, "\n";
  }
}
</pre></body></html>