<commit_before>
=head1 NAME

Log::Fine::Handle::File::Timestamp - Output log messages to time-stamped files

=head1 SYNOPSIS

Provides logging to a time-stamped file

    use Log::Fine;
    use Log::Fine::Handle::File::Timestamp;

    # Get a new logger
    my $log = Log::Fine->getLogger("foo");

    # register a file handle (default values shown)
    my $handle = Log::Fine::Handle::File::Timestamp
        ->new( name => 'file1',
               mask => LOGMASK_EMERG | LOGMASK_ALERT | LOGMASK_CRIT | LOGMASK_ERR | LOGMASK_WARNING | LOGMASK_NOTICE | LOGMASK_INFO,
               dir  => "/var/log",
               file => "myapp.%y%m%d.log" );

    # Register the handle
    $log->registerHandle($handle);

    # Log something
    $log->(INFO, "Opened new log handle");


=head1 DESCRIPTION

Log::Fine::Handle::File::Timestamp, aside from having a ridiculously
long name, provides logging to a time-stamped file.  Usage is similar
to L<Log::Fine::Handle::File> with the exception that the file name
can take an L<strftime(3)-compatible|strftime> string.

=cut

use strict;
use warnings;

package Log::Fine::Handle::File::Timestamp;

use base qw( Log::Fine::Handle::File );

#use Data::Dumper;
use File::Spec::Functions;
use FileHandle;
use POSIX qw( strftime );

our $VERSION = $Log::Fine::Handle::File::VERSION;

=head1 OVERRIDDEN METHODS

=head2 fileHandle

See L<Log::Fine::Handle::File/fileHandle>

=cut

sub fileHandle
{

        my $self = shift;

        # Return if we have a registered filehandle and the date is
        # still the same
        return $self->{_filehandle}
            if (    not $self->_fileRotate()
                and defined $self->{_filehandle}
                and ref $self->{_filehandle}
                and UNIVERSAL::can($self->{_filehandle}, 'isa')
                and $self->{_filehandle}->isa("IO::File")
                and defined fileno($self->{_filehandle}));

        # We need a new file.  Close our filehandle if it exists
        $self->{_filehandle}->close()
            if (    defined $self->{_filehandle}
                and ref $self->{_filehandle}
                and UNIVERSAL::can($self->{_filehandle}, 'isa')
                and $self->{_filehandle}->isa("IO::File")
                and defined fileno($self->{_filehandle}));

        # Generate file name
        my $filename = catdir($self->{dir}, $self->{_expanded_filename});

        # Generate a new filehandle
        $self->{_filehandle} = FileHandle->new(">> " . $filename);

        $self->_error("Unable to open log file $filename : $!\n")
            unless defined $self->{_filehandle};

        # Set autoflush if necessary
        $self->{_filehandle}->autoflush($self->{autoflush});

        return $self->{_filehandle};

}          # fileHandle();

# --------------------------------------------------------------------

##<commit_msg>Call _error() if we are unable to close the file handle<commit_after>
=head1 NAME

Log::Fine::Handle::File::Timestamp - Output log messages to time-stamped files

=head1 SYNOPSIS

Provides logging to a time-stamped file

    use Log::Fine;
    use Log::Fine::Handle::File::Timestamp;

    # Get a new logger
    my $log = Log::Fine->getLogger("foo");

    # register a file handle (default values shown)
    my $handle = Log::Fine::Handle::File::Timestamp
        ->new( name => 'file1',
               mask => LOGMASK_EMERG | LOGMASK_ALERT | LOGMASK_CRIT | LOGMASK_ERR | LOGMASK_WARNING | LOGMASK_NOTICE | LOGMASK_INFO,
               dir  => "/var/log",
               file => "myapp.%y%m%d.log" );

    # Register the handle
    $log->registerHandle($handle);

    # Log something
    $log->(INFO, "Opened new log handle");


=head1 DESCRIPTION

Log::Fine::Handle::File::Timestamp, aside from having a ridiculously
long name, provides logging to a time-stamped file.  Usage is similar
to L<Log::Fine::Handle::File> with the exception that the file name
can take an L<strftime(3)-compatible|strftime> string.

=cut

use strict;
use warnings;

package Log::Fine::Handle::File::Timestamp;

use base qw( Log::Fine::Handle::File );

#use Data::Dumper;
use File::Spec::Functions;
use FileHandle;
use POSIX qw( strftime );

our $VERSION = $Log::Fine::Handle::File::VERSION;

=head1 OVERRIDDEN METHODS

=head2 fileHandle

See L<Log::Fine::Handle::File/fileHandle>

=cut

sub fileHandle
{

        my $self = shift;

        # Return if we have a registered filehandle and the date is
        # still the same
        return $self->{_filehandle}
            if (    not $self->_fileRotate()
                and defined $self->{_filehandle}
                and ref $self->{_filehandle}
                and UNIVERSAL::can($self->{_filehandle}, 'isa')
                and $self->{_filehandle}->isa("IO::File")
                and defined fileno($self->{_filehandle}));

        # We need a new file.  Close our filehandle if it exists
        if (     defined $self->{_filehandle}
             and ref $self->{_filehandle}
             and UNIVERSAL::can($self->{_filehandle}, 'isa')
             and $self->{_filehandle}->isa("IO::File")
             and defined fileno($self->{_filehandle})) {

                $self->_error(
                              sprintf("Unable to close file handle to %s : %s",
                                      $self->{_expanded_filename}, $!
                              )) unless $self->{_filehandle}->close();

        }

        # Generate file name
        my $filename = catdir($self->{dir}, $self->{_expanded_filename});

        # Generate a new filehandle
        $self->{_filehandle} = FileHandle->new(">> " . $filename);

        $self->_error("Unable to open log file $filename : $!\n")
            unless defined $self->{_filehandle};

        # Set autoflush if necessary
        $self->{_filehandle}->autoflush($self->{autoflush});

        return $self->{_filehandle};

}          # fileHandle();

# --------------------------------------------------------------------

##
{'fraction_non_alphanumeric': 0.15813253012048192, 'fraction_numerical': 0.0023427041499330657, 'mean_word_length': 2.3960227272727272, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2961620', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1746, 'n_words': 473}