ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Log.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Log.pm (file contents):
Revision 1.1 by root, Mon Aug 15 23:21:09 2011 UTC vs.
Revision 1.2 by root, Tue Aug 16 14:47:27 2011 UTC

6 6
7 use AnyEvent::Log; 7 use AnyEvent::Log;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module implements a relatively simple "logging framework". It doesn't
12attempt to be "the" logging solution or even "a" logging solution for
13AnyEvent - AnyEvent simply creates logging messages internally, and this
14module more or less exposes the mechanism, with some extra spiff to allow
15using it from other modules as well.
16
17Remember that the default verbosity level is C<0>, so nothing
18will be logged, ever, unless you set C<$Anyvent::VERBOSE> or
19C<PERL_ANYEVENT_VERBOSE> to a higher number.
20
21Possible future extensions are to allow custom log targets (where the
22level is an object), log filtering based on package, formatting, aliasing
23or package groups.
24
11=head1 FUNCTIONS 25=head1 LOG FUNCTIONS
26
27These functions allow you to log messages. They always use the caller's
28package as a "logging module/source". Also, The main logging function is
29easily available as C<AnyEvent::log> or C<AE::log> when the C<AnyEvent>
30module is loaded.
12 31
13=over 4 32=over 4
14 33
15=cut 34=cut
16 35
17package AnyEvent::Log; 36package AnyEvent::Log;
18 37
38use Carp ();
19use POSIX (); 39use POSIX ();
20 40
21use AnyEvent (); BEGIN { AnyEvent::common_sense } 41use AnyEvent (); BEGIN { AnyEvent::common_sense }
42
43our ($now_int, $now_str1, $now_str2);
44
45# Format Time, not public - yet?
46sub ft($) {
47 my $i = int $_[0];
48 my $f = sprintf "%06d", 1e6 * ($_[0] - $i);
49
50 ($now_int, $now_str1, $now_str2) = ($i, split /\x01/, POSIX::strftime "%Y-%m-%d %H:%M:%S.\x01 %z", localtime $i)
51 if $now_int != $i;
52
53 "$now_str1$f$now_str2"
54}
55
56=item AnyEvent::Log::log $level, $msg[, @args]
57
58Requests logging of the given C<$msg> with the given log level (1..9).
59You can also use the following strings as log level: C<fatal> (1),
60C<alert> (2), C<critical> (3), C<error> (4), C<warn> (5), C<note> (6),
61C<info> (7), C<debug> (8), C<trace> (9).
62
63For C<fatal> log levels, the program will abort.
64
65If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the
66C<$msg> is interpreted as an sprintf format string.
67
68The C<$msg> should not end with C<\n>, but may if that is convenient for
69you. Also, multiline messages are handled properly.
70
71In addition, for possible future expansion, C<$msg> must not start with an
72angle bracket (C<< < >>).
73
74Whether the given message will be logged depends on the maximum log level
75and the caller's package.
76
77Note that you can (and should) call this function as C<AnyEvent::log> or
78C<AE::log>, without C<use>-ing this module if possible, as those functions
79will laod the logging module on demand only.
80
81=cut
82
83# also allow syslog equivalent names
84our %STR2LEVEL = (
85 fatal => 1, emerg => 1,
86 alert => 2,
87 critical => 3, crit => 3,
88 error => 4, err => 4,
89 warn => 5, warning => 5,
90 note => 6, notice => 6,
91 info => 7,
92 debug => 8,
93 trace => 9,
94);
95
96our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace);
97
98sub log($$;@) {
99 my ($targ, $msg, @args) = @_;
100
101 my $level = ref $targ ? die "Can't use reference as logging level (yet)"
102 : $targ > 0 && $targ <= 9 ? $targ+0
103 : $STR2LEVEL{$targ} || Carp::croak "$targ: not a valid logging level, caught";
104
105 return if $level > $AnyEvent::VERBOSE;
106
107 my $pkg = (caller)[0];
108
109 $msg = sprintf $msg, @args if @args;
110 $msg =~ s/\n$//;
111
112 # now we have a message, log it
113 #TODO: could do LOTS of stuff here, and should, at least in some later version
114
115 $msg = sprintf "%5s (%s) %s", $LEVEL2STR[$level], $pkg, $msg;
116 my $pfx = ft AE::now;
117
118 for (split /\n/, $msg) {
119 printf STDERR "$pfx $_\n";
120 $pfx = "\t";
121 }
122
123 exit 1 if $level <= 1;
124}
125
126*AnyEvent::log = *AE::log = \&log;
127
128#TODO
129
130=back
131
132=head1 CONFIGURATION FUNCTIONALITY
133
134None, yet, except for C<PERL_ANYEVENT_VERBOSE>, described in the L<AnyEvent> manpage.
135
136=over 4
137
138=cut
22 139
231; 1401;
24 141
25=back 142=back
26 143

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines