| 1 | ###################################################################### | ||
| 2 | # Synchronized.pm -- 2003, 2007 Mike Schilli <m@perlmeister.com> | ||
| 3 | ###################################################################### | ||
| 4 | # Special appender employing a locking strategy to synchronize | ||
| 5 | # access. | ||
| 6 | ###################################################################### | ||
| 7 | |||
| 8 | ########################################### | ||
| 9 | package Log::Log4perl::Appender::Synchronized; | ||
| 10 | ########################################### | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | use Log::Log4perl::Util::Semaphore; | ||
| 15 | |||
| 16 | our @ISA = qw(Log::Log4perl::Appender); | ||
| 17 | |||
| 18 | our $VERSION = '1.53'; | ||
| 19 | |||
| 20 | ########################################### | ||
| 21 | sub new { | ||
| 22 | ########################################### | ||
| 23 | my($class, %options) = @_; | ||
| 24 | |||
| 25 | my $self = { | ||
| 26 | appender=> undef, | ||
| 27 | key => '_l4p', | ||
| 28 | level => 0, | ||
| 29 | %options, | ||
| 30 | }; | ||
| 31 | |||
| 32 | my @values = (); | ||
| 33 | for my $param (qw(uid gid mode destroy key)) { | ||
| 34 | push @values, $param, $self->{$param} if defined $self->{$param}; | ||
| 35 | } | ||
| 36 | |||
| 37 | $self->{sem} = Log::Log4perl::Util::Semaphore->new( | ||
| 38 | @values | ||
| 39 | ); | ||
| 40 | |||
| 41 | # Pass back the appender to be synchronized as a dependency | ||
| 42 | # to the configuration file parser | ||
| 43 | push @{$options{l4p_depends_on}}, $self->{appender}; | ||
| 44 | |||
| 45 | # Run our post_init method in the configurator after | ||
| 46 | # all appenders have been defined to make sure the | ||
| 47 | # appender we're synchronizing really exists | ||
| 48 | push @{$options{l4p_post_config_subs}}, sub { $self->post_init() }; | ||
| 49 | |||
| 50 | bless $self, $class; | ||
| 51 | } | ||
| 52 | |||
| 53 | ########################################### | ||
| 54 | sub log { | ||
| 55 | ########################################### | ||
| 56 | my($self, %params) = @_; | ||
| 57 | |||
| 58 | $self->{sem}->semlock(); | ||
| 59 | |||
| 60 | # Relay that to the SUPER class which needs to render the | ||
| 61 | # message according to the appender's layout, first. | ||
| 62 | $Log::Log4perl::caller_depth +=2; | ||
| 63 | $self->{app}->SUPER::log(\%params, | ||
| 64 | $params{log4p_category}, | ||
| 65 | $params{log4p_level}); | ||
| 66 | $Log::Log4perl::caller_depth -=2; | ||
| 67 | |||
| 68 | $self->{sem}->semunlock(); | ||
| 69 | } | ||
| 70 | |||
| 71 | ########################################### | ||
| 72 | sub post_init { | ||
| 73 | ########################################### | ||
| 74 | my($self) = @_; | ||
| 75 | |||
| 76 | if(! exists $self->{appender}) { | ||
| 77 | die "No appender defined for " . __PACKAGE__; | ||
| 78 | } | ||
| 79 | |||
| 80 | my $appenders = Log::Log4perl->appenders(); | ||
| 81 | my $appender = Log::Log4perl->appenders()->{$self->{appender}}; | ||
| 82 | |||
| 83 | if(! defined $appender) { | ||
| 84 | die "Appender $self->{appender} not defined (yet) when " . | ||
| 85 | __PACKAGE__ . " needed it"; | ||
| 86 | } | ||
| 87 | |||
| 88 | $self->{app} = $appender; | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ |