ensembl-hive  2.6
RedirectStack.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 DESCRIPTION
8 
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.
11 
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.
14 
15 =head1 USAGE EXAMPLE
16 
17  use RedirectStack;
18 
19  my $rs_stdout = RedirectStack->new(\*STDOUT);
20 
21  print "Message 1\n"; # gets displayed on the screen
22 
23  $rs_stdout->push('foo');
24 
25  print "Message 2\n"; # goes to 'foo'
26 
27  $rs_stdout->push('bar');
28 
29  print "Message 3\n"; # goes to 'bar'
30 
31  system('echo subprocess A'); # it works for subprocesses too
32 
33  $rs_stdout->pop;
34 
35  print "Message 4\n"; # goes to 'foo'
36 
37  system('echo subprocess B'); # again, works for subprocesses as well
38 
39  $rs_stdout->push('baz');
40 
41  print "Message 5\n"; # goest to 'baz'
42 
43  $rs_stdout->pop;
44 
45  print "Message 6\n"; # goes to 'foo'
46 
47  $rs_stdout->pop;
48 
49  print "Message 7\n"; # gets displayed on the screen
50 
51 =head1 LICENSE
52 
53  Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
54  Copyright [2016-2024] EMBL-European Bioinformatics Institute
55 
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
58 
59  http://www.apache.org/licenses/LICENSE-2.0
60 
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.
64 
65 =head1 CONTACT
66 
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
68 
69 =cut
70 
71 
72 package Bio::EnsEMBL::Hive::Utils::RedirectStack;
73 
74 use strict;
75 use warnings;
76 
77 sub new {
78  my ($class, $fh) = @_;
79 
80  die "Please supply filehandle to be redirected as the only argument" unless $fh;
81 
82  return bless {
83  '_fh' => $fh,
84  '_sp' => 0,
85  '_handle_stack' => [],
86  }, $class;
87 }
88 
89 sub push {
90  my ($self, $filename) = @_;
91 
92  die "Please supply filename to be redirected into as the only argument" unless $filename;
93 
94  unless($self->{_handle_stack}[$self->{_sp}]) {
95  open $self->{_handle_stack}[$self->{_sp}], '>&', $self->{_fh};
96  }
97  close $self->{_fh};
98  open $self->{_fh}, '>', $filename;
99  ++$self->{_sp};
100 }
101 
102 sub pop {
103  my ($self) = @_;
104 
105  if($self->{_handle_stack}[$self->{_sp}]) {
106  close $self->{_handle_stack}[$self->{_sp}];
107  delete $self->{_handle_stack}[$self->{_sp}];
108  }
109  close $self->{_fh};
110  open $self->{_fh}, '>&', $self->{_handle_stack}[--$self->{_sp}];
111 }
112 
113 1;
114 
Bio::EnsEMBL::Hive::Utils::RedirectStack
Definition: RedirectStack.pm:14