lib/MGH_Biostat/TravEpi/SimpleRulesBase/Rotator.pm


package MGH_Biostat::TravEpi::SimpleRulesBase::Rotator;
use Modern::Perl '2013';
use Carp;
use POSIX ();
use Path::Tiny;
##use warnings FATAL => 'flock'; # (not available on production yet)
use Try::Tiny;

sub new {
    my $class = shift;
    my %args  = @_;

    # handle arguments
    confess('No file template') unless $args{'template'};

    # create myself
    my $self = bless {
        'template' => $args{'template'},
    }, $class;

    return $self;
}

# print data to the log file
sub print {
    my $self = shift;
    my @log  = @_;

    my $filename = POSIX::strftime( $self->{'template'}, localtime( time() ) );

    try {
        path($filename)->append_utf8(@log);
    }
    catch {
        warn( "Could not write data to $filename: " . join( "" => @log ) );
    };
}

1;