metabot/extern/cel-bot/CelBot/Plugin/LogFile.pm
2021-05-20 17:59:36 -04:00

59 lines
1,015 B
Perl

package CelBot::Plugin::LogFile;
use strict;
use constant PLUGIN_TYPE => "logfile";
use base qw( CelBot::Plugin::LogBase );
use Date::Format qw( strftime );
use IO::Handle;
sub new
{
my $class = shift;
my ( $core, $config ) = @_;
my $file = $config->get_string( '@file' );
my $self = bless {
core => $core,
file => $file,
}, $class;
$self->open_file;
return $self;
}
sub open_file
{
my $self = shift;
undef $self->{fileh};
my $file = $self->{file};
if( -e $file ) {
unlink( "$file.old" ); # Ignore a failure
rename( $file, "$file.old" ); # Ignore a failure
}
open( my $fileh, ">", $file ) or die "Cannot write $file - $!\n";
$fileh->autoflush(1);
$self->{fileh} = $fileh;
}
sub do_log_really
{
my $self = shift;
my ( $subject, $message ) = @_;
my $timestamp = strftime( "%Y/%m/%d %H:%M:%S", @{[ localtime ]} );
$self->{fileh}->print( "[$timestamp] $subject: $message\n" );
}
# Keep perl happy, keep Britain tidy
1;