App-JobLog
view release on metacpan or search on metacpan
lib/App/JobLog/Log.pm view on Meta::CPAN
return $self;
}
sub lines {
[ shift->[IO]->getlines ];
}
sub all_taglines {
my ($self) = @_;
# reopen log in sequential reading mode
$self->[IO] = io log;
my (@lines);
while ( my $line = $self->[IO]->getline ) {
my $ll = App::JobLog::Log::Line->parse($line);
push @lines, $ll if $ll->is_beginning;
}
return \@lines;
}
sub all_events {
my ($self) = @_;
# reopen log in sequential reading mode
$self->[IO] = io log;
my ( @events, $previous );
while ( my $line = $self->[IO]->getline ) {
my $ll = App::JobLog::Log::Line->parse($line);
if ( $ll->is_endpoint ) {
$previous->end = $ll->time if $previous;
if ( $ll->is_beginning ) {
$previous = App::JobLog::Log::Event->new($ll);
push @events, $previous;
}
else {
$previous = undef;
}
}
}
return \@events;
}
sub all_notes {
my ($self) = @_;
# reopen log in sequential reading mode
$self->[IO] = io log;
my @notes;
while ( my $line = $self->[IO]->getline ) {
my $ll = App::JobLog::Log::Line->parse($line);
push @notes, App::JobLog::Log::Note->new($ll) if $ll->is_note;
}
return \@notes;
}
sub validate {
my ($self) = @_;
my ( $i, $previous_event ) = (0);
my $errors = 0;
while ( my $line = $self->[IO][$i] ) {
my $ll = App::JobLog::Log::Line->parse($line);
if ( $ll->is_malformed ) {
$errors++;
print STDERR "line $i -- '$line' -- is malformed; commenting out\n";
splice @{ $self->[IO] }, $i, 0,
App::JobLog::Log::Line->new( comment => 'ERROR; malformed line' );
$self->[IO][ ++$i ] = $ll->comment_out;
}
elsif ( $ll->is_event ) {
if ($previous_event) {
if ( DateTime->compare( $previous_event->time, $ll->time ) > 0 )
{
$errors++;
print STDERR
"line $i -- '$line' -- is out of order relative to the last event; commenting out\n";
splice @{ $self->[IO] }, $i, 0,
App::JobLog::Log::Line->new(
comment => 'ERROR; dates out of order' );
$self->[IO][ ++$i ] = $ll->comment_out;
}
elsif ( $previous_event->is_end && $ll->is_end ) {
$errors++;
print STDERR
"line $i -- '$line' -- specifies the end of a task not yet begun; commenting out\n";
splice @{ $self->[IO] }, $i, 0,
App::JobLog::Log::Line->new( comment =>
'ERROR; task end without corresponding beginning' );
$self->[IO][ ++$i ] = $ll->comment_out;
}
else {
$previous_event = $ll;
}
}
elsif ( $ll->is_end ) {
$errors++;
print STDERR
"line $i -- '$line' -- specifies the end of a task not yet begun; commenting out\n";
splice @{ $self->[IO] }, $i, 0,
App::JobLog::Log::Line->new( comment =>
'ERROR; task end without corresponding beginning' );
$self->[IO][ ++$i ] = $ll->comment_out;
}
else {
$previous_event = $ll;
}
}
$i++;
}
return $errors;
}
sub first_event {
my ($self) = @_;
return $self->[FIRST_EVENT], $self->[FIRST_INDEX] if $self->[FIRST_EVENT];
my $io = $self->[IO];
lib/App/JobLog/Log.pm view on Meta::CPAN
sub replace {
my ( $self, $index, $line ) = @_;
carp 'expected integer and log line'
unless $index =~ /^\d++$/ && ref $line eq 'App::JobLog::Log::Line';
$self->[IO][$index] = $line;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
App::JobLog::Log - the code that lets us interact with the log
=head1 VERSION
version 1.042
=head1 DESCRIPTION
C<App::JobLog::Log> uses an L<IO::All> object to extract information from the
log file or add lines to it.
This wasn't written to be used outside of C<App::JobLog>. The code itself contains interlinear comments if
you want the details.
=head1 METHODS
=head2 new
C<new> is the constructor, naturally. It touches the log file into existence
if it does not yet exist, initializing the hidden job log directory in the
process, which means creating the directory and the README file. It also opens
an L<IO::All> object to read or modify the log with.
=head2 lines
A reference to the list of lines in the current log. This is useful chiefly
for debugging.
=head2 all_taglines
C<all_taglines> returns a list of all lines in the log that may have tags.
=head2 all_events
C<all_events> processes the log as a stream, extracting all events and
returning them as an array reference.
=head2 all_notes
C<all_notes> processes the log as a stream, extracting all notes and
returning them as an array reference.
=head2 validate
C<validate> makes sure the log contains only valid lines, all events are
in chronological order, and every ending follows a beginning. Invalid lines
are commented out and a warning is emitted. The number of errors found is
returned.
=head2 first_event
C<first_event> returns the first event in the log and the index
of its line. Its return object is an L<App::JobLog::Log::Event>.
=head2 last_ts
Returns last L<DateTime> timestamp in log and the index of this timestamp.
=head2 first_ts
Returns first L<DateTime> timestamp in log.
=head2 last_event
C<last_event> returns the last event in the log and the index
of its line. Its return object is an L<App::JobLog::Log::Event>.
=head2 last_note
Returns most recent note in log and its index, or the empty list if none is found.
=head2 reverse_iterator
C<reverse_iterator> returns a closure that allows you to iterate
over the events in the log in reverse. Every time you call the closure
it returns the next unvisited event.
If you pass this method an optional argument, either a L<DateTime> or a
L<App::JobLog::Log::Event>, it will iterate from the event beginning at or
after this event or time.
=head2 find_events
C<find_events> expects two L<DateTime> objects representing the
termini of an interval. It returns an array reference containing
the portion of all logged events falling within this interval. These
portions are represented as L<App::JobLog::Log::Event> objects.
=head2 find_notes
C<find_notes> expects two L<DateTime> objects representing the
termini of an interval. It returns an array reference containing
the portion of all logged notes falling within this interval. These
portions are represented as L<App::JobLog::Log::Note> objects.
=head2 find_previous
C<find_previous> looks for the logged event previous to a given
moment, returning the L<App::JobLog::Log::Event> objects and the
appropriate log line number, or the empty list if no such
event exists. It expects a L<DateTime> object as its parameter.
=head2 find_previous
C<find_previous> looks for the logged event previous to a given
( run in 1.060 second using v1.01-cache-2.11-cpan-39bf76dae61 )