| 1 |
package App::Yath::Options::Logging; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use POSIX qw/strftime/; |
| 8 |
use Test2::Harness::Util qw/clean_path/; |
| 9 |
use File::Spec; |
| 10 |
|
| 11 |
use App::Yath::Options; |
| 12 |
|
| 13 |
option_group {prefix => 'logging', category => "Logging Options"} => sub { |
| 14 |
option log => ( |
| 15 |
short => 'L', |
| 16 |
description => 'Turn on logging', |
| 17 |
); |
| 18 |
|
| 19 |
option log_file_format => ( |
| 20 |
alt => ['lff'], |
| 21 |
type => 's', |
| 22 |
|
| 23 |
env_vars => [qw/YATH_LOG_FILE_FORMAT TEST2_HARNESS_LOG_FORMAT/], |
| 24 |
default => sub { '%!P%Y-%m-%d_%H:%M:%S_%!U.jsonl' }, |
| 25 |
|
| 26 |
description => 'Specify the format for automatically-generated log files. Overridden by --log-file, if given. This option implies -L (Default: \$YATH_LOG_FILE_FORMAT, if that is set, or else "%!P%Y-%m-%d~%H:%M:%S~%!U~%!p.jsonl"). This is a string in which percent-escape sequences will be replaced as per POSIX::strftime. The following special escape sequences are also replaced: (%!P : Project name followed by a ~, if a project is defined, otherwise empty string) (%!U : the unique test run ID) (%!p : the process ID) (%!S : the number of seconds since local midnight UTC)', |
| 27 |
|
| 28 |
); |
| 29 |
|
| 30 |
option bzip2 => ( |
| 31 |
short => 'B', |
| 32 |
alt => ['bz2', 'bzip2_log'], |
| 33 |
description => 'Use bzip2 compression when writing the log. This option implies -L. The .bz2 prefix is added to log file name for you', |
| 34 |
); |
| 35 |
|
| 36 |
option gzip => ( |
| 37 |
short => 'G', |
| 38 |
alt => ['gz', 'gzip_log'], |
| 39 |
description => 'Use gzip compression when writing the log. This option implies -L. The .gz prefix is added to log file name for you', |
| 40 |
); |
| 41 |
|
| 42 |
option log_dir => ( |
| 43 |
type => 's', |
| 44 |
normalize => \&clean_path, |
| 45 |
description => 'Specify a log directory. Will fall back to the system temp dir.', |
| 46 |
); |
| 47 |
|
| 48 |
option log_file => ( |
| 49 |
short => 'F', |
| 50 |
type => 's', |
| 51 |
normalize => \&clean_path, |
| 52 |
description => "Specify the name of the log file. This option implies -L.", |
| 53 |
); |
| 54 |
|
| 55 |
post \&post_process; |
| 56 |
}; |
| 57 |
|
| 58 |
sub post_process { |
| 59 |
my %params = @_; |
| 60 |
my $settings = $params{settings}; |
| 61 |
my $logging = $settings->logging; |
| 62 |
|
| 63 |
die "You cannot specify both bzip2-log and gzip-log\n" if $logging->bzip2 && $logging->gzip; |
| 64 |
|
| 65 |
return unless $logging->log || $logging->bzip2 || $logging->gzip || $logging->log_file; |
| 66 |
|
| 67 |
# We want to keep the log and put it in a findable location |
| 68 |
$logging->field(log => 1); |
| 69 |
|
| 70 |
unless ($logging->log_file) { |
| 71 |
my $log_dir = $logging->log_dir // ($settings->check_prefix('workspace') ? $settings->workspace->tmp_dir : File::Spec->tmpdir); |
| 72 |
|
| 73 |
mkdir($log_dir) or die "Could not create dir '$log_dir': $!" |
| 74 |
unless -d $log_dir; |
| 75 |
|
| 76 |
my $format = $logging->log_file_format; |
| 77 |
my $filename = expand_log_file_format($format, $settings); |
| 78 |
$logging->field(log_file => clean_path(File::Spec->catfile($log_dir, $filename))); |
| 79 |
} |
| 80 |
|
| 81 |
my $log_file = $logging->log_file; |
| 82 |
$log_file =~ s{/+$}{}g; |
| 83 |
$log_file =~ s/\.(gz|bz2)$//; |
| 84 |
$log_file =~ s/\.jsonl?$//; |
| 85 |
$log_file .= "\.jsonl"; |
| 86 |
$log_file .= "\.bz2" if $logging->bzip2; |
| 87 |
$log_file .= "\.gz" if $logging->gzip; |
| 88 |
$logging->field(log_file => $log_file); |
| 89 |
} |
| 90 |
|
| 91 |
sub time_for_strftime { time() } |
| 92 |
|
| 93 |
sub expand_log_file_format { |
| 94 |
my ($pattern, $settings) = @_; |
| 95 |
my $before = $pattern; |
| 96 |
$pattern =~ s{%!(\w)}{expand($1, $settings)}ge; |
| 97 |
my $res = strftime($pattern, localtime(time_for_strftime())); |
| 98 |
return $res; |
| 99 |
} |
| 100 |
|
| 101 |
sub expand { |
| 102 |
my ($letter, $settings) = @_; |
| 103 |
# This could be driven by a hash, but for now if-else is easiest |
| 104 |
if ($letter eq "U") { return $settings->run->run_id } |
| 105 |
elsif ($letter eq "p") { return $$ } |
| 106 |
elsif ($letter eq "P") { |
| 107 |
my $project = $settings->harness->project // return ""; |
| 108 |
return $project . "~"; |
| 109 |
} |
| 110 |
elsif ($letter eq "S") { |
| 111 |
# Number of seconds since midnight |
| 112 |
my ($s, $m, $h) = (localtime(time_for_strftime()))[0, 1, 2]; |
| 113 |
return sprintf("%05d", $s + 60 * $m + 3600 * $h); |
| 114 |
} |
| 115 |
else { |
| 116 |
# unrecognized `%!x` expansion. Should we warn? Die? |
| 117 |
return "%!$letter"; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
1; |
| 122 |
|
| 123 |
__END__ |
| 124 |
|
| 125 |
=pod |
| 126 |
|
| 127 |
=encoding UTF-8 |
| 128 |
|
| 129 |
=head1 NAME |
| 130 |
|
| 131 |
App::Yath::Options::Logging - Logging options for yath |
| 132 |
|
| 133 |
=head1 DESCRIPTION |
| 134 |
|
| 135 |
This is where the command line options for logging are defined. |
| 136 |
|
| 137 |
=head1 PROVIDED OPTIONS |
| 138 |
|
| 139 |
=head2 COMMAND OPTIONS |
| 140 |
|
| 141 |
=head3 Logging Options |
| 142 |
|
| 143 |
=over 4 |
| 144 |
|
| 145 |
=item --bzip2 |
| 146 |
|
| 147 |
=item --bz2 |
| 148 |
|
| 149 |
=item --bzip2_log |
| 150 |
|
| 151 |
=item -B |
| 152 |
|
| 153 |
=item --no-bzip2 |
| 154 |
|
| 155 |
Use bzip2 compression when writing the log. This option implies -L. The .bz2 prefix is added to log file name for you |
| 156 |
|
| 157 |
|
| 158 |
=item --gzip |
| 159 |
|
| 160 |
=item --gz |
| 161 |
|
| 162 |
=item --gzip_log |
| 163 |
|
| 164 |
=item -G |
| 165 |
|
| 166 |
=item --no-gzip |
| 167 |
|
| 168 |
Use gzip compression when writing the log. This option implies -L. The .gz prefix is added to log file name for you |
| 169 |
|
| 170 |
|
| 171 |
=item --log |
| 172 |
|
| 173 |
=item -L |
| 174 |
|
| 175 |
=item --no-log |
| 176 |
|
| 177 |
Turn on logging |
| 178 |
|
| 179 |
|
| 180 |
=item --log-dir ARG |
| 181 |
|
| 182 |
=item --log-dir=ARG |
| 183 |
|
| 184 |
=item --no-log-dir |
| 185 |
|
| 186 |
Specify a log directory. Will fall back to the system temp dir. |
| 187 |
|
| 188 |
|
| 189 |
=item --log-file ARG |
| 190 |
|
| 191 |
=item --log-file=ARG |
| 192 |
|
| 193 |
=item -F ARG |
| 194 |
|
| 195 |
=item -F=ARG |
| 196 |
|
| 197 |
=item --no-log-file |
| 198 |
|
| 199 |
Specify the name of the log file. This option implies -L. |
| 200 |
|
| 201 |
|
| 202 |
=item --log-file-format ARG |
| 203 |
|
| 204 |
=item --log-file-format=ARG |
| 205 |
|
| 206 |
=item --lff ARG |
| 207 |
|
| 208 |
=item --lff=ARG |
| 209 |
|
| 210 |
=item --no-log-file-format |
| 211 |
|
| 212 |
Specify the format for automatically-generated log files. Overridden by --log-file, if given. This option implies -L (Default: \$YATH_LOG_FILE_FORMAT, if that is set, or else "%!P%Y-%m-%d~%H:%M:%S~%!U~%!p.jsonl"). This is a string in which percent-escape sequences will be replaced as per POSIX::strftime. The following special escape sequences are also replaced: (%!P : Project name followed by a ~, if a project is defined, otherwise empty string) (%!U : the unique test run ID) (%!p : the process ID) (%!S : the number of seconds since local midnight UTC) |
| 213 |
|
| 214 |
Can also be set with the following environment variables: C<YATH_LOG_FILE_FORMAT>, C<TEST2_HARNESS_LOG_FORMAT> |
| 215 |
|
| 216 |
|
| 217 |
=back |
| 218 |
|
| 219 |
=head1 SOURCE |
| 220 |
|
| 221 |
The source code repository for Test2-Harness can be found at |
| 222 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 223 |
|
| 224 |
=head1 MAINTAINERS |
| 225 |
|
| 226 |
=over 4 |
| 227 |
|
| 228 |
=item Chad Granum E<lt>[email protected]<gt> |
| 229 |
|
| 230 |
=back |
| 231 |
|
| 232 |
=head1 AUTHORS |
| 233 |
|
| 234 |
=over 4 |
| 235 |
|
| 236 |
=item Chad Granum E<lt>[email protected]<gt> |
| 237 |
|
| 238 |
=back |
| 239 |
|
| 240 |
=head1 COPYRIGHT |
| 241 |
|
| 242 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 243 |
|
| 244 |
This program is free software; you can redistribute it and/or |
| 245 |
modify it under the same terms as Perl itself. |
| 246 |
|
| 247 |
See F<http://dev.perl.org/licenses/> |
| 248 |
|
| 249 |
=cut |