App-JobLog

 view release on metacpan or  search on metacpan

lib/App/JobLog/Command/truncate.pm  view on Meta::CPAN

    for ($io) {
        when ('IO::File') { push @args, 'w' }
        when ('IO::Compress::Zip') {
            $suffix = '.zip';
            push @args, Name => $base;
        }
        when ('IO::Compress::Gzip')  { $suffix = '.gz' }
        when ('IO::Compress::Bzip2') { $suffix = '.bz2' }
        when ('IO::Compress::Lzma')  { $suffix = '.lzma' }
        default { die "unprepared to handle $io; please report bug" };
    }
    my $old_f = File::Spec->catfile( dir, $base . $suffix );
    my $old_fh     = $io->new( $old_f, @args );
    my $fh         = File::Temp->new;
    my $current_fh = $old_fh;
    my $log_handle = IO::File->new( log, 'r' );
    my ( $unswitched, @buffer, $previous ) = (1);
    while ( defined( my $line = $log_handle->getline ) ) {
        my $ll = App::JobLog::Log::Line->parse($line);
        if ( $ll->is_event ) {
            if ($unswitched) {
                $previous = $ll if $ll->is_beginning;
                if ( $ll->time > $s ) {
                    if ($previous) {    # event spanning border
                        my $end_time = $s->clone->subtract( seconds => 1 );
                        $current_fh->print(
                            App::JobLog::Log::Line->new(
                                done => 1,
                                time => $end_time
                            )
                        );
                        $previous->time = $s;
                        $line = $previous->to_string . "\n";
                    }
                    $current_fh->close;
                    $current_fh = $fh;
                    _header( $base, $suffix, \@buffer );
                    $unswitched = undef;
                }
                elsif ( $ll->is_end ) {
                    $previous = undef;
                }
            }
            while (@buffer) {
                $current_fh->print( shift @buffer );
            }
            $current_fh->print($line);
        }
        else {
            push @buffer, $line;
        }
    }
    while (@buffer) {
        $current_fh->print( shift @buffer );
    }
    $current_fh->close;
    move( "$fh", log );
    print "truncated portion of log saved in $old_f\n";
}

sub validate {
    my ( $self, $opt, $args ) = @_;
    $self->usage_error('no time expression provided') unless @$args;
    if ( $opt->compression ) {
        my $alg = _pick_compression( $opt->compression );
        eval "require $alg";
        $self->usage_error(
            "$@: you must install $alg to use compression option --"
              . $opt->compression )
          if $@;
    }
}

sub usage_desc {
    '%c ' . __PACKAGE__->name . ' %o <date>';
}

sub abstract {
    'shorten the log to contain only those moments after a given date';
}

sub options {
    return (
        [
            compression => hidden => {
                one_of => [
                    [ 'zip|z',   'pass truncated head of log through zip', ],
                    [ 'gzip|g',  'pass truncated head of log through gzip', ],
                    [ 'bzip2|b', 'pass truncated head of log through bzip2', ],
                    [ 'lzma|l',  'pass truncated head of log through lzma', ],
                ]
            }
        ]
    );
}

sub full_description {
    <<END
Over time your log will fill with cruft: work no one is interested in any longer,
tags whose meaning you've forgotten. What you want to do at this point is chop off
all the old stuff, stash it somewhere you can find it if need be, and retain in
your active log only the more recent events. This is what truncate is for. You give
it a starting date and it splits your log into two with the active portion containing
all moments on that date or after. The older portion is retained in your joblog hidden
directory.
END
}

# comment header added to truncated log
sub _header {
    my ( $base, $suffix, $buffer ) = @_;
    unshift @$buffer,
      map { App::JobLog::Log::Line->new( comment => "$_\n" ) }
      <<END =~ /.*\S/mg; #<--- Global symbol "$base" requires explicit package name at (eval 1853) line 9.
Log file truncated on @{[now]}.
Head of log to be found in $base$suffix
END
}

# converts chosen compression opt into appropriate IO:: algorithm
sub _pick_compression {



( run in 0.495 second using v1.01-cache-2.11-cpan-39bf76dae61 )