Log/Dual.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Image/Jpg.pm ↑ Summary Log/File.pm »
1 #
2 # $Id$
3 #
4 # log::dual Brik
5 #
6 package Metabrik::Log::Dual;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Core::Log);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable logging) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 level => [ qw(0|1|2|3) ],
21 output => [ qw(file) ],
22 time_prefix => [ qw(0|1) ],
23 text_prefix => [ qw(0|1) ],
24 _fd => [ qw(file_descriptor) ],
25 },
26 attributes_default => {
27 time_prefix => 0,
28 text_prefix => 0,
29 },
30 commands => {
31 message => [ qw(string caller|OPTIONAL) ],
32 info => [ qw(string caller|OPTIONAL) ],
33 verbose => [ qw(string caller|OPTIONAL) ],
34 warning => [ qw(string caller|OPTIONAL) ],
35 error => [ qw(string caller|OPTIONAL) ],
36 fatal => [ qw(string caller|OPTIONAL) ],
37 debug => [ qw(string caller|OPTIONAL) ],
38 },
39 require_modules => {
40 'Term::ANSIColor' => [ ],
41 },
42 };
43 }
44  
45 sub brik_use_properties {
46 my $self = shift;
47  
48 my $datadir = $self->datadir;
49  
50 return {
51 attributes_default => {
52 debug => $self->log->debug,
53 level => $self->log->level,
54 output => $datadir.'/output.log',
55 },
56 };
57 }
58  
59 sub brik_init {
60 my $self = shift;
61  
62 my $output = $self->output;
63 open(my $fd, '>>', $output)
64 or return $self->log->error("brik_init: can't open output file [$output]: $!");
65  
66 # Makes the file handle unbuffered
67 my $current = select;
68 select($fd);
69 $|++;
70 select($current);
71  
72 $self->_fd($fd);
73  
74 return $self->SUPER::brik_init;
75 }
76  
77 sub _print {
78 my $self = shift;
79 my ($buffer) = @_;
80  
81 my $fd = $self->_fd;
82  
83 print $fd $buffer;
84 print $buffer;
85  
86 return 1;
87 }
88  
89 sub warning {
90 my $self = shift;
91 my ($msg, $caller) = @_;
92  
93 my $prefix = $self->text_prefix ? 'WARN ' : '[!]';
94 my $time = $self->time_prefix ? localtime().' ' : '';
95 my $buffer = $time."$prefix ".$self->message($msg, ($caller) ||= caller());
96  
97 return $self->_print($buffer);
98 }
99  
100 sub error {
101 my $self = shift;
102 my ($msg, $caller) = @_;
103  
104 my $prefix = $self->text_prefix ? 'ERROR' : '[-]';
105 my $time = $self->time_prefix ? localtime().' ' : '';
106 my $buffer = $time."$prefix ".$self->message($msg, ($caller) ||= caller());
107  
108 $self->_print($buffer);
109  
110 # Returning undef is my official way of stating an error occured:
111 # Number 0 is for stating a false condition occured, not not error.
112 return;
113 }
114  
115 sub fatal {
116 my $self = shift;
117 my ($msg, $caller) = @_;
118  
119 my $prefix = $self->text_prefix ? 'FATAL' : '[F]';
120 my $time = $self->time_prefix ? localtime().' ' : '';
121 my $buffer = $time."$prefix ".$self->message($msg, ($caller) ||= caller());
122  
123 my $fd = $self->_fd;
124  
125 print $fd $buffer;
126 die($buffer);
127 }
128  
129 sub info {
130 my $self = shift;
131 my ($msg, $caller) = @_;
132  
133 return 1 unless $self->level > 0;
134  
135 my $prefix = $self->text_prefix ? 'INFO ' : '[+]';
136 my $time = $self->time_prefix ? localtime().' ' : '';
137 my $buffer = $time."$prefix ".$self->message($msg, ($caller) ||= caller());
138  
139 return $self->_print($buffer);
140 }
141  
142 sub verbose {
143 my $self = shift;
144 my ($msg, $caller) = @_;
145  
146 return 1 unless $self->level > 1;
147  
148 my $prefix = $self->text_prefix ? 'VERB ' : '[*]';
149 my $time = $self->time_prefix ? localtime().' ' : '';
150 my $buffer = $time."$prefix ".$self->message($msg, ($caller) ||= caller());
151  
152 return $self->_print($buffer);
153 }
154  
155 sub debug {
156 my $self = shift;
157 my ($msg, $caller) = @_;
158  
159 # We have a conflict between the method and the accessor,
160 # we have to identify which one is accessed.
161  
162 # If no message defined, we want to access the Attribute
163 if (! defined($msg)) {
164 return $self->{debug};
165 }
166 else {
167 # If $msg is either 1 or 0, we want to set the Attribute
168 if ($msg =~ /^(?:1|0)$/) {
169 return $self->{debug} = $msg;
170 }
171 else {
172 return 1 unless $self->level > 2;
173  
174 my $prefix = $self->text_prefix ? 'DEBUG' : '[D]';
175 my $time = $self->time_prefix ? localtime().' ' : '';
176 my $buffer = $time."$prefix ".$self->message($msg, ($caller) ||= caller());
177  
178 $self->_print($buffer);
179 }
180 }
181  
182 return 1;
183 }
184  
185 sub brik_fini {
186 my $self = shift;
187  
188 my $fd = $self->_fd;
189 if (defined($fd)) {
190 close($fd);
191 $self->_fd(undef);
192 }
193  
194 return 1;
195 }
196  
197 1;
198  
199 __END__
« Image/Jpg.pm ↑ Summary Log/File.pm »