File Coverage

blib/lib/Log/Handler/Output/Screen.pm
Criterion Covered Total %
statement 18 53 33.9
branch 0 26 0.0
condition n/a
subroutine 6 11 54.5
pod 5 5 100.0
total 29 95 30.5


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Log::Handler::Output::Screen - Log messages to the screen.
4              
5             =head1 SYNOPSIS
6              
7             use Log::Handler::Output::Screen;
8              
9             my $screen = Log::Handler::Output::Screen->new(
10             log_to => "STDERR",
11             dump => 1,
12             );
13              
14             $screen->log($message);
15              
16             =head1 DESCRIPTION
17              
18             This output module makes it possible to log messages to your screen.
19              
20             =head1 METHODS
21              
22             =head2 new()
23              
24             Call C to create a new Log::Handler::Output::Screen object.
25              
26             The following options are possible:
27              
28             =over 4
29              
30             =item B
31              
32             Where do you want to log? Possible is: STDOUT, STDERR and WARN.
33              
34             WARN means to call C.
35              
36             The default is STDOUT.
37              
38             =item B
39              
40             Set this option to 1 if you want that the message will be dumped with
41             C to the screen.
42              
43             =item B, B
44              
45             Set utf8 or utf-8 on STDOUT or STDERR. It depends on the parameter B.
46              
47             utf8 = binmode, $fh, ":utf8";
48             utf-8 = binmode, $fh, "encoding(utf-8)";
49              
50             Yes, there is a difference.
51              
52             L
53              
54             L
55              
56             =back
57              
58             =head2 log()
59              
60             Call C if you want to log a message to the screen.
61              
62             Example:
63              
64             $screen->log("this message goes to the screen");
65              
66             =head2 validate()
67              
68             Validate a configuration.
69              
70             =head2 reload()
71              
72             Reload with a new configuration.
73              
74             =head2 errstr()
75              
76             This function returns the last error message.
77              
78             =head1 PREREQUISITES
79              
80             Data::Dumper
81             Params::Validate
82              
83             =head1 EXPORTS
84              
85             No exports.
86              
87             =head1 REPORT BUGS
88              
89             Please report all bugs to .
90              
91             If you send me a mail then add Log::Handler into the subject.
92              
93             =head1 AUTHOR
94              
95             Jonny Schulz .
96              
97             =head1 COPYRIGHT
98              
99             Copyright (C) 2007-2009 by Jonny Schulz. All rights reserved.
100              
101             This program is free software; you can redistribute it and/or
102             modify it under the same terms as Perl itself.
103              
104             =cut
105              
106             package Log::Handler::Output::Screen;
107              
108 1     1   8 use strict;
  1         1  
  1         31  
109 1     1   4 use warnings;
  1         3  
  1         24  
110 1     1   4 use Data::Dumper;
  1         2  
  1         70  
111 1     1   8 use Params::Validate qw();
  1         3  
  1         688  
112              
113             our $VERSION = "0.07";
114             our $ERRSTR = "";
115              
116             sub new {
117 2     2 1 4 my $class = shift;
118 2         5 my $options = $class->_validate(@_);
119 2         14 return bless $options, $class;
120             }
121              
122             sub log {
123 0     0 1 0 my $self = shift;
124 0 0       0 my $message = @_ > 1 ? {@_} : shift;
125 0         0 local $|=1;
126              
127 0 0       0 if ($self->{dump}) {
128 0         0 $message->{message} = Dumper($message);
129             }
130              
131 0 0       0 if ($self->{log_to} eq "STDOUT") {
    0          
    0          
132 0 0       0 if ($self->{utf8}) {
    0          
133 0         0 binmode STDOUT, ":utf8";
134             } elsif ($self->{"utf-8"}) {
135 0         0 binmode STDOUT, "encoding(utf-8)";
136             }
137             print STDOUT $message->{message}
138 0 0       0 or return $self->_raise_error($!);
139             } elsif ($self->{log_to} eq "STDERR") {
140 0 0       0 if ($self->{utf8}) {
    0          
141 0         0 binmode STDERR, ":utf8";
142             } elsif ($self->{"utf-8"}) {
143 0         0 binmode STDERR, "encoding(utf-8)";
144             }
145             print STDERR $message->{message}
146 0 0       0 or return $self->_raise_error($!);
147             } elsif ($self->{log_to} eq "WARN") {
148             # hmmm, should I really set utf8 for warnings?
149 0         0 warn $message->{message};
150             }
151              
152 0         0 return 1;
153             }
154              
155             sub validate {
156 0     0 1 0 my $self = shift;
157 0         0 my $opts = ();
158              
159 0         0 eval { $opts = $self->_validate(@_) };
  0         0  
160              
161 0 0       0 if ($@) {
162 0         0 $ERRSTR = $@;
163 0         0 return undef;
164             }
165              
166 0         0 return $opts;
167             }
168              
169             sub reload {
170 0     0 1 0 my $self = shift;
171 0         0 my $opts = $self->validate(@_);
172              
173 0 0       0 if (!$opts) {
174 0         0 return undef;
175             }
176              
177 0         0 foreach my $key (keys %$opts) {
178 0         0 $self->{$key} = $opts->{$key};
179             }
180              
181 0         0 return 1;
182             }
183              
184             sub errstr {
185 0     0 1 0 return $ERRSTR;
186             }
187              
188             #
189             # private stuff
190             #
191              
192             sub _validate {
193 2     2   4 my $class = shift;
194              
195 2         73 my %options = Params::Validate::validate(@_, {
196             log_to => {
197             type => Params::Validate::SCALAR,
198             regex => qr/^(?:STDOUT|STDERR|WARN)\z/,
199             default => "STDOUT",
200             },
201             utf8 => {
202             type => Params::Validate::SCALAR,
203             regex => qr/^[01]\z/,
204             default => 0,
205             },
206             "utf-8" => {
207             type => Params::Validate::SCALAR,
208             regex => qr/^[01]\z/,
209             default => 0,
210             },
211             dump => {
212             type => Params::Validate::SCALAR,
213             regex => qr/^[01]\z/,
214             default => 0,
215             },
216             });
217              
218 2         63 return \%options;
219             }
220              
221             sub _raise_error {
222 0     0     my $self = shift;
223 0           $ERRSTR = shift;
224 0           return undef;
225             }
226              
227             1;