File Coverage

blib/lib/Vote/Count/Log.pm
Criterion Covered Total %
statement 51 51 100.0
branch 10 10 100.0
condition 4 4 100.0
subroutine 12 12 100.0
pod 4 4 100.0
total 81 81 100.0


line stmt bran cond sub pod time code
1 39     39   21277 use strict;
  39         84  
  39         1177  
2 39     39   202 use warnings;
  39         79  
  39         1064  
3 39     39   724 use 5.024;
  39         121  
4              
5 39     39   217 use feature qw /postderef signatures/;
  39         72  
  39         3476  
6              
7             package Vote::Count::Log;
8 39     39   252 use Moose::Role;
  39         107  
  39         327  
9              
10 39     39   194850 no warnings 'experimental';
  39         88  
  39         2069  
11 39     39   248 use Path::Tiny 0.108;
  39         967  
  39         28047  
12              
13             our $VERSION='2.01';
14              
15             =head1 NAME
16              
17             Vote::Count::Log
18              
19             =head1 VERSION 2.01
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             has 'LogTo' => (
80             is => 'lazy',
81             is => 'rw',
82             isa => 'Str',
83             builder => '_logsetup',
84             );
85              
86             has 'LogPath' => (
87             is => 'rw',
88             isa => 'Str',
89             default => '/tmp',
90             );
91              
92             has 'LogBaseName' => (
93             is => 'rw',
94             isa => 'Str',
95             default => 'votecount'
96             );
97              
98             has 'Debug' => (
99             default => 0,
100             is => 'rw',
101             isa => 'Bool',
102             );
103              
104 157     157   75157 sub _logsetup ( $self ) {
  157         275  
  157         232  
105 157   100     504 my $pathBase = $self->{'LogPath'} || '/tmp';
106 157         786 $pathBase =~ s/\/$|\\$//; # trim \ or / from end.
107 157 100       3303 unless ( stat $pathBase ) {
108 1         7 path($pathBase)->mkpath();
109             }
110 157   100     984 my $baseName = $self->{'LogBaseName'} || 'votecount';
111 157         4297 return "$pathBase/$baseName";
112             }
113              
114             sub logt {
115 637     637 1 11500 my $self = shift @_;
116 637 100       1788 return $self->{'LogT'} unless (@_);
117 611         1802 my $msg = join( "\n", @_ ) . "\n";
118 611         1797 $self->{'LogT'} .= $msg;
119 611         1767 $self->{'LogV'} .= $msg;
120 611         1757 $self->logd(@_);
121             }
122              
123             sub logv {
124 6482     6482 1 16992 my $self = shift @_;
125 6482 100       11903 return $self->{'LogV'} unless (@_);
126 6452         15150 my $msg = join( "\n", @_ ) . "\n";
127 6452         13644 $self->{'LogV'} .= $msg;
128 6452         12842 $self->logd(@_);
129             }
130              
131             sub logd {
132 7157     7157 1 19682 my $self = shift @_;
133 7157 100       13033 return $self->{'LogD'} unless (@_);
134 7133         13130 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 7133         14579 my $msg = join( "\n", @args ) . "\n";
139 7133         14294 $self->{'LogD'} .= $msg;
140 7133 100       184424 warn $msg if $self->Debug();
141             }
142              
143             sub WriteLog {
144 14     14 1 70 my $self = shift @_;
145 14         405 my $logroot = $self->LogTo();
146 14         72 path("$logroot.brief")->spew( $self->logt() );
147 14         9178 path("$logroot.full")->spew( $self->logv() );
148 14         10045 path("$logroot.debug")->spew( $self->logd() );
149             }
150              
151             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