File Coverage

blib/lib/Log/Any/Adapter/Dupstd.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package Log::Any::Adapter::Dupstd;
2              
3             #
4             # Cunning adapter for logging to a duplicate of STDOUT or STDERR
5             #
6              
7 1     1   778 use 5.008001;
  1         4  
8 1     1   6 use strict;
  1         1  
  1         20  
9 1     1   4 use warnings;
  1         2  
  1         22  
10 1     1   488 use utf8::all;
  1         52013  
  1         5  
11              
12             our $VERSION = '0.05';
13              
14             #---
15              
16             1;
17              
18             __END__
19              
20             =pod
21              
22             =encoding UTF-8
23              
24             =head1 NAME
25              
26             Log::Any::Adapter::Dupstd - Cunning adapter for logging to a duplicate of
27             STDOUT or STDERR
28              
29              
30             =head1 SYNOPSIS
31              
32             # Log to a duplicate of stdout or stderr
33              
34             use Log::Any::Adapter ('Dupout');
35             use Log::Any::Adapter ('Duperr');
36              
37             # or
38              
39             use Log::Any::Adapter;
40             ...
41             Log::Any::Adapter->set('Dupout');
42             Log::Any::Adapter->set('Duperr');
43              
44             # with minimum level 'warn'
45              
46             use Log::Any::Adapter ('Dupout', log_level => 'warn' );
47             use Log::Any::Adapter ('Duperr', log_level => 'warn' );
48              
49             # and later
50              
51             open(STDOUT, ">/dev/null");
52             open(STDERR, ">/dev/null");
53              
54              
55             =head1 DESCRIPTION
56              
57             Adapters Dupstd are intended to log messages into duplicates of standard
58             descriptors STDOUT and STDERR.
59              
60             Logging into a duplicate of standard descriptor might be needed in special
61             occasions when you need to redefine or even close standard descriptor but you
62             want to continue displaying messages wherever they are displayed by a standard
63             descriptor.
64              
65             For instance, your script types something in STDERR, and you want to redirect
66             that message into a file. If you redirect STDERR into a file, warnings C<warn>
67             and even exceptions C<die> will be redirected there as well. But that is not
68             always convenient. In many cases it is more convenient to display warnings and
69             exceptions on the screen.
70              
71             # Redirect STDERR into a file
72             open(STDERR, '>', 'stderr.txt');
73              
74             # This message will go to the file, not on the screen (you want this)
75             print STDERR 'Some message';
76              
77             # This warning will go to the file too (and that is what you don't want)
78             warn('Warning!');
79              
80             You can try to display warning or exception on the screen by yourself using
81             adapter Stderr from the distributive Log::Any. But adapter Stderr types message
82             on STDERR so the message will anyway be in the file and not on the screen.
83              
84             # Adapter Stderr
85             use Log::Any::Adapter ('Stderr');
86              
87             # Redirect STDERR into a file
88             open(STDERR, '>', 'stderr.txt')
89              
90             # This message will go to the file, not on the screen (you want this)
91             print STDERR 'Some message';
92              
93             # Oops, warning will go to the file (again it's not what you expected)
94             $log->warning('Warning!')
95              
96             You can display message on the screen using adapter Stdout, which is also in the
97             distributive Log::Any. Warning will be displayed on the screen as expected, but
98             that will be "not real" warning because it will be displayed through STDOUT.
99             That warning will be impossible to filter in the shell.
100              
101             # That won't be working!
102             $ script.pl 2> error.log
103              
104             That is the situation when you need adapter Dupstd. Warnings and exceptions sent
105             using these adapters will be "real". They can be filtered in the shell just as
106             if they would have been sent to usual STDERR.
107              
108             # Adapter Duperr (definitely PRIOR TO redirecting STDERR)
109             use Log::Any::Adapter ('Duperr');
110              
111             # Redirect STDERR into a file
112             open(STDERR, '>', 'stderr.txt')
113              
114             # This message will go to the file, not on the screen (you want this)
115             print STDERR 'Some message';
116              
117             # Warning will be displayed on the screen (that is what you want)
118             $log->warning('Warning!')
119              
120              
121             =head1 ATTENTION
122              
123             Adapters Dupstd must be initialized prior to standard descriptors being redefined or closed.
124              
125             Standard descriptor can't be reopened, that's why the duplicate must be made in advance.
126              
127              
128             =head1 ADAPTERS
129              
130             In this distributive there are two cunning adapters - Dupout and Duperr.
131              
132             These adapters work similarly to ordinary adapters from distributive Log::Any -
133             L<Stdout|Log::Any::Adapter::Stdout> and L<Stderr|Log::Any::Adapter::Stderr> (save that inside are used descriptors duplicates).
134              
135              
136             =head1 SEE ALSO
137              
138             L<Log::Any|Log::Any>, L<Log::Any::Adapter|Log::Any::Adapter>, L<Log::Any::For::Std|Log::Any::For::Std>
139              
140             =head1 LICENSE
141              
142             Copyright (C) Mikhail Ivanov.
143              
144             This library is free software; you can redistribute it and/or modify
145             it under the same terms as Perl itself.
146              
147             =head1 AUTHORS
148              
149             =over 4
150              
151             =item *
152              
153             Mikhail Ivanov <m.ivanych@gmail.com>
154              
155             =item *
156              
157             Anastasia Zherebtsova <zherebtsova@gmail.com> - translation of documentation
158             into English
159              
160             =back
161              
162             =cut