From 3262dd0952460a7c4b7a747f801f7fec8e02c342 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 25 Jul 2024 02:04:28 -0400 Subject: [PATCH] logging: Rename `pfx` to `label` This is to prepare for a general log prefix mechanism to improve log readability. --- ddclient.in | 17 +++++++++-------- t/logmsg.pl | 40 ++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/ddclient.in b/ddclient.in index 8332777..60858cd 100755 --- a/ddclient.in +++ b/ddclient.in @@ -2348,7 +2348,7 @@ my $_in_logmsg = 0; sub logmsg { my %args = ( msg => '', - pfx => '', + label => '', fh => *STDERR, email => 0, # If truthy, the message is also included in the next email. (@_ % 2) ? (msg => pop) : (), @@ -2357,7 +2357,7 @@ sub logmsg { my $buffer = $args{msg}; chomp($buffer); - my $prefix = $args{pfx}; + my $prefix = $args{label}; $prefix = sprintf "%-8s ", $prefix if $prefix; if ($file) { $prefix .= "file $file"; @@ -2383,12 +2383,13 @@ sub logmsg { } } sub _logmsg_fmt { return (@_ > 1) ? sprintf(shift, @_) : shift; } -sub info { logmsg(email => 1, pfx => 'INFO:', _logmsg_fmt(@_)) if opt('verbose'); } -sub debug { logmsg( pfx => 'DEBUG:', _logmsg_fmt(@_)) if opt('debug'); } -sub warning { logmsg(email => 1, pfx => 'WARNING:', _logmsg_fmt(@_)); } -sub fatal { logmsg(email => 1, pfx => 'FATAL:', _logmsg_fmt(@_)); sendmail(); exit(1); } -sub success { logmsg(email => 1, pfx => 'SUCCESS:', _logmsg_fmt(@_)); } -sub failed { logmsg(email => 1, pfx => 'FAILED:', _logmsg_fmt(@_)); $result = 'FAILED'; } +sub info { logmsg(email => 1, label => 'INFO:', _logmsg_fmt(@_)) if opt('verbose'); } +sub debug { logmsg( label => 'DEBUG:', _logmsg_fmt(@_)) if opt('debug'); } +sub warning { logmsg(email => 1, label => 'WARNING:', _logmsg_fmt(@_)); } +sub fatal { logmsg(email => 1, label => 'FATAL:', _logmsg_fmt(@_)); sendmail(); exit(1); } +sub success { logmsg(email => 1, label => 'SUCCESS:', _logmsg_fmt(@_)); } +sub failed { logmsg(email => 1, label => 'FAILED:', _logmsg_fmt(@_)); $result = 'FAILED'; } + sub prettytime { return scalar(localtime(shift)); } sub prettyinterval { diff --git a/t/logmsg.pl b/t/logmsg.pl index f3577a5..8a3d9f3 100644 --- a/t/logmsg.pl +++ b/t/logmsg.pl @@ -53,51 +53,51 @@ my @test_cases = ( want => "foo\n", }, { - desc => 'single-line prefix', - args => [pfx => 'PFX:', 'foo'], - want => "PFX: > foo\n", + desc => 'single-line label', + args => [label => 'LBL:', 'foo'], + want => "LBL: > foo\n", }, { - desc => 'multi-line prefix', - args => [pfx => 'PFX:', "foo\nbar"], - want => "PFX: > foo\nPFX: bar\n", + desc => 'multi-line label', + args => [label => 'LBL:', "foo\nbar"], + want => "LBL: > foo\nLBL: bar\n", }, { - desc => 'single-line long prefix', - args => [pfx => 'VERY LONG PREFIX:', 'foo'], - want => "VERY LONG PREFIX: > foo\n", + desc => 'single-line long label', + args => [label => 'VERY LONG LABEL:', 'foo'], + want => "VERY LONG LABEL: > foo\n", }, { - desc => 'multi-line long prefix', - args => [pfx => 'VERY LONG PREFIX:', "foo\nbar"], - want => "VERY LONG PREFIX: > foo\nVERY LONG PREFIX: bar\n", + desc => 'multi-line long label', + args => [label => 'VERY LONG LABEL:', "foo\nbar"], + want => "VERY LONG LABEL: > foo\nVERY LONG LABEL: bar\n", }, { - desc => 'single line, no prefix, file', + desc => 'single line, no label, file', args => ['foo'], file => 'name', 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'], file => 'name', lineno => 42, want => "file name, line 42: > foo\n", }, { - desc => 'single line, prefix, file, and line number', - args => [pfx => 'PFX:', 'foo'], + desc => 'single line, label, file, and line number', + args => [label => 'LBL:', 'foo'], file => 'name', 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', - args => [pfx => 'PFX:', "foo\nbar"], + desc => 'multiple lines, label, file, and line number', + args => [label => 'LBL:', "foo\nbar"], file => 'name', 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", }, );