Filename | /usr/local/lib/perl5/site_perl/mach/5.24/Razor2/Logger.pm |
Statements | Executed 13 statements in 2.83ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 10.8ms | 25.3ms | BEGIN@12 | Razor2::Logger::
1 | 1 | 1 | 1.34ms | 8.53ms | BEGIN@6 | Razor2::Logger::
1 | 1 | 1 | 65µs | 254µs | BEGIN@8 | Razor2::Logger::
1 | 1 | 1 | 50µs | 68µs | BEGIN@5 | Razor2::Logger::
1 | 1 | 1 | 38µs | 992µs | BEGIN@9 | Razor2::Logger::
1 | 1 | 1 | 33µs | 400µs | BEGIN@7 | Razor2::Logger::
0 | 0 | 0 | 0s | 0s | log | Razor2::Logger::
0 | 0 | 0 | 0s | 0s | log2file | Razor2::Logger::
0 | 0 | 0 | 0s | 0s | new | Razor2::Logger::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # $Id: Logger.pm,v 1.22 2005/06/10 18:31:32 vipul Exp $ | ||||
2 | |||||
3 | package Razor2::Logger; | ||||
4 | |||||
5 | 2 | 77µs | 2 | 87µs | # spent 68µs (50+18) within Razor2::Logger::BEGIN@5 which was called:
# once (50µs+18µs) by base::import at line 5 # spent 68µs making 1 call to Razor2::Logger::BEGIN@5
# spent 18µs making 1 call to strict::import |
6 | 2 | 335µs | 2 | 8.63ms | # spent 8.53ms (1.34+7.19) within Razor2::Logger::BEGIN@6 which was called:
# once (1.34ms+7.19ms) by base::import at line 6 # spent 8.53ms making 1 call to Razor2::Logger::BEGIN@6
# spent 100µs making 1 call to Exporter::import |
7 | 2 | 96µs | 2 | 768µs | # spent 400µs (33+367) within Razor2::Logger::BEGIN@7 which was called:
# once (33µs+367µs) by base::import at line 7 # spent 400µs making 1 call to Razor2::Logger::BEGIN@7
# spent 367µs making 1 call to Time::HiRes::import |
8 | 2 | 121µs | 2 | 443µs | # spent 254µs (65+189) within Razor2::Logger::BEGIN@8 which was called:
# once (65µs+189µs) by base::import at line 8 # spent 254µs making 1 call to Razor2::Logger::BEGIN@8
# spent 189µs making 1 call to POSIX::import |
9 | 2 | 90µs | 2 | 1.95ms | # spent 992µs (38+954) within Razor2::Logger::BEGIN@9 which was called:
# once (38µs+954µs) by base::import at line 9 # spent 992µs making 1 call to Razor2::Logger::BEGIN@9
# spent 954µs making 1 call to Exporter::import |
10 | # 2003/09/10 Anne Bennett: syslog of our choice (uses socket, | ||||
11 | # does not assume network listener). | ||||
12 | 2 | 2.10ms | 2 | 25.5ms | # spent 25.3ms (10.8+14.5) within Razor2::Logger::BEGIN@12 which was called:
# once (10.8ms+14.5ms) by base::import at line 12 # spent 25.3ms making 1 call to Razor2::Logger::BEGIN@12
# spent 210µs making 1 call to Exporter::import |
13 | |||||
14 | # designed to be inherited module | ||||
15 | # but can stand alone. | ||||
16 | |||||
17 | sub new { | ||||
18 | |||||
19 | my ($class, %args) = @_; | ||||
20 | my %self = ( %args ); | ||||
21 | my $self = bless \%self, $class; | ||||
22 | |||||
23 | my $prefix = $args{LogPrefix} || 'razord2'; | ||||
24 | my $facility = $args{LogFacility} || 'local3'; | ||||
25 | my $loghost = $args{LogHost} || '127.0.0.1'; | ||||
26 | |||||
27 | if ($self->{LogTo} eq 'syslog') { | ||||
28 | $$self{syslog} = new Razor2::Syslog (Facility=> $facility, Priority => 'debug', Name => $prefix, SyslogHost => $loghost); | ||||
29 | $self->{LogType} = 'syslog'; | ||||
30 | } elsif ($self->{LogTo} =~ /^file:(.*)$/) { | ||||
31 | $self->{LogType} = 'file'; | ||||
32 | my $name = $1; chomp $name; | ||||
33 | open (LOGF, ">>$name") or do { | ||||
34 | if ($self->{DontDie}) { | ||||
35 | open LOGF, ">>/dev/null" or do { | ||||
36 | print STDERR "Failed to open /dev/null, $!\n"; | ||||
37 | }; | ||||
38 | } else { | ||||
39 | die $!; | ||||
40 | } | ||||
41 | }; | ||||
42 | LOGF->autoflush(1); | ||||
43 | $self->{fd} = *LOGF{IO}; | ||||
44 | } elsif ($self->{LogTo} eq 'sys-syslog') { | ||||
45 | # 2003/09/10 Anne Bennett: syslog of our choice (uses socket, | ||||
46 | # does not assume network listener). | ||||
47 | $self->{LogType} = 'sys-syslog'; | ||||
48 | openlog($prefix,"pid",$facility); | ||||
49 | } elsif ($self->{LogTo} eq 'stdout') { | ||||
50 | $self->{LogType} = 'file'; | ||||
51 | $self->{fd} = *STDOUT{IO}; | ||||
52 | } elsif ($self->{LogTo} eq 'stderr') { | ||||
53 | $self->{LogType} = 'file'; | ||||
54 | $self->{fd} = *STDERR{IO}; | ||||
55 | } else { | ||||
56 | $self->{LogType} = 'file'; | ||||
57 | $self->{fd} = *STDERR{IO}; | ||||
58 | } | ||||
59 | |||||
60 | $self->{LogTimeFormat} ||= "%b %d %H:%M:%S"; # formatting from strftime() | ||||
61 | $self->{LogDebugLevel} = exists $self->{LogDebugLevel} ? $self->{LogDebugLevel} : 5; | ||||
62 | $self->{Log2FileDir} ||= "/tmp"; | ||||
63 | |||||
64 | # 2002/11/27 Anne Bennett: log this at level 2 so we can set level | ||||
65 | # 1 (to get errors only) and avoid this unneeded line. | ||||
66 | $self->log(2,"[bootup] Logging initiated LogDebugLevel=$self->{LogDebugLevel} to $self->{LogTo}"); | ||||
67 | |||||
68 | return $self; | ||||
69 | |||||
70 | } | ||||
71 | |||||
72 | |||||
73 | sub log { | ||||
74 | |||||
75 | my ($self, $prio, $message) = @_; | ||||
76 | |||||
77 | return unless $prio <= $self->{LogDebugLevel}; | ||||
78 | |||||
79 | my ($package, $filename, $line) = caller; | ||||
80 | $filename =~ s:.*/::; | ||||
81 | |||||
82 | if ($self->{LogType} eq 'syslog') { | ||||
83 | |||||
84 | my $logstr = sprintf("[%2d] %s\n", $prio, $message); | ||||
85 | $logstr =~ s/\n+\n$/\n/; | ||||
86 | $self->{syslog}->send($logstr, Priority => 'debug'); | ||||
87 | |||||
88 | } elsif ($self->{LogType} eq 'sys-syslog') { | ||||
89 | # 2003/09/10 Anne Bennett: syslog of our choice (uses socket, | ||||
90 | # does not assume network listener). | ||||
91 | my $logstr = sprintf("[%2d] %s\n", $prio, $message); | ||||
92 | $logstr =~ s/\n+$//g; | ||||
93 | syslog("debug",$logstr); | ||||
94 | |||||
95 | } elsif ($self->{LogType} eq 'file') { | ||||
96 | |||||
97 | my $now_string; | ||||
98 | if ($self->{LogTimestamp}) { | ||||
99 | my ($seconds, $microseconds) = gettimeofday; | ||||
100 | $now_string = strftime $self->{LogTimeFormat}, localtime($seconds); | ||||
101 | $now_string .= sprintf ".%06d ", $microseconds; | ||||
102 | } | ||||
103 | |||||
104 | my $logstr = sprintf("%s[%d]: [%2d] %s\n", $self->{LogPrefix}, $$, $prio, $message); | ||||
105 | $logstr =~ s/\n+\n$/\n/; | ||||
106 | my $fd = $self->{fd}; | ||||
107 | print $fd "$now_string$logstr"; | ||||
108 | |||||
109 | } | ||||
110 | |||||
111 | return 1; | ||||
112 | } | ||||
113 | |||||
114 | sub log2file { | ||||
115 | my ($self, $prio, $textref, $fn_ext) = @_; | ||||
116 | |||||
117 | return unless $prio <= $self->{LogDebugLevel}; | ||||
118 | |||||
119 | unless (ref($textref) eq 'SCALAR') { | ||||
120 | print "log2file: not a scalar ref ($fn_ext)\n"; | ||||
121 | return; | ||||
122 | } | ||||
123 | my $len = length($$textref); | ||||
124 | my $fn = "$self->{Log2FileDir}/razor.$$.$fn_ext"; | ||||
125 | |||||
126 | if (open OUT, ">$fn") { | ||||
127 | print OUT $$textref; | ||||
128 | close OUT; | ||||
129 | $self->log($prio,"log2file: wrote message len=$len to file: $fn"); | ||||
130 | } else { | ||||
131 | $self->log($prio,"log2file: could not write to $fn: $!"); | ||||
132 | } | ||||
133 | } | ||||
134 | |||||
135 | 1 | 7µs | 1; | ||
136 |