Output a | character in log message continuation lines

This makes it easier to tell where multi-line log messages begin and
end.
This commit is contained in:
Richard Hansen 2024-05-22 19:28:03 -04:00
parent ff39ce3874
commit 9c6e5fdda4
3 changed files with 14 additions and 10 deletions

View file

@ -38,6 +38,9 @@ repository history](https://github.com/ddclient/ddclient/commits/master).
[#639](https://github.com/ddclient/ddclient/pull/639) [#639](https://github.com/ddclient/ddclient/pull/639)
* Updated sample systemd service unit file to improve logging in the systemd * Updated sample systemd service unit file to improve logging in the systemd
journal. [#669](https://github.com/ddclient/ddclient/pull/669) journal. [#669](https://github.com/ddclient/ddclient/pull/669)
* The second and subsequent lines in a multi-line log message are now prefixed
with a `|` character.
[#676](https://github.com/ddclient/ddclient/pull/676)
### Bug fixes ### Bug fixes

View file

@ -2398,14 +2398,15 @@ sub logmsg {
chomp($buffer); chomp($buffer);
my $prefix = $args{pfx}; my $prefix = $args{pfx};
$prefix = sprintf "%-9s ", $prefix if $prefix; $prefix = sprintf "%-9s ", $prefix if $prefix;
if ($file) { if ($file) {
$prefix .= "file $file"; $prefix .= "file $file";
$prefix .= ", line $lineno" if $lineno; $prefix .= ", line $lineno" if $lineno;
$prefix .= ": "; $prefix .= ": ";
} }
if ($prefix) { if ($prefix) {
$buffer = "$prefix$buffer"; $buffer = "$prefix$buffer";
$prefix =~ s/ $/| /;
$buffer =~ s/\n/\n$prefix/g; $buffer =~ s/\n/\n$prefix/g;
} }
$buffer .= "\n"; $buffer .= "\n";

View file

@ -55,49 +55,49 @@ my @test_cases = (
{ {
desc => 'single-line prefix', desc => 'single-line prefix',
args => [pfx => 'PFX:', 'foo'], args => [pfx => 'PFX:', 'foo'],
want => "PFX: foo\n", want => "PFX: foo\n",
}, },
{ {
desc => 'multi-line prefix', desc => 'multi-line prefix',
args => [pfx => 'PFX:', "foo\nbar"], args => [pfx => 'PFX:', "foo\nbar"],
want => "PFX: foo\nPFX: bar\n", want => "PFX: foo\nPFX: | bar\n",
}, },
{ {
desc => 'single-line long prefix', desc => 'single-line long prefix',
args => [pfx => 'VERY LONG PREFIX:', 'foo'], args => [pfx => 'VERY LONG PREFIX:', 'foo'],
want => "VERY LONG PREFIX: foo\n", want => "VERY LONG PREFIX: foo\n",
}, },
{ {
desc => 'multi-line long prefix', desc => 'multi-line long prefix',
args => [pfx => 'VERY LONG PREFIX:', "foo\nbar"], args => [pfx => 'VERY LONG PREFIX:', "foo\nbar"],
want => "VERY LONG PREFIX: foo\nVERY LONG PREFIX: bar\n", want => "VERY LONG PREFIX: foo\nVERY LONG PREFIX:| bar\n",
}, },
{ {
desc => 'single line, no prefix, file', desc => 'single line, no prefix, 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 prefix, 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, prefix, file, and line number',
args => [pfx => 'PFX:', 'foo'], args => [pfx => 'PFX:', 'foo'],
file => 'name', file => 'name',
lineno => 42, lineno => 42,
want => "PFX: file name, line 42: foo\n", want => "PFX: file name, line 42: foo\n",
}, },
{ {
desc => 'multiple lines, prefix, file, and line number', desc => 'multiple lines, prefix, file, and line number',
args => [pfx => 'PFX:', "foo\nbar"], args => [pfx => 'PFX:', "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 => "PFX: file name, line 42: foo\nPFX: file name, line 42:| bar\n",
}, },
); );