… | |
… | |
108 | info => 7, |
108 | info => 7, |
109 | debug => 8, |
109 | debug => 8, |
110 | trace => 9, |
110 | trace => 9, |
111 | ); |
111 | ); |
112 | |
112 | |
|
|
113 | sub now () { time } |
|
|
114 | AnyEvent::post_detect { |
|
|
115 | *now = \&AE::now; |
|
|
116 | }; |
|
|
117 | |
113 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
118 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
114 | |
119 | |
115 | sub _log { |
120 | sub _log { |
116 | my ($pkg, $targ, $msg, @args) = @_; |
121 | my ($pkg, $targ, $msg, @args) = @_; |
117 | |
122 | |
118 | my $level = ref $targ ? die "Can't use reference as logging level (yet)" |
123 | my $level = ref $targ ? die "Can't use reference as logging level (yet)" |
119 | : $targ > 0 && $targ <= 9 ? $targ+0 |
124 | : $targ > 0 && $targ <= 9 ? $targ+0 |
120 | : $STR2LEVEL{$targ} || Carp::croak "$targ: not a valid logging level, caught"; |
125 | : $STR2LEVEL{$targ} || Carp::croak "$targ: not a valid logging level, caught"; |
121 | |
126 | |
|
|
127 | #TODO: find actual targets, see if we even have to log |
|
|
128 | |
122 | return if $level > $AnyEvent::VERBOSE; |
129 | return unless $level <= $AnyEvent::VERBOSE; |
123 | |
130 | |
124 | $msg = $msg->() if ref $msg; |
131 | $msg = $msg->() if ref $msg; |
125 | $msg = sprintf $msg, @args if @args; |
132 | $msg = sprintf $msg, @args if @args; |
126 | $msg =~ s/\n$//; |
133 | $msg =~ s/\n$//; |
127 | |
134 | |
128 | # now we have a message, log it |
135 | # now we have a message, log it |
129 | #TODO: could do LOTS of stuff here, and should, at least in some later version |
|
|
130 | |
136 | |
|
|
137 | # TODO: writers/processors/filters/formatters? |
|
|
138 | |
131 | $msg = sprintf "%5s %s: %s", $LEVEL2STR[$level], $pkg, $msg; |
139 | $msg = sprintf "%-5s %s: %s", $LEVEL2STR[$level], $pkg, $msg; |
132 | my $pfx = ft AE::now; |
140 | my $pfx = ft now; |
133 | |
141 | |
134 | for (split /\n/, $msg) { |
142 | for (split /\n/, $msg) { |
135 | printf STDERR "$pfx $_\n"; |
143 | printf STDERR "$pfx $_\n"; |
136 | $pfx = "\t"; |
144 | $pfx = "\t"; |
137 | } |
145 | } |