| 1 | package App::Sysadmin::Log::Simple::File; | ||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: a file-logger for App::Sysadmin::Log::Simple | ||||||||||||||||||||||||||||||||||
| 5 | 5 | our $VERSION = '0.009'; # VERSION | |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use autodie qw(:file :filesys); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 14 | 5 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 15 | 5 | my %opts = @_; | |||||||||||||||||||||||||||||||||
| 16 | 5 | my $app = $opts{app}; | |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | return bless { | ||||||||||||||||||||||||||||||||||
| 19 | logdir => path( $app->{logdir} || $opts{logdir} || qw(/ var log sysadmin) ), | ||||||||||||||||||||||||||||||||||
| 20 | index_preamble => $app->{index_preamble}, | ||||||||||||||||||||||||||||||||||
| 21 | view_preamble => $app->{view_preamble}, | ||||||||||||||||||||||||||||||||||
| 22 | date => $app->{date}, | ||||||||||||||||||||||||||||||||||
| 23 | user => $app->{user}, | ||||||||||||||||||||||||||||||||||
| 24 | do_file => $app->{do_file}, | ||||||||||||||||||||||||||||||||||
| 25 | 5 | ▶ | }, $class; | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
$app->{'logdir'} || $opts{'logdir'} || ('/', 'var', 'log', 'sysadmin')
$app->{"logdir"} or $opts{"logdir"}
MC/DC0%0/3
$app->{'logdir'} || $opts{'logdir'} || ('/', 'var', 'log', 'sysadmin')
$app->{"logdir"}
$opts{"logdir"}
('/', 'var', 'log', 'sysadmin')
Truth table75%3/4
A or B || C
A= $app->{"logdir"}B= $opts{"logdir"}C= ('/', 'var', 'log', 'sysadmin')
| |||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub view { | ||||||||||||||||||||||||||||||||
|
Subroutine
view: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 30 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 31 | 1 | my $year = $self->{date}->year; | |||||||||||||||||||||||||||||||||
| 32 | 1 | my $month = $self->{date}->month; | |||||||||||||||||||||||||||||||||
| 33 | 1 | my $day = $self->{date}->day; | |||||||||||||||||||||||||||||||||
| 34 | 1 | require IO::Pager; | |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 1 | my $logfile = path($self->{logdir}, $year, $month, "$day.log"); | |||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | die "No log for $year/$month/$day\n" unless $logfile->is_file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 1 | my $logfh = $logfile->openr_utf8; | |||||||||||||||||||||||||||||||||
| 40 | local $STDOUT = IO::Pager->new(*STDOUT) | ||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | unless $ENV{__PACKAGE__.' under test'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | say($self->{view_preamble}) if $self->{view_preamble}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | print while (<$logfh>); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 44 | 1 | close $logfh; | |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 1 | return; | |||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub log { | ||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 51 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 52 | 2 | my $line = shift; | |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | return unless $self->{do_file}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | $self->{logdir}->mkpath unless $self->{logdir}->is_dir; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | 2 | my $year = $self->{date}->year; | |||||||||||||||||||||||||||||||||
| 59 | 2 | my $month = $self->{date}->month; | |||||||||||||||||||||||||||||||||
| 60 | 2 | my $day = $self->{date}->day; | |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | 2 | my $dir = path($self->{logdir}, $year, $month); | |||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | $dir->mkpath unless $dir->is_dir; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 64 | 2 | my $logfile = path($self->{logdir}, $year, $month, "$day.log"); | |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | # Start a new log file if one doesn't exist already | ||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | unless ($logfile->is_file) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | 2 | open my $logfh, '>>', $logfile; | |||||||||||||||||||||||||||||||||
| 69 | 2 | my $line = $self->{date}->day_name . ' ' . $self->{date}->month_name . " $day, $year"; | |||||||||||||||||||||||||||||||||
| 70 | 2 | say $logfh $line; | |||||||||||||||||||||||||||||||||
| 71 | 2 | say $logfh '=' x length($line), "\n"; | |||||||||||||||||||||||||||||||||
| 72 | 2 | close $logfh; # Explicitly close before calling generate_index() so the file is found | |||||||||||||||||||||||||||||||||
| 73 | 2 | $self->_generate_index(); | |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | 2 | open my $logfh, '>>', $logfile; | |||||||||||||||||||||||||||||||||
| 77 | 2 | my $timestamp = $self->{date}->hms; | |||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | my $user = $ENV{SUDO_USER} || $ENV{USER}; # We need to know who wrote this | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{'SUDO_USER'} || $ENV{'USER'}
MC/DC0%0/2
$ENV{'SUDO_USER'} || $ENV{'USER'}
$ENV{'SUDO_USER'}
$ENV{'USER'}
Truth table33%1/3
A || B
A= $ENV{'SUDO_USER'}B= $ENV{'USER'}
| |||||||||||||||||||||||||||||||||||
| 79 | 2 | say $logfh " $timestamp $user:\t$line"; | |||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | # This might be run as root, so fix up ownership and | ||||||||||||||||||||||||||||||||||
| 82 | # permissions so mortals can log to files root started | ||||||||||||||||||||||||||||||||||
| 83 | 2 | my ($uid, $gid) = (getpwnam($self->{user}))[2,3]; | |||||||||||||||||||||||||||||||||
| 84 | 2 | chown $uid, $gid, $logfile; | |||||||||||||||||||||||||||||||||
| 85 | 0 | chmod 0644, $logfile; | |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 0 | return "Logged to $logfile"; | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | sub _generate_index { | ||||||||||||||||||||||||||||||||
|
Subroutine
_generate_index: called CC=9 SCAR=22.7
| |||||||||||||||||||||||||||||||||||
| 91 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 92 | 3 | require File::Find::Rule; | |||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 3 | my $indexfh = path($self->{logdir}, 'index.log')->openw_utf8; # clobbers the file | |||||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | say $indexfh $self->{index_preamble} if defined $self->{index_preamble}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | # Find relevant log files | ||||||||||||||||||||||||||||||||||
| 98 | 3 | my @files = File::Find::Rule->mindepth(3)->in($self->{logdir}); | |||||||||||||||||||||||||||||||||
| 99 | 3 | my @dates; | |||||||||||||||||||||||||||||||||
| 100 | 3 | foreach (@files) { | |||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | if (m{ | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 102 | (?<year>\d{4}) | ||||||||||||||||||||||||||||||||||
| 103 | / | ||||||||||||||||||||||||||||||||||
| 104 | (?<month>\d{1,2}) | ||||||||||||||||||||||||||||||||||
| 105 | / | ||||||||||||||||||||||||||||||||||
| 106 | (?<day>\d{1,2}) | ||||||||||||||||||||||||||||||||||
| 107 | }x) { # Extract the date | ||||||||||||||||||||||||||||||||||
| 108 | 2 | push @dates, [$+{year}, $+{month}, $+{day}]; | |||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||||||||
| 111 | 0 | warn "WTF: $_"; | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | # Sort by year, then by month, then by day | ||||||||||||||||||||||||||||||||||
| 115 | 2 | @dates = map { $_->[0] } | |||||||||||||||||||||||||||||||||
| 116 | 0 | sort { $b->[1] <=> $a->[1] } | |||||||||||||||||||||||||||||||||
| 117 | 3 | map { [ $_, $_->[0]*1000 + $_->[1]*10 + $_->[2] ] } | |||||||||||||||||||||||||||||||||
| 118 | @dates; | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | # Keep track of | ||||||||||||||||||||||||||||||||||
| 121 | 3 | my $lastyear = 0; | |||||||||||||||||||||||||||||||||
| 122 | 3 | my $lastmonth = 0; | |||||||||||||||||||||||||||||||||
| 123 | 3 | for my $date (@dates) { | |||||||||||||||||||||||||||||||||
| 124 | 2 | my $year = $date->[0]; | |||||||||||||||||||||||||||||||||
| 125 | 2 | my $month = $date->[1]; | |||||||||||||||||||||||||||||||||
| 126 | 2 | my $day = $date->[2]; | |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | 2 | ▶ | if ($year != $lastyear) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 129 | 2 | say $indexfh "\n$year"; | |||||||||||||||||||||||||||||||||
| 130 | 2 | say $indexfh "-" x length($year); | |||||||||||||||||||||||||||||||||
| 131 | 2 | $lastyear = $year; | |||||||||||||||||||||||||||||||||
| 132 | 2 | $lastmonth = 0; | |||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | if ($month != $lastmonth) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 135 | 2 | say $indexfh "\n### $month ###\n"; | |||||||||||||||||||||||||||||||||
| 136 | 2 | $lastmonth = $month; | |||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | if ($year == $lastyear and $month == $lastmonth) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$year == $lastyear and $month == $lastmonth
MC/DC0%0/2
$year == $lastyear and $month == $lastmonth
$year == $lastyear
$month == $lastmonth
Truth table33%1/3
A and B
A= $year == $lastyearB= $month == $lastmonth
| |||||||||||||||||||||||||||||||||||
| 139 | 2 | say $indexfh "[$day]($year/$month/$day)" | |||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||
| 142 | 3 | close $indexfh; | |||||||||||||||||||||||||||||||||
| 143 | 3 | return; | |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 5 | 1; | |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||||