logging: Rename pfx to label

This is to prepare for a general log prefix mechanism to improve log
readability.
This commit is contained in:
Richard Hansen 2024-07-25 02:04:28 -04:00
parent 015600d72f
commit 3262dd0952
2 changed files with 29 additions and 28 deletions

View file

@ -2348,7 +2348,7 @@ my $_in_logmsg = 0;
sub logmsg { sub logmsg {
my %args = ( my %args = (
msg => '', msg => '',
pfx => '', label => '',
fh => *STDERR, fh => *STDERR,
email => 0, # If truthy, the message is also included in the next email. email => 0, # If truthy, the message is also included in the next email.
(@_ % 2) ? (msg => pop) : (), (@_ % 2) ? (msg => pop) : (),
@ -2357,7 +2357,7 @@ sub logmsg {
my $buffer = $args{msg}; my $buffer = $args{msg};
chomp($buffer); chomp($buffer);
my $prefix = $args{pfx}; my $prefix = $args{label};
$prefix = sprintf "%-8s ", $prefix if $prefix; $prefix = sprintf "%-8s ", $prefix if $prefix;
if ($file) { if ($file) {
$prefix .= "file $file"; $prefix .= "file $file";
@ -2383,12 +2383,13 @@ sub logmsg {
} }
} }
sub _logmsg_fmt { return (@_ > 1) ? sprintf(shift, @_) : shift; } sub _logmsg_fmt { return (@_ > 1) ? sprintf(shift, @_) : shift; }
sub info { logmsg(email => 1, pfx => 'INFO:', _logmsg_fmt(@_)) if opt('verbose'); } sub info { logmsg(email => 1, label => 'INFO:', _logmsg_fmt(@_)) if opt('verbose'); }
sub debug { logmsg( pfx => 'DEBUG:', _logmsg_fmt(@_)) if opt('debug'); } sub debug { logmsg( label => 'DEBUG:', _logmsg_fmt(@_)) if opt('debug'); }
sub warning { logmsg(email => 1, pfx => 'WARNING:', _logmsg_fmt(@_)); } sub warning { logmsg(email => 1, label => 'WARNING:', _logmsg_fmt(@_)); }
sub fatal { logmsg(email => 1, pfx => 'FATAL:', _logmsg_fmt(@_)); sendmail(); exit(1); } sub fatal { logmsg(email => 1, label => 'FATAL:', _logmsg_fmt(@_)); sendmail(); exit(1); }
sub success { logmsg(email => 1, pfx => 'SUCCESS:', _logmsg_fmt(@_)); } sub success { logmsg(email => 1, label => 'SUCCESS:', _logmsg_fmt(@_)); }
sub failed { logmsg(email => 1, pfx => 'FAILED:', _logmsg_fmt(@_)); $result = 'FAILED'; } sub failed { logmsg(email => 1, label => 'FAILED:', _logmsg_fmt(@_)); $result = 'FAILED'; }
sub prettytime { return scalar(localtime(shift)); } sub prettytime { return scalar(localtime(shift)); }
sub prettyinterval { sub prettyinterval {

View file

@ -53,51 +53,51 @@ my @test_cases = (
want => "foo\n", want => "foo\n",
}, },
{ {
desc => 'single-line prefix', desc => 'single-line label',
args => [pfx => 'PFX:', 'foo'], args => [label => 'LBL:', 'foo'],
want => "PFX: > foo\n", want => "LBL: > foo\n",
}, },
{ {
desc => 'multi-line prefix', desc => 'multi-line label',
args => [pfx => 'PFX:', "foo\nbar"], args => [label => 'LBL:', "foo\nbar"],
want => "PFX: > foo\nPFX: bar\n", want => "LBL: > foo\nLBL: bar\n",
}, },
{ {
desc => 'single-line long prefix', desc => 'single-line long label',
args => [pfx => 'VERY LONG PREFIX:', 'foo'], args => [label => 'VERY LONG LABEL:', 'foo'],
want => "VERY LONG PREFIX: > foo\n", want => "VERY LONG LABEL: > foo\n",
}, },
{ {
desc => 'multi-line long prefix', desc => 'multi-line long label',
args => [pfx => 'VERY LONG PREFIX:', "foo\nbar"], args => [label => 'VERY LONG LABEL:', "foo\nbar"],
want => "VERY LONG PREFIX: > foo\nVERY LONG PREFIX: bar\n", want => "VERY LONG LABEL: > foo\nVERY LONG LABEL: bar\n",
}, },
{ {
desc => 'single line, no prefix, file', desc => 'single line, no label, file',
args => ['foo'], args => ['foo'],
file => 'name', file => 'name',
want => "file name: > foo\n", want => "file name: > foo\n",
}, },
{ {
desc => 'single line, no prefix, file, and line number', desc => 'single line, no label, file, and line number',
args => ['foo'], args => ['foo'],
file => 'name', file => 'name',
lineno => 42, lineno => 42,
want => "file name, line 42: > foo\n", want => "file name, line 42: > foo\n",
}, },
{ {
desc => 'single line, prefix, file, and line number', desc => 'single line, label, file, and line number',
args => [pfx => 'PFX:', 'foo'], args => [label => 'LBL:', 'foo'],
file => 'name', file => 'name',
lineno => 42, lineno => 42,
want => "PFX: file name, line 42: > foo\n", want => "LBL: file name, line 42: > foo\n",
}, },
{ {
desc => 'multiple lines, prefix, file, and line number', desc => 'multiple lines, label, file, and line number',
args => [pfx => 'PFX:', "foo\nbar"], args => [label => 'LBL:', "foo\nbar"],
file => 'name', file => 'name',
lineno => 42, lineno => 42,
want => "PFX: file name, line 42: > foo\nPFX: file name, line 42: bar\n", want => "LBL: file name, line 42: > foo\nLBL: file name, line 42: bar\n",
}, },
); );