| 1 | ## Domain Registry Interface, Logging into files | ||||||||||||||||||||||||||||
| 2 | ## | ||||||||||||||||||||||||||||
| 3 | ## Copyright (c) 2009,2010,2013 Patrick Mevzek <netdri@dotandco.com>. All rights reserved. | ||||||||||||||||||||||||||||
| 4 | ## | ||||||||||||||||||||||||||||
| 5 | ## This file is part of Net::DRI | ||||||||||||||||||||||||||||
| 6 | ## | ||||||||||||||||||||||||||||
| 7 | ## Net::DRI is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 8 | ## it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 9 | ## the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||
| 10 | ## (at your option) any later version. | ||||||||||||||||||||||||||||
| 11 | ## | ||||||||||||||||||||||||||||
| 12 | ## See the LICENSE file that comes with this distribution for more details. | ||||||||||||||||||||||||||||
| 13 | ######################################################################################### | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | package Net::DRI::Logging::Files; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use base qw/Net::DRI::Logging/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Net::DRI::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use IO::Handle; ## needed for the autoflush method on any lexical $fh | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | #################################################################################################### | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub new | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 0 | my ($class,$data)=@_; | |||||||||||||||||||||||||||
| 31 | 0 | my $self=$class->SUPER::new($data); | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | if (! exists $self->{output_directory} || ! defined $self->{output_directory} ) { $self->{output_directory}='.'; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $self->{"output_directory"} and defined $self->{"output_directory"}
MC/DC0%0/2
exists $self->{"output_directory"} and defined $self->{"output_directory"}
exists $self->{"output_directory"}
defined $self->{"output_directory"}
Truth table0%0/3
A and B
A= exists $self->{"output_directory"}B= defined $self->{"output_directory"}
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if (! -d $self->{output_directory}) { Net::DRI::Exception->die(0,'logging',1,'Directory '.$self->{output_directory}.' does not exist'); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if (! -w $self->{output_directory}) { Net::DRI::Exception->die(0,'logging',2,'Directory '.$self->{output_directory}.' is not writable'); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $self->{fh}={}; | |||||||||||||||||||||||||||
| 36 | 0 | return $self; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub name { return 'files'; } | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub setup_channel | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
setup_channel: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 0 | my ($self,$source,$type,$data)=@_; | |||||||||||||||||||||||||||
| 44 | 0 | my $name=$self->generate_filename($type,$data); | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (exists $self->{fh}->{$name}) { return; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | my $fh; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | open $fh,'>>',$name or Net::DRI::Exception->die(0,'logging',3,'File '.$name.' can not be open for writing: '.$!); ## no critic (InputOutput::RequireBriefOpen) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $fh->autoflush(1); ## this is possible thanks to IO::Handle | |||||||||||||||||||||||||||
| 49 | 0 | $self->{fh}->{$name}=$fh; | |||||||||||||||||||||||||||
| 50 | 0 | return; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub output | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
output: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 0 | my ($self,$level,$type,$data)=@_; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | if (! $self->should_log($level)) { return; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | my $name=$self->generate_filename($type,$data); | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (! exists $self->{fh}->{$name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 0 | my $core=$self->generate_filename('core'); | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (exists $self->{fh}->{$core}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | $self->output('critical','core',sprintf('File "%s" (type "%s") has not been setup (no previous call to setup_channel or invalid type?), switching to "core" logging file',$name,$type)); | |||||||||||||||||||||||||||
| 64 | 0 | $name=$core; | |||||||||||||||||||||||||||
| 65 | } else | ||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 0 | Net::DRI::Exception->die(1,'logging',3,sprintf('File "%s" (type "%s") has not been setup (no previous call to setup_channel or invalid type?), and can not switch to "core" logging file',$name,$type)); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 0 | print { $self->{fh}->{$name} } $self->tostring($level,$type,$data),"\n"; | |||||||||||||||||||||||||||
| 71 | 0 | return; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | #################################################################################################### | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub generate_filename | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
generate_filename: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 0 | my ($self,$type,$ctx)=@_; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | return sprintf '%s/%s',$self->{output_directory},$self->{output_filename} if exists $self->{output_filename}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $name=(defined $ctx && ref $ctx eq 'HASH')? sprintf('%s-%s-%s',$ctx->{registry},$ctx->{profile},$type) : $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $ctx and ref $ctx eq "HASH"
MC/DC0%0/2
defined $ctx and ref $ctx eq "HASH"
defined $ctx
ref $ctx eq "HASH"
Truth table0%0/3
A and B
A= defined $ctxB= ref $ctx eq "HASH"
| |||||||||||||||||||||||||||||
| 81 | 0 | return sprintf '%s/%d-%s.log',$self->{output_directory},$$,$name; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub DESTROY | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 86 | 0 | my ($self)=@_; | |||||||||||||||||||||||||||
| 87 | 0 | foreach my $fh (values %{$self->{fh}}) | |||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | close $fh or 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | #################################################################################################### | ||||||||||||||||||||||||||||
| 95 | 1 | 1; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||