lib/NewsClipper/Globals.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 130 CC 10 SCAR 23.3
CC
10
Cov
0%
_Initialize_Log_Files
20.0
dequote
20.0
reformat
12.0
CRAP 10.3
1 # -*- mode: Perl; -*-
2 package NewsClipper::Globals;
3  
4 # This package contains a set of globals used by all News Clipper modules.
5  
6 use strict;
7 # For exporting of functions
8 use Exporter;
9  
10 use vars qw( @ISA @EXPORT $VERSION );
11  
12 @ISA = qw( Exporter );
13 @EXPORT = qw( DEBUG reformat dprint lprint dequote %errors %config %opts);
14  
15 $VERSION = 0.30;
16  
17 # ------------------------------------------------------------------------------
18  
19 # We'll use a global DEBUG constant, which is set by the -d flag on the
20 # command line. (Be sure to "require" this module after doing getopt in main.
21 # Debug mode doesn't put a time limit on the script, outputs some
22 # <!--DEBUG:...--> commentary, and doesn't write to the output file (instead
23 # it dumps to screen).
24  
25 use constant DEBUG => $main::opts{d} || 0;
26  
27 # ------------------------------------------------------------------------------
28  
29 # We will alias this variable to the one in main so that it will be easily
30 # accessible by all modules. We don't simply put this variable here because it
31 # is used in main before this module is loaded.
32  
33 *config = \%main::config;
34  
35 # ------------------------------------------------------------------------------
36  
37 # We will alias this variable to the one in main so that it will be easily
38 # accessible by all modules. We don't simply put this variable here because it
39 # is used in main before this module is loaded.
40  
41 *opts = \%main::opts;
42  
43 # ------------------------------------------------------------------------------
44  
45 # This variable will hold error messages from various parts of the system.
46 # These messages will be stored according to their location, and then printed
47 # as News Clipper commands execute. The contents are cleared at the end of
48 # each sequence of News Clipper commands.
49  
50 my %errors;
51  
52 # ------------------------------------------------------------------------------
53  
54 # The user's home directory (Initialized in main::SetupConfig)
55 my $home;
56  
57 # The cache. There is only one. (Initialized in main::SetupConfig)
58 my $cache;
59  
60 # The News Clipper state. There is only one. (Initialized in main::SetupConfig)
61 my $state;
62  
63 # The handler factory. There is only one. (Initialized in main::SetupConfig)
64 my $handlerFactory;
65  
66 # ------------------------------------------------------------------------------
67  
68 use Text::Wrap;
69  
70 # Reformats the input to 80 columns, or the number specified by the first
71 # argument. Retains any empty lines at the end.
72  
73 sub reformat(@)
74 {
75 my $columns;
76  
77 if ($#_ > 0 && $_[0] =~ /^\d+$/)
78 {
79 $columns = shift;
80 }
81 else
82 {
83 $columns = 80;
84 }
85  
86 my $text = join '\n',@_;
87  
88 my ($ending_newlines) = $text =~ /(\n*)$/s;
89 $ending_newlines = '' unless defined $ending_newlines;
90 $text =~ s/\n*$//;
91  
92 # Change all the newlines to spaces in preparation of reformatting.
93 $text =~ s/\n/ /g;
94 $Text::Wrap::columns = $columns;
95  
96 my $formatted = wrap('','',$text);
97  
98 # Tack a newline on the end if the original had one.
99  
100 $formatted .= $ending_newlines;
101  
102 return $formatted;
103 }
104  
105 # ------------------------------------------------------------------------------
106  
107 use Log::Agent;
108 use Log::Agent::Driver::File;
109 use Log::Agent::Rotate;
110  
111 my $_driver;
112  
113 # Initialize the log files if they haven't been initialized already.
114  
115 sub _Initialize_Log_Files
116 {
117 return if defined $_driver;
118  
119 my $rotate = Log::Agent::Rotate->make(
120 -backlog => $main::config{'max_number_of_log_files'},
121 -max_size => $main::config{'max_log_file_size'},
122 -is_alone => 1,
123 );
124  
125 my ($error_channel_setup,$debug_channel_setup);
126  
127 # Handle non-rotating special output files
128 if ($main::config{'run_log_file'} =~ /^(STDERR|STDOUT)$/)
129 {
130 $error_channel_setup = ">&$main::config{'run_log_file'}";
131 }
132 else
133 {
134 $error_channel_setup = [$main::config{'run_log_file'}, $rotate];
135 }
136  
137 if ($main::config{'debug_log_file'} =~ /^(STDERR|STDOUT)$/)
138 {
139 $debug_channel_setup = ">&$main::config{'debug_log_file'}";
140 }
141 else
142 {
143 $debug_channel_setup = [$main::config{'debug_log_file'}, $rotate];
144 }
145  
146 $_driver = Log::Agent::Driver::File->make(
147 -channels => {
148 'error' => $error_channel_setup,
149 'debug' => $debug_channel_setup,
150 },
151 -stampfmt => 'none',
152 -magic_open => 1,
153 );
154  
155 logconfig(-driver => $_driver, -debug => 10);
156 }
157  
158 # ------------------------------------------------------------------------------
159  
160 # Prints debug messages in the form "<!--DEBUG: ... -->" if the DEBUG constant
161 # is true.
162  
163 sub dprint(@)
164 {
165 return 1 unless DEBUG;
166  
167 my $message = join '',@_;
168  
169 _Initialize_Log_Files();
170  
171 my @lines = split /\n/, $message;
172 foreach my $line (@lines)
173 {
174 logdbg(1,$line);
175 }
176  
177 return 1;
178 }
179  
180 # ------------------------------------------------------------------------------
181  
182 # Logs messages to the logfile.
183 sub lprint(@)
184 {
185 my $message = join '',@_;
186  
187 _Initialize_Log_Files();
188  
189 my @lines = split /\n/, $message;
190 foreach my $line (@lines)
191 {
192 logerr($line);
193 }
194  
195 return 1;
196 }
197  
198 # ------------------------------------------------------------------------------
199  
200 # Allows indented here documents. Modified from the Perl Cookbook. The first
201 # argument can be a prefix string to start each line with.
202  
203 sub dequote($;$)
204 {
205 my $prefix;
206 $prefix = shift if $#_ == 1;
207  
208 local $_ = shift;
209  
210 my ($white, $leader);
211  
212 if (/^\s*(?:([^\w\s<>!@#\$\%^&*()]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/)
213 {
214 ($white, $leader) = ($2, quotemeta($1));
215 }
216 else
217 {
218 ($white, $leader) = (/^(\s*)/,'');
219 }
220  
221 s/^\n/$white\n/gm;
222 s/^\s*?$leader(?:$white)//gm;
223  
224 # Put the prefix on if one was specified
225 $_ =~ s/^/$prefix/gm if $prefix;
226  
227 return $_;
228 }
229  
230 # ------------------------------------------------------------------------------
231  
232 1;