Log-Defer-Viz

 view release on metacpan or  search on metacpan

bin/log-defer-viz  view on Meta::CPAN

    'quiet' => 0,
    'verbose' => 0,

    'timers' => 1,
    'timer-columns' => 100,

    'data-format' => 'json-pretty',
    'data-only' => 0,

    'follow' => 0,
    'follow-reopen' => 0,
    'preserve-newlines' => 0,
    'sort-time' => 0,
};

GetOptions($opt, @opt_spec) || die "GetOptions failed";



if ($opt->{help}) {
  require Pod::Perldoc;
  @ARGV = ('-F', $0);
  Pod::Perldoc->run();
  exit;
}


die "Only json input-format is currently supported" unless $opt->{'input-format'} eq 'json';

$opt->{colour} = $opt->{color} if exists $opt->{color};
if (!exists $opt->{colour}) {
  $opt->{colour} = 1 if -t STDOUT;
}

if ($opt->{verbose} && $opt->{quiet}) {
  die "--verbose and --quiet are incompatible";
} elsif ($opt->{verbose}) {
  $opt->{verbosity} = 40;
  $opt->{data} = 1 unless (defined $opt->{data} && !$opt->{data});
} elsif ($opt->{quiet}) {
  $opt->{verbosity} = 20;
  $opt->{timers} = 0 unless !$opt->{timers};
}


if ($opt->{'data-only'}) {
  $opt->{date} = $opt->{timers} = $opt->{logs} = 0;
  $opt->{data} = 1;
}



my $columns = $opt->{'timer-columns'};

my ($term_cols, $term_rows) = Term::Size::chars(*STDOUT{IO});
$columns = $term_cols if $term_cols;



## The _ sub is a shortcut for use in --grep, --map, and --count expressions, ie --grep "_->{data}"
sub _ () { $_ }

if ($opt->{grep}) {
  my $expr = $opt->{grep};
  $opt->{grep} = eval('sub { local $_ = $_[0]; ' . "$expr\n" . '}');
  die "Error compiling --grep expression '$expr' ($@)" if $@;
}

if ($opt->{map}) {
  my $expr = $opt->{map};
  $opt->{map} = eval('sub { local $_ = $_[0]; ' . "$expr\n" . '}');
  die "Error compiling --map expression '$expr' ($@)" if $@;
}

$Log::Defer::Viz::Reduce::o = {};

if ($opt->{reduce}) {
  my $expr = $opt->{reduce};
  $opt->{reduce} = eval('package Log::Defer::Viz::Reduce; our $o; sub { local $_ = $_[0]; ' . "$expr\n" . '}');
  die "Error compiling --reduce expression '$expr' ($@)" if $@;
}

die "--pass-through and --map are incompatible" if $opt->{'pass-through'} && $opt->{map};
die "--pass-through and --reduce are incompatible" if $opt->{'pass-through'} && $opt->{reduce};


my $count = {};
my $count_subs = {};

foreach my $key (@{ $opt->{count} }) {
  if ($key =~ /^\w+$/) {
    ## The original format for count arguments were keys to the data hash
    $count_subs->{$key} = sub { $_[0]->{data}->{$key} };
  } else {
    ## But the more general way is with an accessor
    $count_subs->{$key} = eval('sub { local $_ = $_[0]; ' . "$key\n" . '}');
    die "Error compiling --count expression '$key' ($@)" if $@;
  }

  $count->{$key} = {};
}





unshift(@ARGV, '-') unless @ARGV;
if (@ARGV > 1 && ($opt->{follow} || $opt->{'follow-reopen'})) {
  die "You've specified --follow or --follow-reopen but have also provided multiple files, which doesn't make sense. Did you mean to do this?";
}

if ($opt->{follow} && $opt->{'follow-reopen'}) {
  die "-f and -F are mutually exclusive";
}


my ($sort_out_fh, $sort_in_fh);

if ($opt->{'sort-time'}) {
  open2($sort_out_fh, $sort_in_fh, 'sort', '-n');
}



( run in 1.055 second using v1.01-cache-2.11-cpan-800906f7e73 )