blib/lib/XAS/Lib/Modules/Log/File.pm

filter: Statementstmt 33.3% 8 / 24 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 40.0% 2 / 5 Podpod 100% 3 / 3 totaltotal 30.9% 13 / 42 CC 6 SCAR 32.5
CC
6
Cov
24%
init_log
42.0
output
2.0
destroy
2.0
CRAP 25.8
« blib/lib/XAS/Lib/Net/Client.pm ↑ Summary blib/lib/XAS/Lib/Pidfile/Unix.pm »
1 package XAS::Lib::Modules::Log::File;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use Params::Validate 'HASHREF';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use XAS::Class
8 1 debug => 0,
9 version => $VERSION,
10 base => 'XAS::Base',
11 utils => 'dotid',
12 mixins => 'init_log output destroy',
13 messages => {
14 invperms => "unable to change file permissions on %s",
15 creatfile => "unable to create file %s"
16 }
17 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # ----------------------------------------------------------------------
20 # Public Methods
21 # ----------------------------------------------------------------------
22  
23 sub output {
24 0 ▶ my $self = shift;
Subroutine output: not called CC=1 SCAR=6.9
Pod documented
25 0 my ($args) = $self->validate_params(\@_, [
26 { type => HASHREF }
27 ]);
28  
29 $self->filename->append(
30 sprintf("[%s] %-5s - %s\n",
31 $args->{datetime}->strftime('%Y-%m-%d %H:%M:%S'),
32 uc($args->{priority}),
33 $args->{message}
34 0 ));
35  
36 }
37  
38 sub destroy {
39 0 ▶ my $self = shift;
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
40
41 }
42  
43 # ----------------------------------------------------------------------
44 # Private Methods
45 # ----------------------------------------------------------------------
46  
47 sub init_log {
48 0 ▶ my $self = shift;
Subroutine init_log: not called CC=6 SCAR=37.4
Pod documented
49  
50 # check to see if the file exists, otherwise create it
51  
52 0 ▶ unless ($self->filename->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filename->exists) 0 0 0
53  
54 0 ▶ if (my $fh = $self->filename->open('>')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->filename->open('>')) { } 0 0 0
55
56 0 $fh->close;
57  
58 } else {
59  
60 0 $self->throw_msg(
61 dotid($self->class) . '.init_log.creatfile',
62 'log_creatfile',
63 $self->filename->path
64 );
65  
66 }
67  
68 }
69  
70 # Change the file permissions to rw-rw-r, skip this on Windows
71 # as this will create a read only file.
72  
73 0 ▶ if ($^O ne "MSWin32") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 0 0 0
74  
75 0 my ($cnt, $mode, $permissions);
76  
77 # set file permissions
78  
79 0 $mode = ($self->filename->stat)[2];
80 0 $permissions = sprintf("%04o", $mode & 07777);
81  
82 0 ▶ if ($permissions ne "0664") {
Branch: 1 branch
BranchTrueFalseTotal
if ($permissions ne "0664") 0 0 0
83  
84 0 $cnt = chmod(0664, $self->filename->path);
85 0 ▶ $self->throw_msg(
Branch: 1 branch
BranchTrueFalseTotal
if $cnt < 1 0 0 0
86 dotid($self->class) . '.init_log.invperms',
87 'invperms',
88 $self->filename->path) if ($cnt < 1);
89  
90 }
91  
92 }
93  
94 }
95  
96 1 1;
97  
98 __END__
99  
100 =head1 NAME
101  
102 XAS::Lib::Modules::Log::File - A mixin class for logging
103  
104 =head1 DESCRIPTION
105  
106 This module is a mixin for logging. It logs to a file.
107  
108 =head1 METHODS
109  
110 =head2 init_log
111  
112 This method initializes the module. It uses the file specified with the
113 -filename parameter. It checks to make sure it exists, if it doesn't it
114 creates the file. On a Unix like system, it will change the file permissions
115 to rx-rx-r.
116  
117 =head2 output($hashref)
118  
119 The method formats the hashref and writes out the results.
120  
121 =head2 destroy
122  
123 This methods deinitializes the module.
124  
125 =head1 SEE ALSO
126  
127 =over 4
128  
129 =item L<XAS::Lib::Modules::Log|XAS::Lib::Modules::Log>
130  
131 =item L<XAS|XAS>
132  
133 =back
134  
135 =head1 AUTHOR
136  
137 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
138  
139 =head1 COPYRIGHT AND LICENSE
140  
141 Copyright (C) 2014 Kevin L. Esteb
142  
143 This is free software; you can redistribute it and/or modify it under
144 the terms of the Artistic License 2.0. For details, see the full text
145 of the license at http://www.perlfoundation.org/artistic_license_2_0.
146  
147 =cut
« blib/lib/XAS/Lib/Net/Client.pm ↑ Summary blib/lib/XAS/Lib/Pidfile/Unix.pm »