From 3b4f051cb67ebd89f0c2b13d5be6f6d6fed395d2 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 | 60 +++------------------------------------------ C4/SIP/Sip.pm | 4 ++- 3 files changed, 53 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 2b73b984b2..e3233f3b35 100755 --- a/C4/SIP/SIPServer.pm +++ b/C4/SIP/SIPServer.pm @@ -17,6 +17,7 @@ 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::Logger; use C4::SIP::Trapper; @@ -85,9 +86,6 @@ __PACKAGE__ ->run(@parms); # Child # -my $activeSIPServer; -my $activeLogger; - # process_request is the callback used by Net::Server to handle # an incoming connection request. @@ -100,7 +98,7 @@ sub process_request { $self->{config} = $config; $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 ); @@ -171,7 +169,7 @@ sub raw_transport { } alarm 0; - $self->{logger} = _set_logger( + $self->{logger} = set_logger( Koha::Logger->get( { interface => 'sip', @@ -421,58 +419,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 9146ebcd77..79474920b2 100644 --- a/C4/SIP/Sip.pm +++ b/C4/SIP/Sip.pm @@ -14,6 +14,7 @@ use IO::Handle; 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); @@ -204,7 +205,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 (Apple Git-117)