diff options
| author | Darkelarious <darkelarious@333networks.com> | 2015-02-09 07:58:06 +0100 |
|---|---|---|
| committer | Darkelarious <darkelarious@333networks.com> | 2015-02-09 07:58:06 +0100 |
| commit | 5057ec47aa9a1702b2483e0a0b3ba325bb0b7abb (patch) | |
| tree | b1a394f64ec7ec2cf69f33bceb54a5b51199c0a4 /lib/MasterServer/Core/Logging.pm | |
| parent | 6ac9d390e417b868b6ed79441b8cc6e1b2ebeb13 (diff) | |
| download | MasterServer-Perl-5057ec47aa9a1702b2483e0a0b3ba325bb0b7abb.tar.gz MasterServer-Perl-5057ec47aa9a1702b2483e0a0b3ba325bb0b7abb.zip | |
receive UDP beacons, validate them and store with country indicator
Diffstat (limited to 'lib/MasterServer/Core/Logging.pm')
| -rwxr-xr-x | lib/MasterServer/Core/Logging.pm | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/MasterServer/Core/Logging.pm b/lib/MasterServer/Core/Logging.pm new file mode 100755 index 0000000..efd2123 --- /dev/null +++ b/lib/MasterServer/Core/Logging.pm @@ -0,0 +1,66 @@ + +package MasterServer::Core::Logging; + +use strict; +use warnings; +use POSIX qw/strftime/; +use Exporter 'import'; + +our @EXPORT = qw| log |; + +################################################################################ +# +# Log to file and print to screen. +# args: $self, message_type, message +# +################################################################################ +sub log { + my ($self, $type, $msg) = @_; + + # parse time of log entry and prep for rotating log + my $time = strftime('%Y-%m-%d %H:%M:%S',localtime); + my $yearly = strftime('-%Y',localtime); + my $monthly = strftime('-%Y-%m',localtime); + my $weekly = strftime('-%Y-week%U',localtime); + my $daily = strftime('-%Y-%m-%d',localtime); + + # is the message suppressed in config? + if (defined $type && $self->{suppress} =~ m/$type/i){ + print "[$time] [SUPPRESSED] [$type] $msg\n"; #FIXME + return; # return if <$> + } + + # determine filename + my $f = "MasterServer-333networks"; + + # rotate log filename according to config + $f .= $daily if ($self->{log_rotate} =~ /^daily$/i ); + $f .= $weekly if ($self->{log_rotate} =~ /^weekly$/i ); + $f .= $monthly if ($self->{log_rotate} =~ /^monthly$/i ); + $f .= $yearly if ($self->{log_rotate} =~ /^yearly$/i ); + $f .= ".log"; + + # put log filename together + my $logfile = $self->{log_dir}.((substr($self->{log_dir},-1) eq "/")?"":"/").$f; + + print "[$time] [$type] > $msg\n" if $self->{printlog}; + + # temporarily disable the warnings-to-log, to avoid infinite recursion if + # this function throws a warning. + my $old = $SIG{__WARN__}; + $SIG{__WARN__} = undef; + + chomp $msg; + $msg =~ s/\n/\n | /g; + if($logfile && open my $F, '>>:utf8', $logfile) { + flock $F, 2; + seek $F, 0, 2; + print $F "[$time]\t[$type]\t$msg\n"; + flock $F, 4; + close $F; + } + $SIG{__WARN__} = $old; +} + + +1; |
