File Coverage

blib/lib/Log/Dispatch/Screen.pm
Criterion Covered Total %
statement 32 33 96.9
branch 2 4 50.0
condition n/a
subroutine 9 9 100.0
pod 0 2 0.0
total 43 48 89.5


line stmt bran cond sub pod time code
1             package Log::Dispatch::Screen;
2              
3 3     3   104077 use strict;
  3         11  
  3         70  
4 3     3   12 use warnings;
  3         6  
  3         101  
5              
6             our $VERSION = '2.69';
7              
8 3     3   1375 use Encode qw( encode );
  3         24579  
  3         180  
9 3     3   22 use IO::Handle;
  3         6  
  3         88  
10 3     3   336 use Log::Dispatch::Types;
  3         8  
  3         26  
11 3     3   69577 use Params::ValidationCompiler qw( validation_for );
  3         13930  
  3         165  
12              
13 3     3   21 use base qw( Log::Dispatch::Output );
  3         4  
  3         1045  
14              
15             {
16             my $validator = validation_for(
17             params => {
18             stderr => {
19             type => t('Bool'),
20             default => 1,
21             },
22             utf8 => {
23             type => t('Bool'),
24             default => 0,
25             },
26             },
27             slurpy => 1,
28             );
29              
30             sub new {
31 4     4 0 13784 my $class = shift;
32 4         84 my %p = $validator->(@_);
33              
34 4         121 my $self = bless { map { $_ => delete $p{$_} } qw( stderr utf8 ) },
  8         26  
35             $class;
36              
37 4         32 $self->_basic_init(%p);
38              
39 4         16 return $self;
40             }
41             }
42              
43             sub log_message {
44 1     1 0 2 my $self = shift;
45 1         3 my %p = @_;
46              
47             # This is a bit gross but it's important that we print directly to the
48             # STDOUT or STDERR handle for backwards compatibility. Various modules
49             # have tests which rely on this, so we can't open a new filehandle to fd 1
50             # or 2 and use that.
51             my $message
52 1 50       7 = $self->{utf8} ? encode( 'UTF-8', $p{message} ) : $p{message};
53              
54             ## no critic (InputOutput::RequireCheckedSyscalls)
55 1 50       172 if ( $self->{stderr} ) {
56 0         0 print STDERR $message;
57             }
58             else {
59 1         45 print STDOUT $message;
60             }
61             }
62              
63             1;
64              
65             # ABSTRACT: Object for logging to the screen
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             Log::Dispatch::Screen - Object for logging to the screen
76              
77             =head1 VERSION
78              
79             version 2.69
80              
81             =head1 SYNOPSIS
82              
83             use Log::Dispatch;
84              
85             my $log = Log::Dispatch->new(
86             outputs => [
87             [
88             'Screen',
89             min_level => 'debug',
90             stderr => 1,
91             newline => 1
92             ]
93             ],
94             );
95              
96             $log->alert("I'm searching the city for sci-fi wasabi");
97              
98             =head1 DESCRIPTION
99              
100             This module provides an object for logging to the screen (really
101             C<STDOUT> or C<STDERR>).
102              
103             Note that a newline will I<not> be added automatically at the end of a
104             message by default. To do that, pass C<< newline => 1 >>.
105              
106             The handle will be autoflushed, but this module opens it's own handle to fd 1
107             or 2 instead of using the global C<STDOUT> or C<STDERR>.
108              
109             =for Pod::Coverage new log_message
110              
111             =head1 CONSTRUCTOR
112              
113             The constructor takes the following parameters in addition to the standard
114             parameters documented in L<Log::Dispatch::Output>:
115              
116             =over 4
117              
118             =item * stderr (0 or 1)
119              
120             Indicates whether or not logging information should go to C<STDERR>. If
121             false, logging information is printed to C<STDOUT> instead.
122              
123             This defaults to true.
124              
125             =item * utf8 (0 or 1)
126              
127             If this is true, then the output uses C<binmode> to apply the
128             C<:encoding(UTF-8)> layer to the relevant handle for output. This will not
129             affect C<STDOUT> or C<STDERR> in other parts of your code.
130              
131             This defaults to false.
132              
133             =back
134              
135             =head1 SUPPORT
136              
137             Bugs may be submitted at L<https://github.com/houseabsolute/Log-Dispatch/issues>.
138              
139             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
140              
141             =head1 SOURCE
142              
143             The source code repository for Log-Dispatch can be found at L<https://github.com/houseabsolute/Log-Dispatch>.
144              
145             =head1 AUTHOR
146              
147             Dave Rolsky <autarch@urth.org>
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is Copyright (c) 2019 by Dave Rolsky.
152              
153             This is free software, licensed under:
154              
155             The Artistic License 2.0 (GPL Compatible)
156              
157             The full text of the license can be found in the
158             F<LICENSE> file included with this distribution.
159              
160             =cut