From 05e44a67f6a4ccf67d3590349a5029208c5a5b0e Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Mon, 22 Apr 2019 11:02:42 -0400 Subject: [PATCH] Bug 15253: Move subs to a new module, allow unit tests to function --- C4/SIP/Logger.pm | 47 ++++++++++++++++++++++++++++++++++ C4/SIP/SIPServer.pm | 61 +++------------------------------------------ C4/SIP/Sip.pm | 4 ++- 3 files changed, 54 insertions(+), 58 deletions(-) create mode 100644 C4/SIP/Logger.pm diff --git a/C4/SIP/Logger.pm b/C4/SIP/Logger.pm new file mode 100644 index 0000000000..8eff6826e2 --- /dev/null +++ b/C4/SIP/Logger.pm @@ -0,0 +1,47 @@ +#!/usr/bin/perl +package C4::SIP::Logger; + +use Modern::Perl; + +our $activeSIPServer; +our $activeLogger; + +=head2 get_SIPServer + + my $sipServer = C4::SIP::SIPServer::get_SIPServer() + +@RETURNS C4::SIP::SIPServer, the current server's child-process used to handle this SIP-transaction + +=cut + +sub get_SIPServer { + return $activeSIPServer; +} + +sub _set_SIPServer { + my ($sipServer) = @_; + $activeSIPServer = $sipServer; + return $activeSIPServer; +} + +=head2 get_logger + + my $logger = C4::SIP::SIPServer::get_logger() + +@RETURNS Koha::Logger, the logger used to log this SIP-transaction + +=cut + +sub get_logger { + return $activeLogger; +} + +sub set_logger { + my ($logger) = @_; + $activeLogger = $logger; + return $activeLogger; +} + +1; + +__END__ diff --git a/C4/SIP/SIPServer.pm b/C4/SIP/SIPServer.pm index 90484e4391..b2c2539c9d 100755 --- a/C4/SIP/SIPServer.pm +++ b/C4/SIP/SIPServer.pm @@ -17,6 +17,8 @@ use C4::SIP::Sip::Constants qw(:all); use C4::SIP::Sip::Configuration; use C4::SIP::Sip::Checksum qw(checksum verify_cksum); use C4::SIP::Sip::MsgType qw( handle login_core ); +use C4::SIP::Logger qw(set_logger); + use Koha::Caches; use Koha::Logger; @@ -86,9 +88,6 @@ __PACKAGE__ ->run(@parms); # Child # -my $activeSIPServer; -my $activeLogger; - # process_request is the callback used by Net::Server to handle # an incoming connection request. @@ -104,7 +103,7 @@ sub process_request { Koha::Caches->flush_L1_caches(); $self->{account} = undef; # Clear out the account from the last request, it may be different - $self->{logger} = _set_logger( Koha::Logger->get( { interface => 'sip' } ) ); + $self->{logger} = set_logger( Koha::Logger->get( { interface => 'sip' } ) ); # Flush previous MDCs to prevent accidentally leaking incorrect MDC-entries Log::Log4perl::MDC->put( "accountid", undef ); @@ -175,7 +174,7 @@ sub raw_transport { } alarm 0; - $self->{logger} = _set_logger( + $self->{logger} = set_logger( Koha::Logger->get( { interface => 'sip', @@ -425,58 +424,6 @@ sub get_timeout { } } -=head2 get_SIPServer - - my $sipServer = C4::SIP::SIPServer::get_SIPServer() - -@RETURNS C4::SIP::SIPServer, the current server's child-process used to handle this SIP-transaction - -=cut - -sub get_SIPServer { - unless($activeSIPServer) { - my @cc = caller(1); - die "$cc[3]() asks for \$activeSIPServer, but he is not defined yet"; - } - return $activeSIPServer; -} - -sub _set_SIPServer { - my ($sipServer) = @_; - unless (blessed($sipServer) && $sipServer->isa('C4::SIP::SIPServer')) { - my @cc = caller(0); - die "$cc[3]():> \$sipServer '$sipServer' is not a C4::SIP::SIPServer-object"; - } - $activeSIPServer = $sipServer; - return $activeSIPServer; -} - -=head2 get_logger - - my $logger = C4::SIP::SIPServer::get_logger() - -@RETURNS Koha::Logger, the logger used to log this SIP-transaction - -=cut - -sub get_logger { - unless($activeLogger) { - my @cc = caller(1); - die "$cc[3]() asks for \$activeLogger, but he is not defined yet"; - } - return $activeLogger; -} - -sub _set_logger { - my ($logger) = @_; - unless (blessed($logger) && $logger->isa('Koha::Logger')) { - my @cc = caller(0); - die "$cc[3]():> \$logger '$logger' is not a Koha::Logger-object"; - } - $activeLogger = $logger; - return $activeLogger; -} - 1; __END__ diff --git a/C4/SIP/Sip.pm b/C4/SIP/Sip.pm index 01c0046cf3..1efa23b7a6 100644 --- a/C4/SIP/Sip.pm +++ b/C4/SIP/Sip.pm @@ -15,6 +15,7 @@ use List::Util qw(first); use C4::SIP::Sip::Constants qw(SIP_DATETIME FID_SCREEN_MSG); use C4::SIP::Sip::Checksum qw(checksum); +use C4::SIP::Logger qw(get_logger); use base qw(Exporter); @@ -216,7 +217,8 @@ sub syslog { my $message = @args ? sprintf($mask, @args) : $mask; - C4::SIP::SIPServer::get_logger()->$method($message); + my $logger = C4::SIP::Logger::get_logger(); + $logger->$method($message) if $logger; } 1; -- 2.20.1