Count/Log.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 10 / 10 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 100% 86 / 86 CC 8 SCAR 7.7
CC
8
Cov
100%
_logsetup
4.0
logd
3.0
logt
2.0
CRAP 2.2
« Count/Common.pm ↑ Summary Count/Method/CondorcetIRV.pm »
1 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 39 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 39 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Vote::Count::Log;
8 39 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 39 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use Path::Tiny 0.108;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 39 our $VERSION='2.02';
14  
15 =head1 NAME
16  
17 Vote::Count::Log
18  
19 =head1 VERSION 2.02
20  
21 =cut
22  
23 # ABSTRACT: Logging for Vote::Count. Toolkit for vote counting.
24  
25 =head1 Vote::Count Logging Methods
26  
27 =head2 LogTo
28  
29 Sets a path and Naming pattern for writing logs with the WriteLogs method.
30  
31 'LogTo' => '/logging_path/election_name'
32  
33 LogTo will not create a new directory if the directory does not exist.
34  
35 The default log location is '/tmp/votecount'.
36  
37 =head2 LogPath
38  
39 Specifies a Path to the Log Files, unlike LogTo, LogPath will create the Path if it does not exist.
40  
41 =head2 LogBaseName
42  
43 Sets the Base portion of the logfile names, but only if LogPath is specified. The default value is votecount.
44  
45 =head2 WriteLog
46  
47 Write the logs appending '.brief', '.full', and '.debug' for the three logs where brief is a summary written with the logt (log terse) method, the full transcript log written with logv, and finally the debug log written with logd. Each higher log level captures all events of the lower log levels.
48  
49 Logged events are not written until WriteLog is called. A fatal runtime error, would prevent execution of a writelog at the end of the script. If you need to see the logs when your program is crashing, set the Debug Flag to write the events as warnings to STDERR while the script is running.
50  
51 =head1 Logging Events
52  
53 When logging from your methods, use logt for events that produce a summary, use logv for events that should be in the full transcript such as round counts, and finally debug is for events that may be helpful in debugging but which should not be in the transcript. Events written to logt will be included in the verbose log and all events in the verbose log will be in the debug log.
54  
55 The logx methods will return the current log if called without any message to log.
56  
57 =head2 logt
58  
59 Record message to the terse (.brief) log.
60  
61 =head2 logv
62  
63 Record message to the more verbose (.full) log.
64  
65 =head2 logd
66  
67 Record message to the debug (.debug) log.
68  
69 =head1 Debug Flag
70  
71 When the debug flag is logx methods will also emit the event as a warning (STDERR). The Debug Flag defaults to off (0), but can be explicitly set via the new method of a Vote::Count object, or toggled by passing 0 or 1 via the Debug Method.
72  
73 $Election->Debug(1); # turn debug on
74 is( $Election->Action(), $expected, 'Thing Im debugging');
75 $Election->Debug(0); # turn debug off
76  
77 =cut
78  
79 39 has 'LogTo' => (
80 is => 'lazy',
81 is => 'rw',
82 isa => 'Str',
83 builder => '_logsetup',
84 );
85  
86 39 has 'LogPath' => (
87 is => 'rw',
88 isa => 'Str',
89 default => '/tmp',
90 );
91  
92 39 has 'LogBaseName' => (
93 is => 'rw',
94 isa => 'Str',
95 default => 'votecount'
96 );
97  
98 39 has 'Debug' => (
99 default => 0,
100 is => 'rw',
101 isa => 'Bool',
102 );
103  
104 ▶sub _logsetup ( $self ) {
Subroutine _logsetup: called CC=4 SCAR=13.9
105 158 ▶ my $pathBase = $self->{'LogPath'} || '/tmp';
Condition100%2/2
$self->{'LogPath'} || '/tmp'
l!l
14414
MC/DC100%1/1
$self->{'LogPath'} || '/tmp'
$self->{'LogPath'}
Truth table100%2/2
A
A= $self->{'LogPath'}
Aresult
11
00
106 158 $pathBase =~ s/\/$|\\$//; # trim \ or / from end.
107 158 ▶ unless ( stat $pathBase ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (stat $pathBase) 1 157 158
108 1 path($pathBase)->mkpath();
109 }
110 158 ▶ my $baseName = $self->{'LogBaseName'} || 'votecount';
Condition100%2/2
$self->{'LogBaseName'} || 'votecount'
l!l
14513
MC/DC100%1/1
$self->{'LogBaseName'} || 'votecount'
$self->{'LogBaseName'}
Truth table100%2/2
A
A= $self->{'LogBaseName'}
Aresult
11
00
111 158 return "$pathBase/$baseName";
112 }
113  
114 sub logt {
115 639 ▶ my $self = shift @_;
Subroutine logt: called CC=2 SCAR=6.9
Pod documented
116 639 ▶ return $self->{'LogT'} unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 26 613 639
117 613 my $msg = join( "\n", @_ ) . "\n";
118 613 $self->{'LogT'} .= $msg;
119 613 $self->{'LogV'} .= $msg;
120 613 $self->logd(@_);
121 }
122  
123 sub logv {
124 6749 ▶ my $self = shift @_;
Subroutine logv: called CC=2 SCAR=6.9
Pod documented
125 6749 ▶ return $self->{'LogV'} unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 30 6719 6749
126 6719 my $msg = join( "\n", @_ ) . "\n";
127 6719 $self->{'LogV'} .= $msg;
128 6719 $self->logd(@_);
129 }
130  
131 sub logd {
132 7426 ▶ my $self = shift @_;
Subroutine logd: called CC=3 SCAR=11.0
Pod documented
133 7426 ▶ return $self->{'LogD'} unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 24 7402 7426
134 7402 my @args = (@_);
135 # since ops are seqential and fast logging event times
136 # clutters the debug log.
137 # unshift @args, localtime->date . ' ' . localtime->time;
138 7402 my $msg = join( "\n", @args ) . "\n";
139 7402 $self->{'LogD'} .= $msg;
140 7402 ▶ warn $msg if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 1 7401 7402
141 }
142  
143 sub WriteLog {
144 14 ▶ my $self = shift @_;
Subroutine WriteLog: called CC=1 SCAR=0.0
Pod documented
145 14 my $logroot = $self->LogTo();
146 14 path("$logroot.brief")->spew( $self->logt() );
147 14 path("$logroot.full")->spew( $self->logv() );
148 14 path("$logroot.debug")->spew( $self->logd() );
149 }
150  
151 39 1;
152  
153 #FOOTER
154  
155 =pod
156  
157 BUG TRACKER
158  
159 L<https://github.com/brainbuz/Vote-Count/issues>
160  
161 AUTHOR
162  
163 John Karr (BRAINBUZ) brainbuz@cpan.org
164  
165 CONTRIBUTORS
166  
167 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
168  
169 LICENSE
170  
171 This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>.
172  
173 SUPPORT
174  
175 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
176  
177 =cut
178  
« Count/Common.pm ↑ Summary Count/Method/CondorcetIRV.pm »