Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection: add USERDEBUGDIR environment variable #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/DJabberd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 19 additions & 17 deletions lib/DJabberd/Connection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -808,7 +810,7 @@ sub close {
$self->{parser} = undef;
});
}
if (XMLDEBUG) {
if (exists $LOGMAP{$self}) {
$LOGMAP{$self}->close;
delete $LOGMAP{$self};
}
Expand Down