From 82f4962f66172c099a01cd32c44425f73fd60d03 Mon Sep 17 00:00:00 2001 From: Bron Gondwana Date: Tue, 19 Apr 2011 21:43:18 +0200 Subject: [PATCH] Connection: use USERDEBUGDIR environment variable to write XML DEBUG LOGs per user --- lib/DJabberd.pm | 5 +++++ lib/DJabberd/Connection.pm | 36 +++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/DJabberd.pm b/lib/DJabberd.pm index 215cb70..5da0ed1 100644 --- a/lib/DJabberd.pm +++ b/lib/DJabberd.pm @@ -138,6 +138,11 @@ sub set_config_unixdomainsocket { $self->{unixdomainsocket} = $val; } +sub set_config_userlogdir { + my ($self, $val) = @_; + $self->{userlogdir} = $val; +} + sub set_config_clientport { my ($self, $val) = @_; $self->{c2s_port} = as_bind_addr($val); diff --git a/lib/DJabberd/Connection.pm b/lib/DJabberd/Connection.pm index 7f944f7..62ea70c 100644 --- a/lib/DJabberd/Connection.pm +++ b/lib/DJabberd/Connection.pm @@ -49,17 +49,6 @@ our $hook_logger = DJabberd::Log->get_logger("DJabberd::Hook"); use constant POLLIN => 1; use constant POLLOUT => 4; -BEGIN { - my $xmldebug = $ENV{XMLDEBUG}; - - if ($xmldebug) { - eval 'use constant XMLDEBUG => "' . quotemeta($xmldebug) . '"'; - die "XMLDEBUG path '$xmldebug' needs to be a directory writable by the user you are running $0 as\n" unless -w $xmldebug; - } else { - eval "use constant XMLDEBUG => ''"; - } -} - our %LOGMAP; sub new { @@ -90,16 +79,19 @@ sub new { $self->log->debug("New connection '$self->{id}' from $fromip"); } - if (XMLDEBUG) { - system("mkdir -p " . XMLDEBUG ."/$$/"); + if ($ENV{XMLDEBUG}) { + my $path = "$ENV{XMLDEBUG}/$$"; + # XXX - escape the path? + system("mkdir -p $path"); my $handle = IO::Handle->new; no warnings; my $from = $fromip || "outbound"; - my $filename = "+>" . XMLDEBUG . "/$$/$from-$self->{id}"; + my $filename = "+>$path/$from-$self->{id}"; open ($handle, $filename) || die "Cannot open $filename: $!"; $handle->autoflush(1); $LOGMAP{$self} = $handle; } + return $self; } @@ -250,6 +242,16 @@ sub set_bound_jid { my ($self, $jid) = @_; die unless $jid && $jid->isa('DJabberd::JID'); $self->{bound_jid} = $jid; + return unless $ENV{USERDEBUGDIR}; + my $path = $ENV{USERDEBUGDIR} . "/" . $jid->as_bare_string(); + if (not exists $LOGMAP{$self} and -d $path) { + my $handle = IO::Handle->new; + no warnings; + my $filename = "+>$path/$$-$self->{id}"; + open ($handle, $filename) || die "Cannot open $filename: $!"; + $handle->autoflush(1); + $LOGMAP{$self} = $handle; + } } sub set_to_host { @@ -466,7 +468,7 @@ sub restart_stream { # eval is being annoying sub write { my $self = shift; - if (XMLDEBUG) { + if (exists $LOGMAP{$self}) { my $time = Time::HiRes::time; no warnings; my $data = $_[0]; @@ -533,7 +535,7 @@ sub event_read { Carp::confess if ($self->{closed}); - if (XMLDEBUG) { + if (exists $LOGMAP{$self}) { my $time = Time::HiRes::time; $LOGMAP{$self}->print("$time\t< $$bref\n"); } @@ -808,7 +810,7 @@ sub close { $self->{parser} = undef; }); } - if (XMLDEBUG) { + if (exists $LOGMAP{$self}) { $LOGMAP{$self}->close; delete $LOGMAP{$self}; }