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   23547 use strict;
  39         94  
  39         1335  
2 39     39   218 use warnings;
  39         95  
  39         1227  
3 39     39   780 use 5.024;
  39         138  
4              
5 39     39   222 use feature qw /postderef signatures/;
  39         111  
  39         4022  
6              
7             package Vote::Count::Log;
8 39     39   310 use Moose::Role;
  39         89  
  39         306  
9              
10 39     39   212077 no warnings 'experimental';
  39         92  
  39         2249  
11 39     39   286 use Path::Tiny 0.108;
  39         979  
  39         31416  
12              
13             our $VERSION='2.00';
14              
15             =head1 NAME
16              
17             Vote::Count::Log
18              
19             =head1 VERSION 2.00
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             =head2 logt
56              
57             Record message to the terse (.brief)
58              
59             =head2 logv
60              
61             =head2 logd
62              
63             =head1 Debug Flag
64              
65             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.
66              
67             $Election->Debug(1); # turn debug on
68             is( $Election->Action(), $expected, 'Thing Im debugging');
69             $Election->Debug(0); # turn debug off
70              
71             =cut
72              
73             has 'LogTo' => (
74             is => 'lazy',
75             is => 'rw',
76             isa => 'Str',
77             builder => '_logsetup',
78             );
79              
80             has 'LogPath' => (
81             is => 'rw',
82             isa => 'Str',
83             default => '/tmp',
84             );
85              
86             has 'LogBaseName' => (
87             is => 'rw',
88             isa => 'Str',
89             default => 'votecount'
90             );
91              
92             has 'Debug' => (
93             default => 0,
94             is => 'rw',
95             isa => 'Bool',
96             );
97              
98 142     142   124440 sub _logsetup ( $self ) {
  142         307  
  142         251  
99 142   100     536 my $pathBase = $self->{'LogPath'} || '/tmp';
100 142         1143 $pathBase =~ s/\/$|\\$//; # trim \ or / from end.
101 142 100       3199 unless ( stat $pathBase ) {
102 1         7 path($pathBase)->mkpath();
103             }
104 142   100     1023 my $baseName = $self->{'LogBaseName'} || 'votecount';
105 142         3259 return "$pathBase/$baseName";
106             }
107              
108             sub logt {
109 368     368 1 9094 my $self = shift @_;
110 368 100       1070 return $self->{'LogT'} unless (@_);
111 343         1060 my $msg = join( "\n", @_ ) . "\n";
112 343         1080 $self->{'LogT'} .= $msg;
113 343         1042 $self->{'LogV'} .= $msg;
114 343         983 $self->logd(@_);
115             }
116              
117             sub logv {
118 5550     5550 1 15852 my $self = shift @_;
119 5550 100       10704 return $self->{'LogV'} unless (@_);
120 5522         12723 my $msg = join( "\n", @_ ) . "\n";
121 5522         11974 $self->{'LogV'} .= $msg;
122 5522         11075 $self->logd(@_);
123             }
124              
125             sub logd {
126 5958     5958 1 17916 my $self = shift @_;
127 5958 100       10849 return $self->{'LogD'} unless (@_);
128 5934         11609 my @args = (@_);
129             # since ops are seqential and fast logging event times
130             # clutters the debug log.
131             # unshift @args, localtime->date . ' ' . localtime->time;
132 5934         12550 my $msg = join( "\n", @args ) . "\n";
133 5934         11894 $self->{'LogD'} .= $msg;
134 5934 100       159222 warn $msg if $self->Debug();
135             }
136              
137             sub WriteLog {
138 14     14 1 87 my $self = shift @_;
139 14         487 my $logroot = $self->LogTo();
140 14         81 path("$logroot.brief")->spew( $self->logt() );
141 14         7820 path("$logroot.full")->spew( $self->logv() );
142 14         6470 path("$logroot.debug")->spew( $self->logd() );
143             }
144              
145             1;
146              
147             #FOOTER
148              
149             =pod
150              
151             BUG TRACKER
152              
153             L<https://github.com/brainbuz/Vote-Count/issues>
154              
155             AUTHOR
156              
157             John Karr (BRAINBUZ) brainbuz@cpan.org
158              
159             CONTRIBUTORS
160              
161             Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
162              
163             LICENSE
164              
165             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>.
166              
167             SUPPORT
168              
169             This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
170              
171             =cut
172