| 1 | package Net::SolarWinds::LogMethods; | ||||||||||
| 2 | |||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =pod | ||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Net::SolarWinds::LogMethods - Passive logging interfcaes | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | package MyClass; | ||||||||||
| 15 | |||||||||||
| 16 | use base qw(Net::SolarWinds::ConstructorHash Net::SolarWinds::LogMethods); | ||||||||||
| 17 | |||||||||||
| 18 | 1; | ||||||||||
| 19 | |||||||||||
| 20 | my $pkg=new MyClass(key=>'value'); | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 24 | This library provides a common logging interfcaes that expect Net::SolarWinds::Log or something that implements its features. It also assumes object instance is a hash with $self->{log} contains the logging object. | ||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | =head1 OO Methods provided | ||||||||||
| 28 | |||||||||||
| 29 | =over 4 | ||||||||||
| 30 | |||||||||||
| 31 | =item * my $log=$self->get_log; | ||||||||||
| 32 | |||||||||||
| 33 | Returns the logging object if any | ||||||||||
| 34 | |||||||||||
| 35 | =cut | ||||||||||
| 36 | |||||||||||
| 37 | 67 | ▶ | sub get_log { $_[0]->{log} } | ||||||||
|
Subroutine
get_log: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 38 | |||||||||||
| 39 | =item * $self->set_log(Net::SolarWinds::Log->new); | ||||||||||
| 40 | |||||||||||
| 41 | Sets the Chater::Log object, you can also set this in the constructor at object creation time. | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | sub set_log { $_[0]->{log} = $_[1] } | ||||||||
|
Subroutine
set_log: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 46 | |||||||||||
| 47 | =item * $self->log_error("Some error"); | ||||||||||
| 48 | |||||||||||
| 49 | This is a lazy man's wrapper function for | ||||||||||
| 50 | |||||||||||
| 51 | my $log=$self->get_log; | ||||||||||
| 52 | $log->log_error("Some error") if $log; | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 11 | ▶ | sub log_error { | ||||||||
|
Subroutine
log_error: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 57 | 11 | my ( $self, @args ) = @_; | |||||||||
| 58 | |||||||||||
| 59 | 11 | my $log = $self->get_log; | |||||||||
| 60 | |||||||||||
| 61 | 11 | ▶ | return 0 unless $log; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 11 | ▶ | return 1 unless $log->get_loglevel >= $log->LOG_ERROR; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | 9 | my @list = ('ERROR'); | |||||||||
| 66 | 9 | ▶ | push @list, $self->log_header if $self->can('log_header'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | |||||||||||
| 68 | 9 | return $log->write_to_log( @list, @args ); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | =item * $log->log_die("Log this and die"); | ||||||||||
| 72 | |||||||||||
| 73 | Logs the given message then dies. | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | 2 | ▶ | sub log_die { | ||||||||
|
Subroutine
log_die: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 78 | 2 | my ( $self, @args ) = @_; | |||||||||
| 79 | |||||||||||
| 80 | 2 | my $log = $self->get_log; | |||||||||
| 81 | 2 | my @list = ('DIE'); | |||||||||
| 82 | 2 | ▶ | push @list, $self->log_header if $self->can('log_header'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | |||||||||||
| 84 | 2 | my $string=$self->format_log(@list,@args); | |||||||||
| 85 | |||||||||||
| 86 | 2 | ▶ | return die $string unless $log; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | return 1 unless $log->get_loglevel >= $log->LOG_ALWAYS; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | 1 | $log->write_to_log( @list, @args ); | |||||||||
| 92 | 1 | die $string; | |||||||||
| 93 | |||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 2 | ▶ | sub format_log { | ||||||||
|
Subroutine
format_log: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 97 | 2 | my ($self,@args)=@_; | |||||||||
| 98 | |||||||||||
| 99 | 2 | ▶ | return join(' ',@args)."\n" unless $self->get_log; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 1 | return $self->get_log->format_log(@args); | |||||||||
| 101 | |||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | =item * $self->log_always("Some msg"); | ||||||||||
| 105 | |||||||||||
| 106 | This is a lazy man's wrapper function for | ||||||||||
| 107 | |||||||||||
| 108 | my $log=$self->get_log; | ||||||||||
| 109 | $log->log_always("Some msg") if $log; | ||||||||||
| 110 | |||||||||||
| 111 | =cut | ||||||||||
| 112 | |||||||||||
| 113 | 11 | ▶ | sub log_always { | ||||||||
|
Subroutine
log_always: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 114 | 11 | my ( $self, @args ) = @_; | |||||||||
| 115 | |||||||||||
| 116 | 11 | my $log = $self->get_log; | |||||||||
| 117 | |||||||||||
| 118 | 11 | ▶ | return 0 unless $log; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | |||||||||||
| 120 | 11 | ▶ | return 1 unless $log->get_loglevel >= $log->LOG_ALWAYS; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | |||||||||||
| 122 | 11 | my @list = ('ALWAYS'); | |||||||||
| 123 | 11 | ▶ | push @list, $self->log_header if $self->can('log_header'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | |||||||||||
| 125 | 11 | return $log->write_to_log( @list, @args ); | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | =item * my $string=$self->log_header; | ||||||||||
| 129 | |||||||||||
| 130 | This is a stub function that allows a quick addin for logging, the string returned will be inserted after the log_level in the log file if this function is created. | ||||||||||
| 131 | |||||||||||
| 132 | =cut | ||||||||||
| 133 | |||||||||||
| 134 | =item * $self->log_warn("Some msg"); | ||||||||||
| 135 | |||||||||||
| 136 | This is a lazy man's wrapper function for: | ||||||||||
| 137 | |||||||||||
| 138 | my $log=$self->get_log; | ||||||||||
| 139 | $log->log_warn("Some msg") if $log; | ||||||||||
| 140 | |||||||||||
| 141 | =cut | ||||||||||
| 142 | |||||||||||
| 143 | 11 | ▶ | sub log_warn { | ||||||||
|
Subroutine
log_warn: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 144 | 11 | my ( $self, @args ) = @_; | |||||||||
| 145 | |||||||||||
| 146 | 11 | my $log = $self->get_log; | |||||||||
| 147 | |||||||||||
| 148 | 11 | ▶ | return 0 unless $log; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | |||||||||||
| 150 | 11 | ▶ | return 1 unless $log->get_loglevel >= $log->LOG_WARN; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 7 | my @list = ('WARN'); | |||||||||
| 152 | 7 | ▶ | push @list, $self->log_header if $self->can('log_header'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | |||||||||||
| 154 | 7 | return $log->write_to_log( @list, @args ); | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | =item * $self->log_info("Some msg"); | ||||||||||
| 158 | |||||||||||
| 159 | This is a lazy man's wrapper function for: | ||||||||||
| 160 | |||||||||||
| 161 | my $log=$self->get_log; | ||||||||||
| 162 | $log->log_info("Some msg") if $log; | ||||||||||
| 163 | |||||||||||
| 164 | =cut | ||||||||||
| 165 | |||||||||||
| 166 | 12 | ▶ | sub log_info { | ||||||||
|
Subroutine
log_info: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 167 | 12 | my ( $self, @args ) = @_; | |||||||||
| 168 | |||||||||||
| 169 | 12 | my $log = $self->get_log; | |||||||||
| 170 | |||||||||||
| 171 | 12 | ▶ | return 0 unless $log; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | |||||||||||
| 173 | 12 | ▶ | return 1 unless $log->get_loglevel >= $log->LOG_INFO; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 6 | my @list = ('INFO'); | |||||||||
| 175 | 6 | ▶ | push @list, $self->log_header if $self->can('log_header'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | |||||||||||
| 177 | 6 | return $log->write_to_log( @list, @args ); | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | =item * $self->log_debug("Some msg"); | ||||||||||
| 181 | |||||||||||
| 182 | This is a lazy man's wrapper function for: | ||||||||||
| 183 | |||||||||||
| 184 | my $log=$self->get_log; | ||||||||||
| 185 | $log->log_debug("Some msg") if $log; | ||||||||||
| 186 | |||||||||||
| 187 | =cut | ||||||||||
| 188 | |||||||||||
| 189 | 15 | ▶ | sub log_debug { | ||||||||
|
Subroutine
log_debug: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 190 | 15 | my ( $self, @args ) = @_; | |||||||||
| 191 | |||||||||||
| 192 | 15 | my $log = $self->get_log; | |||||||||
| 193 | |||||||||||
| 194 | 15 | ▶ | return 0 unless $log; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | |||||||||||
| 196 | 11 | ▶ | return 1 unless $log->get_loglevel >= $log->LOG_DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | |||||||||||
| 198 | 3 | my @list = ('DEBUG'); | |||||||||
| 199 | 3 | ▶ | push @list, $self->log_header if $self->can('log_header'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | |||||||||||
| 201 | 3 | return $log->write_to_log( @list, @args ); | |||||||||
| 202 | } | ||||||||||
| 203 | |||||||||||
| 204 | =back | ||||||||||
| 205 | |||||||||||
| 206 | =head1 AUTHOR | ||||||||||
| 207 | |||||||||||
| 208 | Michael Shipper | ||||||||||
| 209 | |||||||||||
| 210 | =cut | ||||||||||
| 211 | |||||||||||
| 212 | 5 | 1; | |||||||||