summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Log/Message/Handlers.pm
blob: c7c35c6e3890b48d5420bc99c4cead990365465f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package Log::Message::Handlers;
use strict;

=pod

=head1 NAME

Log::Message::Handlers - Message handlers for Log::Message

=head1 SYNOPSIS

    # Implicitly used by Log::Message to serve as handlers for
    # Log::Message::Item objects

    # Create your own file with a package called
    # Log::Message::Handlers to add to the existing ones, or to even
    # overwrite them

    $item->carp;

    $item->trace;


=head1 DESCRIPTION

Log::Message::Handlers provides handlers for Log::Message::Item objects.
The handler corresponding to the level (see Log::Message::Item manpage
for an explanation about levels) will be called automatically upon
storing the error.

Handlers may also explicitly be called on an Log::Message::Item object
if one so desires (see the Log::Message manpage on how to retrieve the
Item objects).

=head1 Default Handlers

=head2 log

Will simply log the error on the stack, and do nothing special

=cut

sub log { 1 }

=head2 carp

Will carp (see the Carp manpage) with the error, and add the timestamp
of when it occurred.

=cut

sub carp {
    my $self = shift;
    warn join " ", $self->message, $self->shortmess, 'at', $self->when, "\n";
}

=head2 croak

Will croak (see the Carp manpage) with the error, and add the
timestamp of when it occurred.

=cut

sub croak {
    my $self = shift;
    die join " ", $self->message, $self->shortmess, 'at', $self->when, "\n";
}

=head2 cluck

Will cluck (see the Carp manpage) with the error, and add the
timestamp of when it occurred.

=cut

sub cluck {
    my $self = shift;
    warn join " ", $self->message, $self->longmess, 'at', $self->when, "\n";
}

=head2 confess

Will confess (see the Carp manpage) with the error, and add the
timestamp of when it occurred

=cut

sub confess {
    my $self = shift;
    die join " ", $self->message, $self->longmess, 'at', $self->when, "\n";
}

=head2 die

Will simply die with the error message of the item

=cut

sub die  { die  shift->message; }


=head2 warn

Will simply warn with the error message of the item

=cut

sub warn { warn shift->message; }


=head2 trace

Will provide a traceback of this error item back to the first one that
occurrent, clucking with every item as it comes across it.

=cut

sub trace {
    my $self = shift;

    for my $item( $self->parent->retrieve( chrono => 0 ) ) {
        $item->cluck;
    }
}

=head1 Custom Handlers

If you wish to provide your own handlers, you can simply do the
following:

=over 4

=item *

Create a file that holds a package by the name of
C<Log::Message::Handlers>

=item *

Create subroutines with the same name as the levels you wish to
handle in the Log::Message module (see the Log::Message manpage for
explanation on levels)

=item *

Require that file in your program, or add it in your configuration
(see the Log::Message::Config manpage for explanation on how to use a
config file)

=back

And that is it, the handler will now be available to handle messages
for you.

The arguments a handler may receive are those specified by the
C<extra> key, when storing the message.
See the Log::Message manpage for details on the arguments.

=head1 SEE ALSO

L<Log::Message>, L<Log::Message::Item>, L<Log::Message::Config>

=head1 AUTHOR

This module by
Jos Boumans E<lt>kane@cpan.orgE<gt>.

=head1 Acknowledgements

Thanks to Ann Barcomb for her suggestions.

=head1 COPYRIGHT

This module is
copyright (c) 2002 Jos Boumans E<lt>kane@cpan.orgE<gt>.
All rights reserved.

This library is free software;
you may redistribute and/or modify it under the same
terms as Perl itself.

=cut

1;

# Local variables:
# c-indentation-style: bsd
# c-basic-offset: 4
# indent-tabs-mode: nil
# End:
# vim: expandtab shiftwidth=4: