9 Sometimes there is a need to intercept STDOUT/STDERR and log it in multiple files,
10 depending on which of the nested objects is
"in control" at the moment.
12 In Hive when a Worker is running a job it logs into a job
's file, but between jobs it logs into its own file.
13 This class implements a convenient stack of proxy file descriptors that lets you log STDOUT or STDERR in various files.
19 my $rs_stdout = RedirectStack->new(\*STDOUT);
21 print "Message 1\n"; # gets displayed on the screen
23 $rs_stdout->push('foo
');
25 print "Message 2\n"; # goes to 'foo
'
27 $rs_stdout->push('bar
');
29 print "Message 3\n"; # goes to 'bar
'
31 system('echo subprocess A
'); # it works for subprocesses too
35 print "Message 4\n"; # goes to 'foo
'
37 system('echo subprocess B
'); # again, works for subprocesses as well
39 $rs_stdout->push('baz
');
41 print "Message 5\n"; # goest to 'baz
'
45 print "Message 6\n"; # goes to 'foo
'
49 print "Message 7\n"; # gets displayed on the screen
53 Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
54 Copyright [2016-2024] EMBL-European Bioinformatics Institute
56 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
57 You may obtain a copy of the License at
59 http://www.apache.org/licenses/LICENSE-2.0
61 Unless required by applicable law or agreed to in writing, software distributed under the License
62 is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
63 See the License for the specific language governing permissions and limitations under the License.
67 Please subscribe to the Hive mailing list: http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users to discuss Hive-related questions or to be notified of our updates
72 package Bio::EnsEMBL::Hive::Utils::RedirectStack;
78 my ($class, $fh) = @_;
80 die "Please supply filehandle to be redirected as the only argument" unless $fh;
85 '_handle_stack
' => [],
90 my ($self, $filename) = @_;
92 die "Please supply filename to be redirected into as the only argument" unless $filename;
94 unless($self->{_handle_stack}[$self->{_sp}]) {
95 open $self->{_handle_stack}[$self->{_sp}], '>&
', $self->{_fh};
98 open $self->{_fh}, '>
', $filename;
105 if($self->{_handle_stack}[$self->{_sp}]) {
106 close $self->{_handle_stack}[$self->{_sp}];
107 delete $self->{_handle_stack}[$self->{_sp}];
110 open $self->{_fh}, '>&
', $self->{_handle_stack}[--$self->{_sp}];