From 3a6a2ac0363fe8b8e4bd4d9478ecdab20ea5dd6c Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 28 May 2020 14:33:19 -0400 Subject: [PATCH 1/3] Log errors to STDERR --- ddclient | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ddclient b/ddclient index 1bdf5e7..902bb3a 100755 --- a/ddclient +++ b/ddclient @@ -1757,6 +1757,7 @@ sub ynu { ## fatal ###################################################################### sub _msg { + my $fh = shift; my $log = shift; my $prefix = shift; my $format = shift; @@ -1774,21 +1775,21 @@ sub _msg { $buffer =~ s/\n/\n$prefix /g; } $buffer .= "\n"; - print $buffer; + print $fh $buffer; $msgs .= $buffer if $log; logger($buffer) if $log; } -sub msg { _msg(0, '', @_); } -sub verbose { _msg(1, @_) if opt('verbose'); } -sub info { _msg(1, 'INFO:', @_) if opt('verbose'); } -sub debug { _msg(0, 'DEBUG:', @_) if opt('debug'); } -sub debug2 { _msg(0, 'DEBUG:', @_) if opt('debug') && opt('verbose'); } -sub warning { _msg(1, 'WARNING:', @_); } -sub fatal { _msg(1, 'FATAL:', @_); sendmail(); exit(1); } -sub success { _msg(1, 'SUCCESS:', @_); } -sub failed { _msg(1, 'FAILED:', @_); $result = 'FAILED'; } +sub msg { _msg(*STDOUT, 0, '', @_); } +sub verbose { _msg(*STDOUT, 1, @_) if opt('verbose'); } +sub info { _msg(*STDOUT, 1, 'INFO:', @_) if opt('verbose'); } +sub debug { _msg(*STDOUT, 0, 'DEBUG:', @_) if opt('debug'); } +sub debug2 { _msg(*STDOUT, 0, 'DEBUG:', @_) if opt('debug') && opt('verbose'); } +sub warning { _msg(*STDERR, 1, 'WARNING:', @_); } +sub fatal { _msg(*STDERR, 1, 'FATAL:', @_); sendmail(); exit(1); } +sub success { _msg(*STDOUT, 1, 'SUCCESS:', @_); } +sub failed { _msg(*STDERR, 1, 'FAILED:', @_); $result = 'FAILED'; } sub prettytime { return scalar(localtime(shift)); } sub prettyinterval { From ca7f0927fc35d435133b6eb9e994e066aa659de6 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 28 May 2020 14:00:36 -0400 Subject: [PATCH 2/3] Fix usage() * The first argument to usage() was supposed to be the exit status, but that is not how usage() is invoked (except in the `-help` case). Change usage() to work with the existing invocations rather than fix the invocations. * Don't attempt to send an email when passed `-help`. * Include the usage error message in the emailed log. --- ddclient | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/ddclient b/ddclient index 902bb3a..f308570 100755 --- a/ddclient +++ b/ddclient @@ -807,11 +807,7 @@ $result = 'OK'; test_geturl(opt('geturl')) if opt('geturl'); -## process help option -if (opt('help')) { - *STDERR = *STDOUT; - usage(0); -} +usage() if opt('help'); ## read config file because 'daemon' mode may be defined there. read_config(define($opt{'file'}, default('file')), \%config, \%globals); @@ -1411,18 +1407,10 @@ sub init_config { ## usage ###################################################################### sub usage { - my $exitcode = 1; - $exitcode = shift if @_ != 0; # use first arg if given - my $msg = ''; - if (@_) { - my $format = shift; - $msg .= sprintf $format, @_; - 1 while chomp($msg); - $msg .= "\n"; - } - printf STDERR "%s%s\n", $msg, $opt_usage; - sendmail(); - exit $exitcode; + _msg(1, 1, 'FATAL:', @_) if @_; + printf { @_ ? *STDERR : *STDOUT } "%s\n", $opt_usage; + sendmail() if @_; + exit (@_ ? 1 : 0); } ###################################################################### From 7be4d58b90e5b47120d75fe534a6ec655ad9a11a Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 28 May 2020 13:37:38 -0400 Subject: [PATCH 3/3] Convert invocations of usage() to fatal() The usage text is extremely long which causes the error message to scroll off the top of the screen. Switch to fatal() to avoid this problem. --- ddclient | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/ddclient b/ddclient index f308570..c4de9ac 100755 --- a/ddclient +++ b/ddclient @@ -807,7 +807,10 @@ $result = 'OK'; test_geturl(opt('geturl')) if opt('geturl'); -usage() if opt('help'); +if (opt('help')) { + printf "%s\n", $opt_usage; + exit 0; +} ## read config file because 'daemon' mode may be defined there. read_config(define($opt{'file'}, default('file')), \%config, \%globals); @@ -860,7 +863,7 @@ do { read_cache(opt('cache'), \%cache); print_info() if opt('debug') && opt('verbose'); - usage("invalid argument '-use %s'; possible values are:\n%s", $opt{'use'}, join("\n", ip_strategies_usage())) + fatal("invalid argument '-use %s'; possible values are:\n%s", $opt{'use'}, join("\n", ip_strategies_usage())) unless exists $ip_strategies{lc opt('use')}; $daemon = $opt{'daemon'}; @@ -1327,7 +1330,7 @@ sub init_config { ## sanity check if (defined $opt{'host'} && defined $opt{'retry'}) { - usage("options -retry and -host (or -option host=..) are mutually exclusive"); + fatal("options -retry and -host (or -option host=..) are mutually exclusive"); } ## determine hosts to update (those on the cmd-line, config-file, or failed cached) @@ -1403,16 +1406,6 @@ sub init_config { } } -###################################################################### -## usage -###################################################################### -sub usage { - _msg(1, 1, 'FATAL:', @_) if @_; - printf { @_ ? *STDERR : *STDOUT } "%s\n", $opt_usage; - sendmail() if @_; - exit (@_ ? 1 : 0); -} - ###################################################################### ## process_args - ######################################################################