Document/LogFile.pm

filter: Statementstmt 36.1% 17 / 47 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod 100% 2 / 2 totaltotal 32.8% 24 / 73 CC 9 SCAR 25.3
CC
9
Cov
27%
new
20.0
log
20.0
_setdirobj
12.0
CRAP 12.6
« Document/Directory.pm ↑ Summary File/Spec/DatedPage.pm »
1 #================================ LogFile.pm =================================
2 # Filename: LogFile.pm
3 # Description: LogFile file manager.
4 # Original Author: Dale Amon
5 # Revised by: $Author: amon $
6 # Date: $Date: 2008-08-28 23:26:17 $
7 # Version: $Revision: 1.5 $
8 # License: LGPL 2.1, Perl Artistic or BSD
9 #
10 #=============================================================================
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fault::ErrorHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use DMA::ISODate;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec::DatedPage;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Document::LogFile;
17 1 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 @ISA = qw( UNIVERSAL );
19  
20 #=============================================================================
21 # Class Methods
22 #=============================================================================
23  
24 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
25 0 my ($class,$directory) = @_;
26 0 my $self = bless {}, $class;
27  
28 0 ▶ $directory ||
Branch: 1 branch
BranchTrueFalseTotal
$directory or "Fault::ErrorHandler"->warn("LogFile->new: Missing directory path."), (return undef) 0 0 0
29 (Fault::ErrorHandler->warn ("LogFile->new: Missing directory path."),
30 return undef);
31  
32 0 my $dir = $self->_setdirobj ($directory);
33 0 ▶ $dir || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$dir or return undef 0 0 0
34  
35 0 $self->{'directory'} = $dir;
36  
37 # Read in our own values if the .log already exists, create it if it
38 # doesn't and failure if we can't.
39 #
40 0 my $log = $dir->pathname . "/.log";
41 0 ▶ if (!-f $log) {$self->log("Initialized logfile for ". $dir->filename);}
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $log) 0 0 0
42  
43 0 return $self;
44 }
45  
46 #=============================================================================
47 # Object Methods
48 #=============================================================================
49  
50 0 ▶sub log {
Subroutine log: not called CC=4 SCAR=30.0
Pod documented
51 0 my ($self,$str) = @_;
52 0 ▶ defined $str || ($str="<Null log message>");
Branch: 1 branch
BranchTrueFalseTotal
defined $str or $str = "<Null log message>" 0 0 0
53  
54 0 my $fd;
55 0 my $path = $self->{'directory'}->pathname;
56 0 my $utc = DMA::ISODate->utc;
57 0 my $msg = sprintf "%s %s UTC> %s", $utc->date,$utc->time,$str;
58  
59 # First write the new values to a tmp file.
60 0 ▶ if (!open ($fd, ">>$path/.log")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fd, ">>$path/.log") 0 0 0
61 0 Fault::ErrorHandler->warn ("Failed to open .log file at ($path)!");
62 0 return 0;
63 }
64
65 0 ▶ if (!printf $fd "$msg\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (printf $fd "$msg\n") 0 0 0
66 0 Fault::ErrorHandler->warn ("Failed log write: ($str) to ($path)!");
67 0 close $fd;
68 0 return 0;
69 }
70 0 close $fd;
71 0 return 1;
72 }
73  
74 #=============================================================================
75 # Internal Methods
76 #=============================================================================
77 # Check the type of the filename argument. If it is an object of the right
78 # Class, use it; otherwise return undef
79  
80 0 ▶sub _setdirobj {
Subroutine _setdirobj: not called CC=3 SCAR=24.8
81 0 my ($self, $dir) = @_;
82 0 my $obj = $dir;
83  
84 # If it is not a reference, assume it is a filename string and parse it.
85 0 ▶ (ref($dir)) || ($obj = File::Spec::DatedPage->new ($dir));
Branch: 1 branch
BranchTrueFalseTotal
ref $dir or $obj = "File::Spec::DatedPage"->new($dir) 0 0 0
86  
87 0 ▶ ($obj->isa ('File::Spec::DatedPage')) || (return undef);
Branch: 1 branch
BranchTrueFalseTotal
$obj->isa("File::Spec::DatedPage") or return undef 0 0 0
88 0 return $obj;
89 }
90
91 #=============================================================================
92 # Pod Documentation
93 #=============================================================================
94 # You may extract and format the documention section with the 'perldoc' cmd.
95  
96 =head1 NAME
97  
98 Document::LogFile - LogFile file manager.
99  
100 =head1 SYNOPSIS
101  
102 use Document::LogFile;
103 $obj = Document::LogFile->new ($filespecobj);
104 $obj = Document::LogFile->new ($filename)
105 $okay = $obj->log ($string);
106  
107 =head1 Inheritance
108  
109 UNIVERSAL
110  
111 =head1 Description
112  
113 This Class manages .log files contained within a Document directory. Every time
114 something changes or of interest happens, a message may be written to .log.
115  
116 =head1 Examples
117  
118 use Document::LogFile;
119 my $baz = Document::LogFile->new ($filespecobj);
120 my $baz = Document::LogFile->new ($pathname);
121 my $waslogged = $baz->log ( Arf! Arf! );
122  
123 =head1 Class Variables
124  
125 None.
126  
127 =head1 Instance Variables
128  
129 None.
130  
131 =head1 Class Methods
132  
133 =over 4
134  
135 =item B<$obj = Document::LogFile-E<gt>new ($filespecobj)>
136  
137 =item B<$obj = Document::LogFile-E<gt>new ($filename)>
138  
139 Create an object to allow communications with a log file object of a Document
140 directory defined by $filename or $filespecobj. If there is currently no file
141 in it named .log, create one and emit a log creation message to it.
142  
143 Objects of this Class may be used as inputs to the Fault::Logger class.
144  
145 All of the path information comes either from a preparsed filename in
146 $filespecobj, a File::Spec::DatedPage object, or a string containing a
147 pathname to the target Document directory.
148  
149 [Can we get by with a File::Spec::Dated object?]
150  
151 =back 4
152  
153 =head1 Instance Methods
154  
155 =over 4
156  
157 =item B<$okay = $obj-E<gt>log ($string)>
158  
159 Write a time-stamped message into the .log file of the form:
160  
161 20021207223010 $string\n
162  
163 and return true if we succeeded in doing so.
164  
165 =back 4
166  
167 =head1 Private Class Methods
168  
169 None.
170  
171 =head1 Private Instance Methods
172  
173 None.
174  
175 =head1 KNOWN BUGS
176  
177 See TODO.
178  
179 =head1 SEE ALSO
180  
181 Fault::ErrorHandler, DMA::ISODate, File::Spec::DatedPage
182  
183 =head1 AUTHOR
184  
185 Dale Amon <amon@vnl.com>
186  
187 =cut
188
189 #=============================================================================
190 # CVS HISTORY
191 #=============================================================================
192 # $Log: LogFile.pm,v $
193 # Revision 1.5 2008-08-28 23:26:17 amon
194 # perldoc section regularization.
195 #
196 # Revision 1.4 2008-08-13 21:04:32 amon
197 # Third phase of reformatting to current standard.
198 #
199 # Revision 1.3 2008-08-13 20:56:53 amon
200 # Second phase of reformatting to current standard.
201 #
202 # Revision 1.2 2008-08-13 14:50:02 amon
203 # First phase of reformatting to current standard.
204 #
205 # Revision 1.1.1.1 2004-08-29 17:04:56 amon
206 # Manages a Document directory
207 #
208 # 20040813 Dale Amon <amon@islandone.org>
209 # Moved to Document: from Archivist::
210 # to make it easier to enforce layers.
211 #
212 # 20040812 Dale Amon <amon@islandone.org>
213 # *** NON-COMPATIBLE CHANGE TO API ***
214 # Dropped the use of Archivist::File::Directory and such
215 # so the class is more indendant and thus more portable
216 # to other applications.
217 #
218 # 20030108 Dale Amon <amon@vnl.com>
219 # Modified log method to check its' args, use ISODate and
220 # return t/f
221 #
222 # 20021207 Dale Amon <amon@vnl.com>
223 # Created.
224 1 1;
« Document/Directory.pm ↑ Summary File/Spec/DatedPage.pm »