| 1 | package App::JobLog::Command::truncate; | ||
| 2 | $App::JobLog::Command::truncate::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: decapitate the log | ||
| 4 | |||
| 5 | |||
| 6 | use App::JobLog -command; | ||
| 7 | use autouse 'App::JobLog::TimeGrammar' => qw(parse); | ||
| 8 | use Class::Autouse | ||
| 9 | qw(IO::File App::JobLog::Log App::JobLog::Log::Line File::Temp File::Spec); | ||
| 10 | use autouse 'App::JobLog::Time' => qw(now); | ||
| 11 | use autouse 'App::JobLog::Config' => qw(log dir); | ||
| 12 | use autouse 'File::Copy' => qw(move); | ||
| 13 | |||
| 14 | use Modern::Perl; | ||
| 15 | no if $] >= 5.018, warnings => "experimental::smartmatch"; | ||
| 16 | |||
| 17 | sub execute { | ||
| 18 | my ( $self, $opt, $args ) = @_; | ||
| 19 | my $expression = join ' ', @$args; | ||
| 20 | my ( $s, $is_interval ); | ||
| 21 | eval { ( $s, undef, $is_interval ) = parse $expression; }; | ||
| 22 | $self->usage_error($@) if $@; | ||
| 23 | $self->usage_error('truncation date must not be a interval') | ||
| 24 | if $is_interval; | ||
| 25 | |||
| 26 | # determine name of head log | ||
| 27 | my $log = App::JobLog::Log->new; | ||
| 28 | my ($p) = $log->find_previous($s); | ||
| 29 | $self->usage("no event in log prior to $expression") unless $p; | ||
| 30 | my ($e) = $log->first_event; | ||
| 31 | my $base = 'log-' . $e->start->ymd . '--' . $p->start->ymd; | ||
| 32 | |||
| 33 | # create output handle for head log | ||
| 34 | my $io = | ||
| 35 | $opt->compression ? _pick_compression( $opt->compression ) : 'IO::File'; | ||
| 36 | my $suffix = ''; | ||
| 37 | my @args = (); | ||
| 38 | for ($io) { | ||
| 39 | when ('IO::File') { push @args, 'w' } | ||
| 40 | when ('IO::Compress::Zip') { | ||
| 41 | $suffix = '.zip'; | ||
| 42 | push @args, Name => $base; | ||
| 43 | } | ||
| 44 | when ('IO::Compress::Gzip') { $suffix = '.gz' } | ||
| 45 | when ('IO::Compress::Bzip2') { $suffix = '.bz2' } | ||
| 46 | when ('IO::Compress::Lzma') { $suffix = '.lzma' } | ||
| 47 | default { die "unprepared to handle $io; please report bug" }; | ||
| 48 | } | ||
| 49 | my $old_f = File::Spec->catfile( dir, $base . $suffix ); | ||
| 50 | my $old_fh = $io->new( $old_f, @args ); | ||
| 51 | my $fh = File::Temp->new; | ||
| 52 | my $current_fh = $old_fh; | ||
| 53 | my $log_handle = IO::File->new( log, 'r' ); | ||
| 54 | my ( $unswitched, @buffer, $previous ) = (1); | ||
| 55 | while ( defined( my $line = $log_handle->getline ) ) { | ||
| 56 | my $ll = App::JobLog::Log::Line->parse($line); | ||
| 57 | if ( $ll->is_event ) { | ||
| 58 | if ($unswitched) { | ||
| 59 | $previous = $ll if $ll->is_beginning; | ||
| 60 | if ( $ll->time > $s ) { | ||
| 61 | if ($previous) { # event spanning border | ||
| 62 | my $end_time = $s->clone->subtract( seconds => 1 ); | ||
| 63 | $current_fh->print( | ||
| 64 | App::JobLog::Log::Line->new( | ||
| 65 | done => 1, | ||
| 66 | time => $end_time | ||
| 67 | ) | ||
| 68 | ); | ||
| 69 | $previous->time = $s; | ||
| 70 | $line = $previous->to_string . "\n"; | ||
| 71 | } | ||
| 72 | $current_fh->close; | ||
| 73 | $current_fh = $fh; | ||
| 74 | _header( $base, $suffix, \@buffer ); | ||
| 75 | $unswitched = undef; | ||
| 76 | } | ||
| 77 | elsif ( $ll->is_end ) { | ||
| 78 | $previous = undef; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | while (@buffer) { | ||
| 82 | $current_fh->print( shift @buffer ); | ||
| 83 | } | ||
| 84 | $current_fh->print($line); | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | push @buffer, $line; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | while (@buffer) { | ||
| 91 | $current_fh->print( shift @buffer ); | ||
| 92 | } | ||
| 93 | $current_fh->close; | ||
| 94 | move( "$fh", log ); | ||
| 95 | print "truncated portion of log saved in $old_f\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub validate { | ||
| 99 | my ( $self, $opt, $args ) = @_; | ||
| 100 | $self->usage_error('no time expression provided') unless @$args; | ||
| 101 | if ( $opt->compression ) { | ||
| 102 | my $alg = _pick_compression( $opt->compression ); | ||
| 103 | eval "require $alg"; | ||
| 104 | $self->usage_error( | ||
| 105 | "$@: you must install $alg to use compression option --" | ||
| 106 | . $opt->compression ) | ||
| 107 | if $@; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub usage_desc { | ||
| 112 | '%c ' . __PACKAGE__->name . ' %o <date>'; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub abstract { | ||
| 116 | 'shorten the log to contain only those moments after a given date'; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub options { | ||
| 120 | return ( | ||
| 121 | [ | ||
| 122 | compression => hidden => { | ||
| 123 | one_of => [ | ||
| 124 | [ 'zip|z', 'pass truncated head of log through zip', ], | ||
| 125 | [ 'gzip|g', 'pass truncated head of log through gzip', ], | ||
| 126 | [ 'bzip2|b', 'pass truncated head of log through bzip2', ], | ||
| 127 | [ 'lzma|l', 'pass truncated head of log through lzma', ], | ||
| 128 | ] | ||
| 129 | } | ||
| 130 | ] | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub full_description { | ||
| 135 | <<END | ||
| 136 | Over time your log will fill with cruft: work no one is interested in any longer, | ||
| 137 | tags whose meaning you've forgotten. What you want to do at this point is chop off | ||
| 138 | all the old stuff, stash it somewhere you can find it if need be, and retain in | ||
| 139 | your active log only the more recent events. This is what truncate is for. You give | ||
| 140 | it a starting date and it splits your log into two with the active portion containing | ||
| 141 | all moments on that date or after. The older portion is retained in your joblog hidden | ||
| 142 | directory. | ||
| 143 | END | ||
| 144 | } | ||
| 145 | |||
| 146 | # comment header added to truncated log | ||
| 147 | sub _header { | ||
| 148 | my ( $base, $suffix, $buffer ) = @_; | ||
| 149 | unshift @$buffer, | ||
| 150 | map { App::JobLog::Log::Line->new( comment => "$_\n" ) } | ||
| 151 | <<END =~ /.*\S/mg; #<--- Global symbol "$base" requires explicit package name at (eval 1853) line 9. | ||
| 152 | Log file truncated on @{[now]}. | ||
| 153 | Head of log to be found in $base$suffix | ||
| 154 | END | ||
| 155 | } | ||
| 156 | |||
| 157 | # converts chosen compression opt into appropriate IO:: algorithm | ||
| 158 | sub _pick_compression { | ||
| 159 | my $alg = shift; | ||
| 160 | for ($alg) { | ||
| 161 | when ('zip') { return 'IO::Compress::Zip' } | ||
| 162 | when ('gzip') { return 'IO::Compress::Gzip' } | ||
| 163 | when ('bzip2') { return 'IO::Compress::Bzip2' } | ||
| 164 | when ('lzma') { return 'IO::Compress::Lzma' } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | __END__ |