| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 2 |  |  |  |  |  |  | # Copyright (C) 2002-2015 by Jörn Reder . | 
| 3 |  |  |  |  |  |  | # All Rights Reserved. See file COPYRIGHT for details. | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | # This module is part of Event::RPC, which is free software; you can | 
| 6 |  |  |  |  |  |  | # redistribute it and/or modify it under the same terms as Perl itself. | 
| 7 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | package Event::RPC::Logger; | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 17 |  |  | 17 |  | 8722985 | use strict; | 
|  | 17 |  |  |  |  | 241 |  | 
|  | 17 |  |  |  |  | 1769 |  | 
| 12 | 17 |  |  | 17 |  | 338 | use utf8; | 
|  | 17 |  |  |  |  | 82 |  | 
|  | 17 |  |  |  |  | 660 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 17 |  |  | 17 |  | 20184 | use FileHandle; | 
|  | 17 |  |  |  |  | 111250 |  | 
|  | 17 |  |  |  |  | 181 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 0 |  |  | 0 | 0 |  | sub get_filename                { shift->{filename}                     } | 
| 17 | 0 |  |  | 0 | 0 |  | sub get_filename_fh             { shift->{filename_fh}                  } | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 0 |  |  | 0 | 0 |  | sub get_fh_lref                 { shift->{fh_lref}                      } | 
| 20 | 0 |  |  | 0 | 0 |  | sub get_min_level               { shift->{min_level}                    } | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  | 0 | 0 |  | sub set_fh_lref                 { shift->{fh_lref}              = $_[1] } | 
| 23 | 0 |  |  | 0 | 0 |  | sub set_min_level               { shift->{min_level}            = $_[1] } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub new { | 
| 26 | 0 |  |  | 0 | 0 |  | my $class = shift; | 
| 27 | 0 |  |  |  |  |  | my %par = @_; | 
| 28 |  |  |  |  |  |  | my  ($filename, $fh_lref, $min_level) = | 
| 29 | 0 |  |  |  |  |  | @par{'filename','fh_lref','min_level'}; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 0 |  |  |  |  |  | my $filename_fh; | 
| 32 | 0 | 0 |  |  |  |  | if ( $filename ) { | 
| 33 | 0 |  |  |  |  |  | $filename_fh = FileHandle->new; | 
| 34 | 0 | 0 |  |  |  |  | open ($filename_fh, ">>$filename") | 
| 35 |  |  |  |  |  |  | or die "can't write log $filename"; | 
| 36 | 0 |  |  |  |  |  | $filename_fh->autoflush(1); | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 0 | 0 |  |  |  |  | if ( $fh_lref ) { | 
| 40 | 0 |  |  |  |  |  | foreach my $fh ( @{$fh_lref} ) { | 
|  | 0 |  |  |  |  |  |  | 
| 41 | 0 |  |  |  |  |  | my $old_fh = select $fh; | 
| 42 | 0 |  |  |  |  |  | $| = 1; | 
| 43 | 0 |  |  |  |  |  | select $old_fh; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  | else { | 
| 47 | 0 |  |  |  |  |  | $fh_lref = []; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | my $self = bless { | 
| 51 |  |  |  |  |  |  | filename        => $filename, | 
| 52 |  |  |  |  |  |  | filename_fh     => $filename_fh, | 
| 53 |  |  |  |  |  |  | fh_lref         => $fh_lref, | 
| 54 |  |  |  |  |  |  | min_level       => $min_level, | 
| 55 |  |  |  |  |  |  | }, $class; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 |  |  |  |  |  | return $self; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub DESTROY { | 
| 61 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | my $filename_fh = $self->get_filename_fh; | 
| 64 | 0 | 0 |  |  |  |  | close $filename_fh if $filename_fh; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | 1; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub log { | 
| 70 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 71 | 0 |  |  |  |  |  | my ($level, $msg); | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 | 0 |  |  |  |  | if ( @_ == 2 ) { | 
| 74 | 0 |  |  |  |  |  | $level = $_[0]; | 
| 75 | 0 |  |  |  |  |  | $msg   = $_[1]; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | else { | 
| 78 | 0 |  |  |  |  |  | $level = 1; | 
| 79 | 0 |  |  |  |  |  | $msg = $_[0]; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 | 0 |  |  |  |  | return if $level > $self->get_min_level; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 | 0 |  |  |  |  | $msg .= "\n" if $msg !~ /\n$/; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | my $str = localtime(time)." [$level] $msg"; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 0 |  |  |  |  |  | for my $fh ( @{$self->get_fh_lref} ) { | 
|  | 0 |  |  |  |  |  |  | 
| 89 | 0 | 0 |  |  |  |  | print $fh $str if $fh; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 0 |  |  |  |  |  | my $fh = $self->get_filename_fh; | 
| 93 | 0 | 0 |  |  |  |  | print $fh $str if $fh; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 |  |  |  |  |  | 1; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | sub add_fh { | 
| 99 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 100 | 0 |  |  |  |  |  | my ($fh) = @_; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 0 |  |  |  |  |  | push @{$self->get_fh_lref}, $fh; | 
|  | 0 |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | 1; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | sub remove_fh { | 
| 108 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 109 | 0 |  |  |  |  |  | my ($fh) = @_; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 |  |  |  |  |  | my $fh_lref = $self->get_fh_lref; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 |  |  |  |  |  | my $i; | 
| 114 | 0 |  |  |  |  |  | for ( $i=0; $i<@{$fh_lref}; ++$i ) { | 
|  | 0 |  |  |  |  |  |  | 
| 115 | 0 | 0 |  |  |  |  | last if $fh_lref->[$i] eq $fh; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 0 | 0 |  |  |  |  | return if $i == @{$fh_lref}; | 
|  | 0 |  |  |  |  |  |  | 
| 119 | 0 |  |  |  |  |  | splice @{$fh_lref}, $i, 1; | 
|  | 0 |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 0 |  |  |  |  |  | 1; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | 1; | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | __END__ |