JobLog/Log.pm untested

filter: Statementstmt 0.0% 0 / 477 Branchbran 0.0% 0 / 234 Conditioncond 0.0% 0 / 80 MC/DCmcdc 0.0% 0 / 50 Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 912 CC 117 SCAR 47.8
CC
117
Cov
0%
find_events
420.0
find_notes
306.0
append_event
156.0
CRAP 119.6
1 package App::JobLog::Log;
2 $App::JobLog::Log::VERSION = '1.042';
3 # ABSTRACT: the code that lets us interact with the log
4  
5  
6 use Modern::Perl;
7 use App::JobLog::Config qw(log init_file);
8 use App::JobLog::Log::Line;
9 use IO::All -utf8;
10 use autouse 'Carp' => qw(carp);
11 use autouse 'App::JobLog::Time' => qw(now);
12 use Class::Autouse qw(
13 App::JobLog::Log::Event
14 App::JobLog::Log::Note
15 DateTime
16 FileHandle
17 );
18 no if $] >= 5.018, warnings => "experimental::smartmatch";
19  
20 # some stuff useful for searching log
21 use constant WINDOW => 30;
22 use constant LOW_LIM => 1 / 10;
23 use constant HIGH_LIM => 1 - LOW_LIM;
24  
25 # some indices
26 use constant IO => 0;
27 use constant FIRST_EVENT => 1;
28 use constant LAST_EVENT => 2;
29 use constant FIRST_INDEX => 3;
30 use constant LAST_INDEX => 4;
31  
32 # timestamp format
33 use constant TS => '%Y/%m/%d';
34  
35  
36 sub new {
37 my $class = shift;
38 $class = ref $class if ref $class;
39  
40 # touch log into existence
41 unless ( -e log ) {
42 init_file log;
43 my $fh = FileHandle->new( log, 'w' );
44 $fh->close;
45 }
46  
47 # using an array to make things a little snappier
48 my $self = bless [], $class;
49 $self->[IO] = io log;
50 return $self;
51 }
52  
53  
54 sub lines {
55 [ shift->[IO]->getlines ];
56 }
57  
58  
59 sub all_taglines {
60 my ($self) = @_;
61  
62 # reopen log in sequential reading mode
63 $self->[IO] = io log;
64 my (@lines);
65 while ( my $line = $self->[IO]->getline ) {
66 my $ll = App::JobLog::Log::Line->parse($line);
67 push @lines, $ll if $ll->is_beginning;
68 }
69 return \@lines;
70 }
71  
72  
73 sub all_events {
74 my ($self) = @_;
75  
76 # reopen log in sequential reading mode
77 $self->[IO] = io log;
78 my ( @events, $previous );
79 while ( my $line = $self->[IO]->getline ) {
80 my $ll = App::JobLog::Log::Line->parse($line);
81 if ( $ll->is_endpoint ) {
82 $previous->end = $ll->time if $previous;
83 if ( $ll->is_beginning ) {
84 $previous = App::JobLog::Log::Event->new($ll);
85 push @events, $previous;
86 }
87 else {
88 $previous = undef;
89 }
90 }
91 }
92 return \@events;
93 }
94  
95  
96 sub all_notes {
97 my ($self) = @_;
98  
99 # reopen log in sequential reading mode
100 $self->[IO] = io log;
101 my @notes;
102 while ( my $line = $self->[IO]->getline ) {
103 my $ll = App::JobLog::Log::Line->parse($line);
104 push @notes, App::JobLog::Log::Note->new($ll) if $ll->is_note;
105 }
106 return \@notes;
107 }
108  
109  
110 sub validate {
111 my ($self) = @_;
112 my ( $i, $previous_event ) = (0);
113 my $errors = 0;
114 while ( my $line = $self->[IO][$i] ) {
115 my $ll = App::JobLog::Log::Line->parse($line);
116 if ( $ll->is_malformed ) {
117 $errors++;
118 print STDERR "line $i -- '$line' -- is malformed; commenting out\n";
119 splice @{ $self->[IO] }, $i, 0,
120 App::JobLog::Log::Line->new( comment => 'ERROR; malformed line' );
121 $self->[IO][ ++$i ] = $ll->comment_out;
122 }
123 elsif ( $ll->is_event ) {
124 if ($previous_event) {
125 if ( DateTime->compare( $previous_event->time, $ll->time ) > 0 )
126 {
127 $errors++;
128 print STDERR
129 "line $i -- '$line' -- is out of order relative to the last event; commenting out\n";
130 splice @{ $self->[IO] }, $i, 0,
131 App::JobLog::Log::Line->new(
132 comment => 'ERROR; dates out of order' );
133 $self->[IO][ ++$i ] = $ll->comment_out;
134 }
135 elsif ( $previous_event->is_end && $ll->is_end ) {
136 $errors++;
137 print STDERR
138 "line $i -- '$line' -- specifies the end of a task not yet begun; commenting out\n";
139 splice @{ $self->[IO] }, $i, 0,
140 App::JobLog::Log::Line->new( comment =>
141 'ERROR; task end without corresponding beginning' );
142 $self->[IO][ ++$i ] = $ll->comment_out;
143 }
144 else {
145 $previous_event = $ll;
146 }
147 }
148 elsif ( $ll->is_end ) {
149 $errors++;
150 print STDERR
151 "line $i -- '$line' -- specifies the end of a task not yet begun; commenting out\n";
152 splice @{ $self->[IO] }, $i, 0,
153 App::JobLog::Log::Line->new( comment =>
154 'ERROR; task end without corresponding beginning' );
155 $self->[IO][ ++$i ] = $ll->comment_out;
156 }
157 else {
158 $previous_event = $ll;
159 }
160 }
161 $i++;
162 }
163 return $errors;
164 }
165  
166  
167 sub first_event {
168 my ($self) = @_;
169 return $self->[FIRST_EVENT], $self->[FIRST_INDEX] if $self->[FIRST_EVENT];
170 my $io = $self->[IO];
171 my ( $i, $e ) = 0;
172 while ( $i <= $#$io ) {
173 my $line = $io->[$i];
174 my $ll = App::JobLog::Log::Line->parse($line);
175 if ( $ll->is_endpoint ) {
176 if ($e) {
177 $e->end = $ll->time;
178 last;
179 }
180 else {
181 $e = App::JobLog::Log::Event->new($ll);
182 $self->[FIRST_INDEX] = $i;
183 }
184 }
185 $i++;
186 }
187 $self->[FIRST_EVENT] = $e;
188 return $e, $self->[FIRST_INDEX];
189 }
190  
191  
192 sub last_ts {
193 my ($self) = @_;
194 my $io = $self->[IO];
195 my $i = $#$io;
196 for ( ; $i >= 0 ; $i-- ) {
197 my $ll = App::JobLog::Log::Line->parse( $io->[$i] );
198 return ( $ll->time, $i ) if $ll->is_event;
199 }
200 return;
201 }
202  
203  
204 sub first_ts {
205 my ($self) = @_;
206 my $io = $self->[IO];
207 my $i = 0;
208 for ( my $lim = $#$io ; $i <= $lim ; $i++ ) {
209 my $ll = App::JobLog::Log::Line->parse( $io->[$i] );
210 return ( $ll->time, $i ) if $ll->is_event;
211 }
212 return;
213 }
214  
215  
216 sub last_event {
217 my ($self) = @_;
218 return $self->[LAST_EVENT], $self->[LAST_INDEX] if $self->[LAST_EVENT];
219 my $io = $self->[IO];
220  
221 # was hoping to use IO::All::backwards for this, but seems to be broken
222 # uncertain how to handle utf8 issue with File::ReadBackwards
223 my @lines;
224 my $i = $#$io;
225 for ( ; $i >= 0 ; $i-- ) {
226 my $line = $self->[IO][$i];
227 my $ll = App::JobLog::Log::Line->parse($line);
228 if ( $ll->is_endpoint ) {
229 push @lines, $ll;
230 last if $ll->is_beginning;
231 }
232 }
233 return () unless @lines;
234 my $e = App::JobLog::Log::Event->new( pop @lines );
235 $e->end = $lines[0]->time if @lines;
236 $self->[LAST_EVENT] = $e;
237 $self->[LAST_INDEX] = $i;
238 return $e, $i;
239 }
240  
241  
242 sub last_note {
243 my ($self) = @_;
244 my $io = $self->[IO];
245 for ( my $i = $#$io ; $i >= 0 ; $i-- ) {
246 my $line = $io->[$i];
247 my $ll = App::JobLog::Log::Line->parse($line);
248 return ( App::JobLog::Log::Note->new($ll), $i ) if $ll->is_note;
249 }
250 return ();
251 }
252  
253  
254 sub reverse_iterator {
255 my ( $self, $event ) = @_;
256 if ( ref $event ) {
257 if ( $event->isa('DateTime') ) {
258 my $events =
259 $self->find_events( $event, $self->first_event->start );
260 if (@$events) {
261 $event = $events->[$#$events];
262 }
263 else {
264 $event = undef;
265 }
266 }
267 }
268 else {
269 ($event) = $self->last_event;
270 }
271 return sub { }
272 unless $event;
273 my ( undef, $index, $io ) =
274 ( $self->find_previous( $event->start ), $self->[IO] );
275 return sub {
276 return () unless $event;
277 my $e = $event;
278 my $end_time = $event->start;
279 $event = undef;
280 while ( --$index >= 0 ) {
281 my $line = $io->[$index];
282 my $ll = App::JobLog::Log::Line->parse($line);
283 if ( $ll->is_beginning ) {
284 $event = App::JobLog::Log::Event->new($ll);
285 $event->end = $end_time;
286 last;
287 }
288 elsif ( $ll->is_end ) {
289 $end_time = $ll->time;
290 }
291 }
292 return $e;
293 };
294 }
295  
296  
297 sub find_events {
298 my ( $self, $start, $end ) = @_;
299 my $io = $self->[IO];
300 my ( $end_event, $bottom, $start_event, $top ) =
301 ( $self->last_event, $self->first_event );
302  
303 # if the log is empty, return empty list
304 return [] unless $start_event && $end_event;
305  
306 # if the log concerns events before the time in question, return empty list
307 return []
308 unless $end_event->is_open
309 || DateTime->compare( $start, $end_event->end ) < 0;
310  
311 # likewise if it concerns events after
312 return [] if DateTime->compare( $start_event->start, $end ) > 0;
313  
314 # narrow time range to that in log
315 my $c1 = DateTime->compare( $start, $start_event->start ) <= 0;
316 my $c2 =
317 $end_event->is_open
318 ? DateTime->compare( $end, $end_event->start ) >= 0
319 : DateTime->compare( $end, $end_event->end ) >= 0;
320 return $self->all_events if $c1 && $c2;
321 $start = $start_event->start if $c1;
322 $end = $end_event->end if $c2;
323  
324 # matters are simple if what we want is at the start of the log
325 if ($c1) {
326 my ( $line, $previous, @events );
327 while ( my $line = $io->getline ) {
328 chomp $line;
329 my $ll = App::JobLog::Log::Line->parse($line);
330 if ( $ll->is_endpoint ) {
331 if ( DateTime->compare( $ll->time, $end ) >= 0 ) {
332 $previous->end = $end if $previous->is_open;
333 last;
334 }
335 if ( $previous && $previous->is_open ) {
336 $previous->end = $ll->time;
337 }
338 if ( $ll->is_beginning ) {
339 $previous = App::JobLog::Log::Event->new($ll);
340 push @events, $previous;
341 }
342 }
343 }
344 return \@events;
345 }
346  
347 # matters are likewise simple if what we want is at the end of the log
348 if ($c2) {
349  
350 # must restart io
351 $io = $self->[IO] = io log;
352 $io->backwards;
353 my ( $line, $previous, @events );
354 while ( my $line = $io->getline ) {
355 chomp $line;
356 my $ll = App::JobLog::Log::Line->parse($line);
357 if ( $ll->is_endpoint ) {
358 my $e;
359 if ( $ll->is_beginning ) {
360 $e = App::JobLog::Log::Event->new($ll);
361 $e->end = $previous->time if $previous;
362 unshift @events, $e;
363 }
364 if ( DateTime->compare( $ll->time, $start ) <= 0 ) {
365 $e->start = $start if $e;
366 last;
367 }
368 $previous = $ll;
369 }
370 }
371 return \@events;
372 }
373  
374 # otherwise, do binary search for first event in range
375 my ( undef, $i ) = $self->find_previous($start);
376 return $self->_scan_from( $i, $start, $end );
377 }
378  
379  
380 sub find_notes {
381 my ( $self, $start, $end ) = @_;
382 my $io = $self->[IO];
383 my ( $end_time, $bottom, $start_time, $top ) =
384 ( $self->last_ts, $self->first_ts );
385  
386 # if the log is empty, return empty list
387 return [] unless $start_time && $end_time;
388  
389 # if the log concerns events before the time in question, return empty list
390 return []
391 unless DateTime->compare( $start, $end_time ) <= 0;
392  
393 # likewise if it concerns events after
394 return [] if DateTime->compare( $start_time, $end ) > 0;
395  
396 # narrow time range to that in log
397 my $c1 = DateTime->compare( $start, $start_time ) <= 0;
398 my $c2 = DateTime->compare( $end, $end_time ) >= 0;
399 return $self->all_notes if $c1 && $c2;
400 $start = $start_time if $c1;
401 $end = $end_time if $c2;
402  
403 # matters are simple if what we want is at the start of the log
404 if ($c1) {
405 my ( $line, @notes );
406 while ( my $line = $io->getline ) {
407 chomp $line;
408 my $ll = App::JobLog::Log::Line->parse($line);
409 if ( $ll->is_event ) {
410 if ( DateTime->compare( $ll->time, $end ) >= 0 ) {
411 last;
412 }
413 push @notes, App::JobLog::Log::Note->new($ll) if $ll->is_note;
414 }
415 }
416 return \@notes;
417 }
418  
419 # matters are likewise simple if what we want is at the end of the log
420 if ($c2) {
421  
422 # must restart io
423 $io = $self->[IO] = io log;
424 $io->backwards;
425 my ( $line, @notes );
426 while ( my $line = $io->getline ) {
427 chomp $line;
428 my $ll = App::JobLog::Log::Line->parse($line);
429 if ( $ll->is_event ) {
430 $c2 = DateTime->compare( $ll->time, $start );
431 last if $c2 < 0;
432 unshift @notes, App::JobLog::Log::Note->new($ll)
433 if $ll->is_note;
434 last unless $c2;
435 }
436 }
437 return \@notes;
438 }
439  
440 # otherwise, do binary search for first note in range
441 my $i =
442 $self->_find_previous_note( $start, $end_time, $bottom, $start_time,
443 $top );
444 return [] unless defined $i;
445 return $self->_scan_for_note_from( $i, $start, $end );
446 }
447  
448  
449 sub find_previous {
450 my ( $self, $e ) = @_;
451 my $io = $self->[IO];
452 my ( $end_event, $bottom, $start_event, $top ) =
453 ( $self->last_event, $self->first_event );
454  
455 # if the log is empty, return empty list
456 return () unless $start_event && $end_event;
457  
458 # if the start time (improbably but fortuitously) happens to be what we're
459 # looking for, return it
460 return ( $start_event, $top )
461 if DateTime->compare( $start_event->start, $e ) == 0;
462  
463 # likewise for the end time
464 return ( $end_event, $bottom ) if $end_event->start < $e;
465  
466 # return the empty list if the event in question precede the first
467 # event in the log
468 return () unless $start_event->start < $e;
469  
470 # otherwise, do binary search for first event in range
471 my ( $et, $eb ) = ( $start_event->start, $end_event->start );
472 my $previous_index;
473 OUTER: while (1) {
474 return $self->_scan_for_previous( $top, $e )
475 if $bottom - $top + 1 <= WINDOW / 2;
476 my $index = _estimate_index( $top, $bottom, $et, $eb, $e );
477 if ( defined $previous_index && $previous_index == $index ) {
478  
479 # search was too clever by half; we've entered an infinite loop
480 return $self->_scan_for_previous( $top, $e );
481 }
482 $previous_index = $index;
483 my $event;
484 for my $i ( $index .. $#$io ) {
485 my $line = $io->[$i];
486 my $ll = App::JobLog::Log::Line->parse($line);
487 if ( $ll->is_beginning ) {
488 my $do_next = 1;
489 for ( DateTime->compare( $ll->time, $e ) ) {
490 when ( $_ < 0 ) {
491 $top = $i;
492 $et = $ll->time;
493 }
494 when ( $_ > 0 ) {
495 $bottom = $i;
496 $eb = $ll->time;
497 }
498 default {
499  
500 # found beginning!!
501 # this should happen essentially never
502 $do_next = 0;
503 }
504 }
505 next OUTER if $do_next;
506 return $self->_scan_for_previous( $i, $e );
507 }
508 }
509 }
510 }
511  
512  
513 sub _find_previous_note {
514 my ( $self, $e, $eb, $bottom, $et, $top ) = @_;
515 my $io = $self->[IO];
516  
517 # binary search for first note in range
518 my $previous_index;
519 OUTER: while (1) {
520 return $self->_scan_for_previous_note( $top, $e )
521 if $bottom - $top + 1 <= WINDOW / 2;
522 my $index = _estimate_index( $top, $bottom, $et, $eb, $e );
523 if ( defined $previous_index && $previous_index == $index ) {
524  
525 # search was too clever by half; we've entered an infinite loop
526 return $self->_scan_for_previous_note( $top, $e );
527 }
528 $previous_index = $index;
529 my $event;
530 for my $i ( $index .. $#$io ) {
531 my $line = $io->[$i];
532 my $ll = App::JobLog::Log::Line->parse($line);
533 if ( $ll->is_event ) {
534 for ( DateTime->compare( $ll->time, $e ) ) {
535 when ( $_ < 0 ) {
536 $top = $i;
537 $et = $ll->time;
538 next OUTER;
539 }
540 when ( $_ > 0 ) {
541 $bottom = $i;
542 $eb = $ll->time;
543 next OUTER;
544 }
545 default {
546  
547 # found beginning!!
548 # this should happen essentially never
549 return $self->_scan_for_previous_note( $i, $e );
550 }
551 }
552 }
553 }
554 }
555 }
556  
557 # now that we're close to the section of the log we want, we
558 # scan it sequentially
559 sub _scan_from {
560 my ( $self, $i, $start, $end ) = @_;
561 my $io = $self->[IO];
562  
563 # collect events
564 my ( $previous, @events );
565 for my $index ( $i .. $#$io ) {
566 my $line = $io->[$index];
567 my $ll = App::JobLog::Log::Line->parse($line);
568 if ( $ll->is_endpoint ) {
569 if ($previous) {
570 $previous->end = $ll->time if $previous->is_open;
571 push @events, $previous
572 if DateTime->compare( $start, $previous->end ) < 0;
573 }
574 if ( $ll->is_beginning ) {
575 last if DateTime->compare( $ll->time, $end ) >= 0;
576 $previous = App::JobLog::Log::Event->new($ll);
577 }
578 else {
579 $previous = undef;
580 }
581 }
582 }
583 push @events, $previous
584 if $previous
585 && $previous->is_open
586 && DateTime->compare( $previous->start, $end ) < 0;
587  
588 # return only overlap
589 my @return = map { $_->overlap( $start, $end ) } @events;
590 return \@return;
591 }
592  
593 sub _scan_for_note_from {
594 my ( $self, $i, $start, $end ) = @_;
595 my $io = $self->[IO];
596  
597 # collect notes
598 my @notes;
599 for my $index ( $i .. $#$io ) {
600 my $line = $io->[$index];
601 my $ll = App::JobLog::Log::Line->parse($line);
602 if ( $ll->is_event ) {
603 last if $ll->time > $end;
604 if ( $ll->is_note && $ll->time >= $start ) {
605 push @notes, App::JobLog::Log::Note->new($ll);
606 }
607 }
608 }
609 return \@notes;
610 }
611  
612 sub _scan_for_previous {
613 my ( $self, $i, $e ) = @_;
614 my $io = $self->[IO];
615  
616 # collect events
617 my ( $previous, $previous_index );
618 OUTER: {
619 for my $index ( $i .. $#$io ) {
620 my $line = $io->[$index];
621 my $ll = App::JobLog::Log::Line->parse($line);
622 if ( $ll->is_endpoint ) {
623 $previous->end = $ll->time if $previous && $previous->is_open;
624 if ( $ll->time > $e ) {
625 last if $previous;
626 $i--;
627 redo OUTER;
628 }
629 if ( $ll->is_beginning ) {
630 $previous = App::JobLog::Log::Event->new($ll);
631 $previous_index = $index;
632 }
633 }
634 }
635 }
636 return $previous, $previous_index;
637 }
638  
639 sub _scan_for_previous_note {
640 my ( $self, $i, $e ) = @_;
641 my $io = $self->[IO];
642  
643 # collect events
644 my ( $previous, $previous_index );
645 for my $index ( $i .. $#$io ) {
646 my $line = $io->[$index];
647 my $ll = App::JobLog::Log::Line->parse($line);
648 if ( $ll->is_event ) {
649 last if $ll->time > $e;
650 if ( $ll->is_note ) {
651 $previous = App::JobLog::Log::Note->new($ll);
652 $previous_index = $index;
653 }
654 }
655 }
656 return $previous_index // $i;
657 }
658  
659 # your generic O(log_n) complexity bisecting search
660 sub _estimate_index {
661 my ( $top, $bottom, $et, $eb, $s ) = @_;
662 my $delta = $bottom - $top + 1;
663 my $i;
664 if ( $delta > WINDOW ) {
665 my $d1 = $s->epoch - $et->epoch;
666 my $d2 = $eb->epoch - $et->epoch;
667 my $fraction = $d1 / $d2;
668 if ( $fraction < LOW_LIM ) {
669 $fraction = LOW_LIM;
670 }
671 elsif ( $fraction > HIGH_LIM ) {
672 $fraction = HIGH_LIM;
673 }
674 $i = sprintf '%.0f', $delta * $fraction;
675 }
676 else {
677 $i = sprintf '%.0f', $delta / 2;
678 }
679 $i ||= 1;
680 return $top + $i;
681 }
682  
683  
684 sub append_event {
685 my ( $self, @args ) = @_;
686 my $current = @args == 1 ? $args[0] : App::JobLog::Log::Line->new(@args);
687 my $io = $self->[IO];
688 my $duration;
689 if ( $current->is_event ) {
690 my ( $previous, $last_index ) = $self->last_event;
691 if ($previous) {
692  
693 # validation to prevent inconsistency
694 carp 'no currently open task'
695 if $current->is_end && $previous->is_closed;
696 if (
697 $current->is_beginning
698 && ( $current->time < $previous->start
699 || $previous->is_closed && $current->time < $previous->end )
700 )
701 {
702 carp
703 'attempting to append event to log younger than last event in log';
704 }
705  
706 # apply default tags
707 $current->tags = $previous->tags if $current->tags_unspecified;
708  
709 # check for day change
710 my ($last_ts) = $self->last_ts;
711 if ( !$last_ts || _different_day( $last_ts, $current->time ) ) {
712 $io->append(
713 App::JobLog::Log::Line->new(
714 comment => $current->time->strftime(TS)
715 )
716 )->append("\n");
717 }
718 if ( $previous->is_open ) {
719 $duration =
720 $current->time->subtract_datetime( $previous->start );
721 $duration = undef unless $duration->in_units('days');
722 }
723 }
724 else {
725  
726 # first record in log
727 $io->append(
728 App::JobLog::Log::Line->new(
729 comment => $current->time->strftime(TS)
730 )
731 )->append("\n");
732 }
733  
734 # cache last event; useful during debugging
735 if ( $current->is_beginning ) {
736 $self->[LAST_EVENT] = App::JobLog::Log::Event->new($current);
737 $self->[LAST_INDEX] = @$io;
738 }
739 elsif ( $self->[LAST_EVENT] && $self->[LAST_EVENT]->is_open ) {
740 $self->[LAST_EVENT]->end = $current->time;
741 }
742 }
743 $io->append($current)->append("\n");
744 $io->close; # flush contents
745 return $duration;
746 }
747  
748  
749 sub append_note {
750 my ( $self, @args ) = @_;
751 my $note = App::JobLog::Log::Line->new( time => now, @args );
752 $note->{note} = 1; # force this to be marked as a note
753 my $io = $self->[IO];
754  
755 # check for day change
756 my ($last_ts) = $self->last_ts;
757 if ( !$last_ts || _different_day( $last_ts, $note->time ) ) {
758 $io->append(
759 App::JobLog::Log::Line->new( comment => $note->time->strftime(TS) )
760 )->append("\n");
761 }
762 $io->append($note)->append("\n");
763 $io->close; # flush contents
764 }
765  
766 # a test to determine whether two DateTime objects
767 # represent different days
768 sub _different_day {
769 my ( $d1, $d2 ) = @_;
770 return !( $d1->day == $d2->day
771 && $d1->month == $d2->month
772 && $d1->year == $d2->year );
773 }
774  
775  
776 sub close {
777 my ($self) = @_;
778 my $io = $self->[IO];
779 $io->close if $io && $io->is_open;
780 }
781  
782  
783 sub insert {
784 my ( $self, $index, @lines ) = @_;
785  
786 # silently return unless some content to insert has been provided
787 return unless @lines;
788 my $comment =
789 App::JobLog::Log::Line->new( comment => 'the following '
790 . ( @lines == 1 ? '' : scalar(@lines) . ' ' ) . 'line'
791 . ( @lines == 1 ? '' : 's' ) . ' ha'
792 . ( @lines == 1 ? 's' : 've' )
793 . ' been inserted by '
794 . __PACKAGE__
795 . ' rather than having been appended' );
796 splice @{ $self->[IO] }, $index, 0, $comment, @lines;
797 }
798  
799  
800 sub replace {
801 my ( $self, $index, $line ) = @_;
802 carp 'expected integer and log line'
803 unless $index =~ /^\d++$/ && ref $line eq 'App::JobLog::Log::Line';
804 $self->[IO][$index] = $line;
805 }
806  
807 1;
808  
809 __END__