1package Log::Dispatch::Screen;
2
3use strict;
4use warnings;
5
6our $VERSION = '2.70';
7
8use Encode qw( encode );
9use IO::Handle;
10use Log::Dispatch::Types;
11use Params::ValidationCompiler qw( validation_for );
12
13use base qw( Log::Dispatch::Output );
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        my $class = shift;
32        my %p     = $validator->(@_);
33
34        my $self = bless { map { $_ => delete $p{$_} } qw( stderr utf8 ) },
35            $class;
36
37        $self->_basic_init(%p);
38
39        return $self;
40    }
41}
42
43sub log_message {
44    my $self = shift;
45    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        = $self->{utf8} ? encode( 'UTF-8', $p{message} ) : $p{message};
53
54    ## no critic (InputOutput::RequireCheckedSyscalls)
55    if ( $self->{stderr} ) {
56        print STDERR $message;
57    }
58    else {
59        print STDOUT $message;
60    }
61}
62
631;
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
75Log::Dispatch::Screen - Object for logging to the screen
76
77=head1 VERSION
78
79version 2.70
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
100This module provides an object for logging to the screen (really
101C<STDOUT> or C<STDERR>).
102
103Note that a newline will I<not> be added automatically at the end of a
104message by default. To do that, pass C<< newline => 1 >>.
105
106The handle will be autoflushed, but this module opens it's own handle to fd 1
107or 2 instead of using the global C<STDOUT> or C<STDERR>.
108
109=for Pod::Coverage new log_message
110
111=head1 CONSTRUCTOR
112
113The constructor takes the following parameters in addition to the standard
114parameters documented in L<Log::Dispatch::Output>:
115
116=over 4
117
118=item * stderr (0 or 1)
119
120Indicates whether or not logging information should go to C<STDERR>. If
121false, logging information is printed to C<STDOUT> instead.
122
123This defaults to true.
124
125=item * utf8 (0 or 1)
126
127If this is true, then the output uses C<binmode> to apply the
128C<:encoding(UTF-8)> layer to the relevant handle for output. This will not
129affect C<STDOUT> or C<STDERR> in other parts of your code.
130
131This defaults to false.
132
133=back
134
135=head1 SUPPORT
136
137Bugs may be submitted at L<https://github.com/houseabsolute/Log-Dispatch/issues>.
138
139I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
140
141=head1 SOURCE
142
143The source code repository for Log-Dispatch can be found at L<https://github.com/houseabsolute/Log-Dispatch>.
144
145=head1 AUTHOR
146
147Dave Rolsky <autarch@urth.org>
148
149=head1 COPYRIGHT AND LICENSE
150
151This software is Copyright (c) 2020 by Dave Rolsky.
152
153This is free software, licensed under:
154
155  The Artistic License 2.0 (GPL Compatible)
156
157The full text of the license can be found in the
158F<LICENSE> file included with this distribution.
159
160=cut
161