| 1 | package Log::Tiny; | ||||||||||||
| 2 | |||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 5 | ▶ | use vars qw($AUTOLOAD $VERSION $errstr %formats); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | =head1 NAME | ||||||||||||
| 7 | |||||||||||||
| 8 | Log::Tiny - Log data with as little code as possible | ||||||||||||
| 9 | |||||||||||||
| 10 | =head1 VERSION | ||||||||||||
| 11 | |||||||||||||
| 12 | Version 1.0 | ||||||||||||
| 13 | |||||||||||||
| 14 | =cut | ||||||||||||
| 15 | |||||||||||||
| 16 | 5 | $VERSION = '1.0'; | |||||||||||
| 17 | 5 | $errstr = ''; | |||||||||||
| 18 | |||||||||||||
| 19 | %formats = ( | ||||||||||||
| 20 | 15 | ▶ | c => [ 's', sub { shift }, ], # category: AUTOLOAD | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 0 | ▶ | C => [ 's', sub { lc shift }, ], # lcategory: AUTOLOAD lc | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 22 | 0 | ▶ | f => [ 's', sub { $0 }, ], # program_file: $0 | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 23 | 0 | ▶ | F => [ 's', sub { (caller(2))[1] }, ], # caller_file: caller | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 24 | 0 | ▶ | g => [ 's', sub { scalar gmtime }, ], # gmtime: scalar gmtime | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 25 | 0 | ▶ | L => [ 'd', sub { (caller(2))[2] }, ], # caller_line: caller | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 26 | 13 | ▶ | m => [ 's', sub { shift; shift }, ], # message: args | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | 0 | ▶ | n => [ 's', sub { $/ }, ], # newline: $/ | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 28 | 0 | ▶ | o => [ 's', sub { $^O }, ], # osname: $^O | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 29 | 0 | ▶ | p => [ 'd', sub { $$ }, ], # pid: $$ | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 30 | 1 | ▶ | P => [ 's', sub { (caller(2))[0] }, ], # caller_pkg: caller | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | 0 | ▶ | r => [ 'd', sub { time - $^T }, ], # runtime: $^T | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 32 | 0 | ▶ | S => [ 's', \&__format_S, sub { my $t = (caller(2))[3]; }, ], # caller_sub: caller | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 33 | 0 | ▶ | t => [ 's', sub { scalar localtime }, ],# localtime: scalar localtime | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 34 | 0 | ▶ | T => [ 'd', sub { time }, ], # unix_time: time | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 35 | 0 | ▶ | u => [ 'd', sub { $> }, ], # effective_uid: $> | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 36 | 0 | ▶ | U => [ 'd', sub { $< }, ], # real_uid: $< | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 37 | 0 | ▶ | v => [ 'd', sub { $] }, ], # long_perl_ver: $] | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 38 | 0 | ▶ | V => [ 's', sub { sprintf("%vd", $^V) }, ], # short_perl_ver | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 39 | ); | ||||||||||||
| 40 | |||||||||||||
| 41 | 1 | ▶ | sub __format_S { my $t = (caller(2))[3]; if ( $t eq 'Log::Tiny::AUTOLOAD' ) { $t = 'main'; }; $t; } | ||||||||||
|
Subroutine
__format_S: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||
| 42 | |||||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||||
| 44 | |||||||||||||
| 45 | This module aims to be a light-weight implementation | ||||||||||||
| 46 | *similiar* to L<Log::Log4perl> for logging data to a file. | ||||||||||||
| 47 | |||||||||||||
| 48 | Its use is very straight forward: | ||||||||||||
| 49 | |||||||||||||
| 50 | use Log::Tiny; | ||||||||||||
| 51 | |||||||||||||
| 52 | my $log = Log::Tiny->new( 'myapp.log' ) or | ||||||||||||
| 53 | die 'Could not log! (' . Log::Tiny->errstr . ')'; | ||||||||||||
| 54 | |||||||||||||
| 55 | foreach ( 1 .. 20 ) { | ||||||||||||
| 56 | $log->DEBUG( "Performing extensive computations on $_" ) if $DEBUG; | ||||||||||||
| 57 | unless ( extensively_compute( $_ ) ) { | ||||||||||||
| 58 | $log->WARN( | ||||||||||||
| 59 | "Investigating error (this may take a while)..." | ||||||||||||
| 60 | ); | ||||||||||||
| 61 | $log->ERROR( find_error() ); | ||||||||||||
| 62 | save_state(); | ||||||||||||
| 63 | exit 1; | ||||||||||||
| 64 | } else { | ||||||||||||
| 65 | $log->INFO( "Everything's A-OK!" ); | ||||||||||||
| 66 | } | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | =head1 FUNCTIONS | ||||||||||||
| 70 | |||||||||||||
| 71 | =head2 new | ||||||||||||
| 72 | |||||||||||||
| 73 | Create a new Log::Tiny object. You must define a log file | ||||||||||||
| 74 | to append to, and, optionally, a format. | ||||||||||||
| 75 | |||||||||||||
| 76 | =cut | ||||||||||||
| 77 | |||||||||||||
| 78 | sub new { | ||||||||||||
| 79 | 3 | ▶ | my $pkg = shift; | ||||||||||
|
Subroutine
new: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||
| 80 | 3 | ▶ | my $logfile = shift || return _error('No logfile provided'); | ||||||||||
|
Condition50%1/2
shift() || (return _error('No logfile provided'))
MC/DC0%0/1
shift() || (return _error('No logfile provided'))
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 81 | 3 | ▶ | my $format = shift || '[%t] %f:%p (%c) %m%n'; | ||||||||||
|
Condition50%1/2
shift() || '[%t] %f:%p (%c) %m%n'
MC/DC0%0/1
shift() || '[%t] %f:%p (%c) %m%n'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 82 | 3 | ▶ | open (my $logfh, '>>' . $logfile ) || | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | return _error( "Could not open $logfile: $!" ); | ||||||||||||
| 84 | 3 | my $self = bless { | |||||||||||
| 85 | format => $format, | ||||||||||||
| 86 | methods_only => [], | ||||||||||||
| 87 | logfile => $logfile, | ||||||||||||
| 88 | logfh => $logfh, | ||||||||||||
| 89 | }, $pkg; | ||||||||||||
| 90 | 3 | $self->format(); | |||||||||||
| 91 | 3 | return $self; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | =head2 format | ||||||||||||
| 95 | |||||||||||||
| 96 | You may, at any time, change the format. The log format is | ||||||||||||
| 97 | similiar in style to the sprintf you know and love; and, as | ||||||||||||
| 98 | a peek inside the source of this module will tell you, sprintf | ||||||||||||
| 99 | is used internally. However, be advised that these log formats | ||||||||||||
| 100 | B<are not sprintf>. | ||||||||||||
| 101 | |||||||||||||
| 102 | Interpolated data are specified by an percent sign (C< % >), | ||||||||||||
| 103 | followed by a character. A literal percent sign can be | ||||||||||||
| 104 | specified via two in succession ( C< %% > ). You may use any | ||||||||||||
| 105 | of the formatting attributes as noted in L<perlfunc>, under | ||||||||||||
| 106 | "sprintf" (C<perldoc -f sprintf>). | ||||||||||||
| 107 | |||||||||||||
| 108 | Internally, the format routine uses a data structure (hash) | ||||||||||||
| 109 | that can be seen near the beggining of this package. Any | ||||||||||||
| 110 | unrecognized interpolation variables will be returned | ||||||||||||
| 111 | literally. This means that, assuming $format{d} does not | ||||||||||||
| 112 | exist, "%d" in your format will result in "%d" being outputted | ||||||||||||
| 113 | to the log file. No interpolation will occur. | ||||||||||||
| 114 | |||||||||||||
| 115 | You may, of course, decide to modify the format data structure. | ||||||||||||
| 116 | I have done my best to ensure a wide range of variables for your | ||||||||||||
| 117 | usage, however. They are (currently) as follows: | ||||||||||||
| 118 | |||||||||||||
| 119 | c => category => The method called (see below for more info) | ||||||||||||
| 120 | C => lcategory => lowercase category | ||||||||||||
| 121 | f => program_file => Value of $0 | ||||||||||||
| 122 | F => caller_file => Calling file | ||||||||||||
| 123 | g => gmtime => Output of scalar L<gmtime> (localized date string) | ||||||||||||
| 124 | L => caller_line => Calling line | ||||||||||||
| 125 | m => message => Message sent to the log method | ||||||||||||
| 126 | n => newline => Value of $/ | ||||||||||||
| 127 | o => osname => Value of $^O | ||||||||||||
| 128 | p => pid => Value of $$ | ||||||||||||
| 129 | P => caller_pkg => Calling package | ||||||||||||
| 130 | r => runtime => Seconds the current process has been running for | ||||||||||||
| 131 | S => caller_sub => Calling subroutine | ||||||||||||
| 132 | t => localtime => Output of scalar L<localtime> (localized date | ||||||||||||
| 133 | string) | ||||||||||||
| 134 | T => unix_time => Time since epoch (L<time>) | ||||||||||||
| 135 | u => effective_uid => Value of $> | ||||||||||||
| 136 | U => real_uid => Value of $< | ||||||||||||
| 137 | v => long_perl_ver => Value of $] (5.008008) | ||||||||||||
| 138 | V => short_perl_ver => A "short" string for the version ("5.8.8") | ||||||||||||
| 139 | |||||||||||||
| 140 | See L<perlvar> for information on the used global variables, and | ||||||||||||
| 141 | L<perlfunc> (under "caller") or C<perldoc -f caller> for information | ||||||||||||
| 142 | on the "calling" variables. Oh, and make sure you add %n if you want | ||||||||||||
| 143 | newines. | ||||||||||||
| 144 | |||||||||||||
| 145 | =cut | ||||||||||||
| 146 | |||||||||||||
| 147 | sub format { | ||||||||||||
| 148 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
format: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 149 | 3 | ▶ | if ( $_[0] ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | 0 | $self->{format} = shift; | |||||||||||
| 151 | } | ||||||||||||
| 152 | 3 | $self->{args} = []; | |||||||||||
| 153 | # make real format | ||||||||||||
| 154 | 3 | my $format = join '', keys %formats; | |||||||||||
| 155 | $self->{format} =~ | ||||||||||||
| 156 | 3 | s/%(-?\d*(?:\.\d+)?)([$format])/_replace($self, $1, $2);/gex; | |||||||||||
| 157 | # thanks, mschilli | ||||||||||||
| 158 | 3 | return $self->{format}; | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | sub _replace { | ||||||||||||
| 162 | 6 | ▶ | my ( $self, $num, $op ) = @_; | ||||||||||
|
Subroutine
_replace: called CC=3 SCAR=11.2
| |||||||||||||
| 163 | 6 | ▶ | return '%%' if $op eq '%'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 164 | 6 | ▶ | return "%%$op" unless defined $formats{$op}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 165 | 6 | push @{ $self->{args} }, $op; | |||||||||||
| 166 | 6 | return "%$num" . $formats{$op}->[ 0 ]; | |||||||||||
| 167 | } | ||||||||||||
| 168 | |||||||||||||
| 169 | =head2 WHATEVER_YOU_WANT (log a message) | ||||||||||||
| 170 | |||||||||||||
| 171 | This method is whatever you want it to be. Any method called | ||||||||||||
| 172 | on a Log::Tiny object that is not reserved will be considered | ||||||||||||
| 173 | an attempt to log in the category named the same as the method | ||||||||||||
| 174 | that was caleld. Currently, only in-use methods are reserved; | ||||||||||||
| 175 | However, to account for expansion, please only use uppercase | ||||||||||||
| 176 | categories. See formats above for information on customizing | ||||||||||||
| 177 | the log messages. | ||||||||||||
| 178 | |||||||||||||
| 179 | =cut | ||||||||||||
| 180 | |||||||||||||
| 181 | sub AUTOLOAD { | ||||||||||||
| 182 | 17 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
AUTOLOAD: called CC=7 SCAR=19.5
| |||||||||||||
| 183 | 17 | my $method = $AUTOLOAD; | |||||||||||
| 184 | 17 | $method =~ s/.*:://; | |||||||||||
| 185 | 17 | ▶ | return _error( "Log routine ($method) is not a class method" ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 186 | unless defined ref $self; | ||||||||||||
| 187 | 17 | ▶ | if (@{ $self->{methods_only} }) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | 3 | my $in = 0; | |||||||||||
| 189 | 3 | foreach (@{ $self->{methods_only} }) { | |||||||||||
| 190 | 6 | ▶ | $in++ if uc $method eq uc $_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 191 | } | ||||||||||||
| 192 | 3 | ▶ | return _error( "Log category '$method' not in whitelist" ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 193 | unless $in; | ||||||||||||
| 194 | } | ||||||||||||
| 195 | 16 | my $tmp = ''; | |||||||||||
| 196 | $tmp .= sprintf ( | ||||||||||||
| 197 | $self->{format}, | ||||||||||||
| 198 | $self->_mk_args( $method, $_ ), | ||||||||||||
| 199 | 16 | ) foreach @_; | |||||||||||
| 200 | 16 | my $ret; | |||||||||||
| 201 | { | ||||||||||||
| 202 | 16 | my $autoflush = $|++; | |||||||||||
| 203 | 16 | $ret = print {$self->{logfh}} $tmp; | |||||||||||
| 204 | 16 | $| = $autoflush; | |||||||||||
| 205 | } | ||||||||||||
| 206 | 16 | return $ret; | |||||||||||
| 207 | } | ||||||||||||
| 208 | |||||||||||||
| 209 | sub _mk_args { | ||||||||||||
| 210 | 16 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_mk_args: called CC=4 SCAR=13.9
| |||||||||||||
| 211 | 16 | my ( $method, $msg ) = @_; | |||||||||||
| 212 | 16 | ▶ | $msg = '' unless defined $msg; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 213 | 16 | my @ret = @{ $self->{args} }; | |||||||||||
| 214 | 16 | my %need = map { $_ => undef } @ret; | |||||||||||
| 215 | 16 | foreach ( keys %need ) { | |||||||||||
| 216 | 30 | $need{ $_ } = $formats{ $_ }->[ 1 ]->( $method, $msg ); | |||||||||||
| 217 | } | ||||||||||||
| 218 | 16 | s/^(\w)$/$need{$1}/e foreach @ret; | |||||||||||
| 219 | 16 | return @ret; | |||||||||||
| 220 | } | ||||||||||||
| 221 | |||||||||||||
| 222 | 3 | ▶ | sub DESTROY { close shift->{logfh} or warn "Couldn't close log file: $!"; } | ||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 223 | |||||||||||||
| 224 | =head2 errstr | ||||||||||||
| 225 | |||||||||||||
| 226 | Called as a class method, C< Log::Tiny->errstr > reveals the | ||||||||||||
| 227 | error that Log::Tiny encountered in creation or invocation. | ||||||||||||
| 228 | |||||||||||||
| 229 | =cut | ||||||||||||
| 230 | |||||||||||||
| 231 | 2 | ▶ | sub errstr { $errstr; } | ||||||||||
|
Subroutine
errstr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 232 | 1 | ▶ | sub _error { $errstr = shift; undef; } | ||||||||||
|
Subroutine
_error: called CC=1 SCAR=0.0
| |||||||||||||
| 233 | |||||||||||||
| 234 | =head2 log_only | ||||||||||||
| 235 | |||||||||||||
| 236 | Log only the given categories | ||||||||||||
| 237 | |||||||||||||
| 238 | =cut | ||||||||||||
| 239 | |||||||||||||
| 240 | sub log_only { | ||||||||||||
| 241 | 1 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
log_only: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 242 | 1 | $self->{methods_only} = \@_; | |||||||||||
| 243 | } | ||||||||||||
| 244 | |||||||||||||
| 245 | =head1 AUTHOR | ||||||||||||
| 246 | |||||||||||||
| 247 | Jordan M. Adler, C<< <jmadler at cpan.org> >> | ||||||||||||
| 248 | |||||||||||||
| 249 | =head1 BUGS | ||||||||||||
| 250 | |||||||||||||
| 251 | Please report any bugs or feature requests to | ||||||||||||
| 252 | C<bug-log-tiny at rt.cpan.org>, or through the web interface at | ||||||||||||
| 253 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-Tiny>. | ||||||||||||
| 254 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||
| 255 | your bug as I make changes. | ||||||||||||
| 256 | |||||||||||||
| 257 | =head1 SUPPORT | ||||||||||||
| 258 | |||||||||||||
| 259 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 260 | |||||||||||||
| 261 | perldoc Log::Tiny | ||||||||||||
| 262 | |||||||||||||
| 263 | You can also look for information at: | ||||||||||||
| 264 | |||||||||||||
| 265 | =over 4 | ||||||||||||
| 266 | |||||||||||||
| 267 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 268 | |||||||||||||
| 269 | L<http://annocpan.org/dist/Log-Tiny> | ||||||||||||
| 270 | |||||||||||||
| 271 | =item * CPAN Ratings | ||||||||||||
| 272 | |||||||||||||
| 273 | L<http://cpanratings.perl.org/d/Log-Tiny> | ||||||||||||
| 274 | |||||||||||||
| 275 | =item * RT: CPAN's request tracker | ||||||||||||
| 276 | |||||||||||||
| 277 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Tiny> | ||||||||||||
| 278 | |||||||||||||
| 279 | =item * Search CPAN | ||||||||||||
| 280 | |||||||||||||
| 281 | L<http://search.cpan.org/dist/Log-Tiny> | ||||||||||||
| 282 | |||||||||||||
| 283 | =item * GitHub | ||||||||||||
| 284 | |||||||||||||
| 285 | L<https://github.com/jmadler/p5-log-tiny> | ||||||||||||
| 286 | |||||||||||||
| 287 | =back | ||||||||||||
| 288 | |||||||||||||
| 289 | =head1 ACKNOWLEDGEMENTS | ||||||||||||
| 290 | |||||||||||||
| 291 | Much thanks to Michael Schilli C<CPAN:mschilli> for his great work on | ||||||||||||
| 292 | Log::Log4perl, of which this module's formatting concept is largely based upon. | ||||||||||||
| 293 | |||||||||||||
| 294 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 295 | |||||||||||||
| 296 | Copyright 2007-2015 Jordan M. Adler, all rights reserved. | ||||||||||||
| 297 | |||||||||||||
| 298 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 299 | under the same terms as Perl itself. | ||||||||||||
| 300 | |||||||||||||
| 301 | =cut | ||||||||||||
| 302 | |||||||||||||
| 303 | 5 | 1; # End of Log::Tiny | |||||||||||