global-functions: merge $LogAnd{Error,Put} to $LogPrintExit ...

... and fix logging.

Logging with severity from variable (:log $severity ...) is not
possible, this is considered a syntax error. Also the 'workaround' with
parsing code failed with missing message in log.

The reliable code is a lot longer, so merge the two functions to save a
lot of duplicate code.
This commit is contained in:
Christian Hesse 2020-02-26 14:19:54 +01:00
parent 6036edb506
commit ceaa83b83e
13 changed files with 56 additions and 56 deletions

View file

@ -14,8 +14,7 @@
:global TimeIsSync; :global TimeIsSync;
:global UrlEncode; :global UrlEncode;
:global WaitForFile; :global WaitForFile;
:global LogAndError; :global LogPrintExit;
:global LogAndPut;
:local FormatExpire do={ :local FormatExpire do={
:global CharacterReplace; :global CharacterReplace;
@ -23,7 +22,7 @@
} }
:if ($TimeIsSync = false) do={ :if ($TimeIsSync = false) do={
$LogAndError warning "Time is not yet synchronized."; $LogPrintExit warning "Time is not yet synchronized." true;
} }
:foreach Cert in=[ / certificate find where !revoked !ca expires-after<3w ] do={ :foreach Cert in=[ / certificate find where !revoked !ca expires-after<3w ] do={
@ -31,7 +30,7 @@
:do { :do {
:if ([ :len $CertRenewUrl ] = 0) do={ :if ([ :len $CertRenewUrl ] = 0) do={
$LogAndError warning "No CertRenewUrl given."; $LogPrintExit warning "No CertRenewUrl given." true;
} }
:foreach Type in={ ".pem"; ".p12" } do={ :foreach Type in={ ".pem"; ".p12" } do={
@ -86,7 +85,7 @@
"Issuer: " . ([ $ParseKeyValueStore ($CertNewVal->"issuer") ]->"CN") . "\n" . \ "Issuer: " . ([ $ParseKeyValueStore ($CertNewVal->"issuer") ]->"CN") . "\n" . \
"Validity: " . ($CertNewVal->"invalid-before") . " to " . ($CertNewVal->"invalid-after") . "\n" . \ "Validity: " . ($CertNewVal->"invalid-before") . " to " . ($CertNewVal->"invalid-after") . "\n" . \
"Expires in: " . [ $FormatExpire ($CertNewVal->"expires-after") ]) "" "true"; "Expires in: " . [ $FormatExpire ($CertNewVal->"expires-after") ]) "" "true";
$LogAndPut info ("The certificate " . ($CertVal->"name") . " has been renewed."); $LogPrintExit info ("The certificate " . ($CertVal->"name") . " has been renewed.") false;
} on-error={ } on-error={
:log debug ("Could not renew certificate " . ($CertVal->"name") . "."); :log debug ("Could not renew certificate " . ($CertVal->"name") . ".");
} }
@ -110,6 +109,6 @@
"Issuer: " . ($CertVal->"ca") . ([ $ParseKeyValueStore ($CertVal->"issuer") ]->"CN") . "\n" . \ "Issuer: " . ($CertVal->"ca") . ([ $ParseKeyValueStore ($CertVal->"issuer") ]->"CN") . "\n" . \
"Validity: " . ($CertVal->"invalid-before") . " to " . ($CertVal->"invalid-after") . "\n" . \ "Validity: " . ($CertVal->"invalid-before") . " to " . ($CertVal->"invalid-after") . "\n" . \
"Expires in: " . $ExpiresAfter); "Expires in: " . $ExpiresAfter);
$LogAndPut warning ("The certificate " . ($CertVal->"name") . " " . $State . \ $LogPrintExit warning ("The certificate " . ($CertVal->"name") . " " . $State . \
", it is invalid after " . ($CertVal->"invalid-after") . "."); ", it is invalid after " . ($CertVal->"invalid-after") . ".") false;
} }

View file

@ -11,7 +11,7 @@
:global DeviceInfo; :global DeviceInfo;
:global ScriptFromTerminal; :global ScriptFromTerminal;
:global SendNotification; :global SendNotification;
:global LogAndError; :global LogPrintExit;
:local DoUpdate do={ :local DoUpdate do={
:if ([ / system script print count-only where name="packages-update" ] > 0) do={ :if ([ / system script print count-only where name="packages-update" ] > 0) do={
@ -25,7 +25,7 @@
:if ([ / system package print count-only where name="wireless" disabled=no ] > 0) do={ :if ([ / system package print count-only where name="wireless" disabled=no ] > 0) do={
:if ([ / interface wireless cap get enabled ] = true && \ :if ([ / interface wireless cap get enabled ] = true && \
[ / caps-man manager get enabled ] = false) do={ [ / caps-man manager get enabled ] = false) do={
$LogAndError error "System is managed by CAPsMAN, not checking."; $LogPrintExit error "System is managed by CAPsMAN, not checking." true;
} }
} }
@ -37,7 +37,7 @@
:local Update [ / system package update get ]; :local Update [ / system package update get ];
:if ([ :len ($Update->"latest-version") ] = 0) do={ :if ([ :len ($Update->"latest-version") ] = 0) do={
$LogAndError warning "An empty string is not a valid version."; $LogPrintExit warning "An empty string is not a valid version." true;
} }
:if ($Update->"installed-version" != $Update->"latest-version") do={ :if ($Update->"installed-version" != $Update->"latest-version") do={

View file

@ -14,11 +14,11 @@
:global CharacterReplace; :global CharacterReplace;
:global DeviceInfo; :global DeviceInfo;
:global LogAndError; :global LogPrintExit;
:if ($BackupSendBinary != true && \ :if ($BackupSendBinary != true && \
$BackupSendExport != true) do={ $BackupSendExport != true) do={
$LogAndError error ("Configured to send neither backup nor config export."); $LogPrintExit error ("Configured to send neither backup nor config export.") true;
} }
# filename based on identity # filename based on identity

View file

@ -37,8 +37,7 @@
:global MailServerIsUp; :global MailServerIsUp;
:global TimeIsSync; :global TimeIsSync;
:global WaitTimeSync; :global WaitTimeSync;
:global LogAndError; :global LogPrintExit;
:global LogAndPut;
# url encoding # url encoding
:set UrlEncode do={ :set UrlEncode do={
@ -491,20 +490,22 @@
} }
} }
# log and error with same text # log and print with same text, optionally exit
:set LogAndError do={ :set LogPrintExit do={
:local Severity [ :tostr $1 ]; :local Severity [ :tostr $1 ];
:local Message [ :tostr $2 ]; :local Message [ :tostr $2 ];
:local Exit [ :tostr $3 ];
[ :parse (":log " . $Severity . " \$Message") ]; :if ($Severity ~ "^(error|info)\$") do={
:error ($Severity . ": " . $Message); :if ($Severity = "error" ) do={ :log error $Message; }
} :if ($Severity = "info" ) do={ :log info $Message; }
} else={
# log and put (print on terminal) same text :log warning $Message;
:set LogAndPut do={ }
:local Severity [ :tostr $1 ];
:local Message [ :tostr $2 ]; :if ($Exit = "true") do={
:error ($Severity . ": " . $Message);
[ :parse (":log " . $Severity . " \$Message") ]; } else={
:put ($Severity . ": " . $Message); :put ($Severity . ": " . $Message);
}
} }

View file

@ -7,10 +7,10 @@
:local PdPrefix $"pd-prefix"; :local PdPrefix $"pd-prefix";
:global ParseKeyValueStore; :global ParseKeyValueStore;
:global LogAndError; :global LogPrintExit;
:if ([ :typeof $PdPrefix ] = "nothing") do={ :if ([ :typeof $PdPrefix ] = "nothing") do={
$LogAndError error "This script is supposed to run from ipv6 dhcp-client."; $LogPrintExit error "This script is supposed to run from ipv6 dhcp-client." true;
} }
:local Pool [ / ipv6 pool get [ find where prefix=$PdPrefix ] name ]; :local Pool [ / ipv6 pool get [ find where prefix=$PdPrefix ] name ];

View file

@ -4,13 +4,13 @@
# #
# run scripts on DHCP lease # run scripts on DHCP lease
:global LogAndError; :global LogPrintExit;
:if ([ :typeof $leaseActIP ] = "nothing" || \ :if ([ :typeof $leaseActIP ] = "nothing" || \
[ :typeof $leaseActMAC ] = "nothing" || \ [ :typeof $leaseActMAC ] = "nothing" || \
[ :typeof $leaseServerName ] = "nothing" || \ [ :typeof $leaseServerName ] = "nothing" || \
[ :typeof $leaseBound ] = "nothing") do={ [ :typeof $leaseBound ] = "nothing") do={
$LogAndError error "This script is supposed to run from ip dhcp-client."; $LogPrintExit error "This script is supposed to run from ip dhcp-client." true;
} }
:local Scripts; :local Scripts;

View file

@ -7,24 +7,24 @@
:global DownloadPackage; :global DownloadPackage;
:global ScriptFromTerminal; :global ScriptFromTerminal;
:global ScriptLock; :global ScriptLock;
:global LogAndError; :global LogPrintExit;
$ScriptLock "packages-update"; $ScriptLock "packages-update";
:local Update [ / system package update get ]; :local Update [ / system package update get ];
:if ([ :typeof ($Update->"latest-version") ] = "nothing") do={ :if ([ :typeof ($Update->"latest-version") ] = "nothing") do={
$LogAndError warning "Latest version is not known."; $LogPrintExit warning "Latest version is not known." true;
} }
:if ($Update->"installed-version" = $Update->"latest-version") do={ :if ($Update->"installed-version" = $Update->"latest-version") do={
$LogAndError info ("Version " . $Update->"latest-version" . " is already installed."); $LogPrintExit info ("Version " . $Update->"latest-version" . " is already installed.") true;
} }
:foreach Package in=[ / system package find where !bundle ] do={ :foreach Package in=[ / system package find where !bundle ] do={
:local PkgName [ / system package get $Package name ]; :local PkgName [ / system package get $Package name ];
if ([ $DownloadPackage $PkgName ($Update->"latest-version") ] = false) do={ if ([ $DownloadPackage $PkgName ($Update->"latest-version") ] = false) do={
$LogAndError error ("Download for package " . $PkgName . " failed."); $LogPrintExit error ("Download for package " . $PkgName . " failed.") true;
} }
} }
@ -47,7 +47,7 @@ $ScriptLock "packages-update";
/ system scheduler add name="reboot-for-update" start-time=03:00:00 interval=1d \ / system scheduler add name="reboot-for-update" start-time=03:00:00 interval=1d \
on-event=(":global RandomDelay; \$RandomDelay 3600; " . \ on-event=(":global RandomDelay; \$RandomDelay 3600; " . \
"/ system scheduler remove reboot-for-update; / system reboot;"); "/ system scheduler remove reboot-for-update; / system reboot;");
$LogAndError info ("Scheduled reboot for update between 03:00 and 04:00."); $LogPrintExit info ("Scheduled reboot for update between 03:00 and 04:00.") true;
} }
} }

View file

@ -4,12 +4,12 @@
# #
# run scripts on ppp up # run scripts on ppp up
:global LogAndError; :global LogPrintExit;
:local Interface $interface; :local Interface $interface;
:if ([ :typeof $Interface ] = "nothing") do={ :if ([ :typeof $Interface ] = "nothing") do={
$LogAndError error "This script is supposed to run from ppp on-up script hook."; $LogPrintExit error "This script is supposed to run from ppp on-up script hook." true;
} }
:local IntName [ / interface get $Interface name ]; :local IntName [ / interface get $Interface name ];

View file

@ -15,7 +15,7 @@
:global ScriptUpdatesIgnore; :global ScriptUpdatesIgnore;
:global SendNotification; :global SendNotification;
:global LogAndPut; :global LogPrintExit;
:foreach Script in=[ / system script find where source~"^#!rsc" ] do={ :foreach Script in=[ / system script find where source~"^#!rsc" ] do={
:local Ignore 0; :local Ignore 0;
@ -30,15 +30,15 @@
:foreach Scheduler in=[ / system scheduler find where on-event~("\\b" . $ScriptVal->"name" . "\\b") ] do={ :foreach Scheduler in=[ / system scheduler find where on-event~("\\b" . $ScriptVal->"name" . "\\b") ] do={
:local SchedulerVal [ / system scheduler get $Scheduler ]; :local SchedulerVal [ / system scheduler get $Scheduler ];
:if ($ScriptVal->"policy" != $SchedulerVal->"policy") do={ :if ($ScriptVal->"policy" != $SchedulerVal->"policy") do={
$LogAndPut warning ("Policies differ for script " . $ScriptVal->"name" . \ $LogPrintExit warning ("Policies differ for script " . $ScriptVal->"name" . \
" and its scheduler " . $SchedulerVal->"name" . "!"); " and its scheduler " . $SchedulerVal->"name" . "!") false;
} }
:if ($SchedulerVal->"name" != "global-scripts" && \ :if ($SchedulerVal->"name" != "global-scripts" && \
$SchedulerVal->"start-time" = "startup" && \ $SchedulerVal->"start-time" = "startup" && \
$SchedulerVal->"interval" = 0s && \ $SchedulerVal->"interval" = 0s && \
[ :pick ($SchedulerVal->"on-event") 0 7 ] != ":delay ") do={ [ :pick ($SchedulerVal->"on-event") 0 7 ] != ":delay ") do={
$LogAndPut warning ("Scheduler " . $SchedulerVal->"name" . " starts on startup, " . \ $LogPrintExit warning ("Scheduler " . $SchedulerVal->"name" . " starts on startup, " . \
"without interval. Add delay to make sure the configuration is available!"); "without interval. Add delay to make sure the configuration is available!") false;
} }
} }
@ -57,7 +57,7 @@
:set SourceNew ($Result->"data"); :set SourceNew ($Result->"data");
} }
} on-error={ } on-error={
$LogAndPut warning ("Failed fetching " . $ScriptVal->"name"); $LogPrintExit warning ("Failed fetching " . $ScriptVal->"name") false;
} }
} }
} }
@ -67,7 +67,7 @@
:if ($SourceNew != $ScriptVal->"source") do={ :if ($SourceNew != $ScriptVal->"source") do={
:local DontRequirePermissions \ :local DontRequirePermissions \
($SourceNew~"\n# requires: dont-require-permissions=yes\n"); ($SourceNew~"\n# requires: dont-require-permissions=yes\n");
$LogAndPut info ("Updating script: " . $ScriptVal->"name"); $LogPrintExit info ("Updating script: " . $ScriptVal->"name") false;
/ system script set owner=($ScriptVal->"name") source=$SourceNew \ / system script set owner=($ScriptVal->"name") source=$SourceNew \
dont-require-permissions=$DontRequirePermissions $Script; dont-require-permissions=$DontRequirePermissions $Script;
:if ($ScriptVal->"name" = "global-config" && \ :if ($ScriptVal->"name" = "global-config" && \
@ -81,7 +81,7 @@
:log debug ("Script " . $ScriptVal->"name" . " did not change."); :log debug ("Script " . $ScriptVal->"name" . " did not change.");
} }
} else={ } else={
$LogAndPut warning ("Looks like new script " . $ScriptVal->"name" . " is not valid. Ignoring!"); $LogPrintExit warning ("Looks like new script " . $ScriptVal->"name" . " is not valid. Ignoring!") false;
} }
} else={ } else={
:log debug ("No update for script " . $ScriptVal->"name" . "."); :log debug ("No update for script " . $ScriptVal->"name" . ".");
@ -117,7 +117,7 @@
} }
:set GlobalConfigChanges; :set GlobalConfigChanges;
} on-error={ } on-error={
$LogAndPut warning ("Failed fetching changes!"); $LogPrintExit warning ("Failed fetching changes!") false;
:set NotificationMessage ($NotificationMessage . \ :set NotificationMessage ($NotificationMessage . \
"\n\nChanges are not available."); "\n\nChanges are not available.");
} }

View file

@ -6,12 +6,12 @@
:global SmsAction; :global SmsAction;
:global LogAndError; :global LogPrintExit;
:local Action $action; :local Action $action;
:if ([ :typeof $Action ] = "nothing") do={ :if ([ :typeof $Action ] = "nothing") do={
$LogAndError error "This script is supposed to run from SMS hook with action=..."; $LogPrintExit error "This script is supposed to run from SMS hook with action=..." true;
} }
:local Code ($SmsAction->$Action); :local Code ($SmsAction->$Action);

View file

@ -8,11 +8,11 @@
:global SendNotification; :global SendNotification;
:global MailServerIsUp; :global MailServerIsUp;
:global LogAndError; :global LogPrintExit;
# check mail server # check mail server
:if ($MailServerIsUp = false) do={ :if ($MailServerIsUp = false) do={
$LogAndError warning "Mail server is not up."; $LogPrintExit warning "Mail server is not up." true;
} }
:local Settings [ / tool sms get ]; :local Settings [ / tool sms get ];

View file

@ -5,10 +5,10 @@
:global CertificateAvailable; :global CertificateAvailable;
:global ParseKeyValueStore; :global ParseKeyValueStore;
:global LogAndError; :global LogPrintExit;
:if ([ / ip cloud get ddns-enabled ] != true) do={ :if ([ / ip cloud get ddns-enabled ] != true) do={
$LogAndError error "IP cloud DDNS is not enabled."; $LogPrintExit error "IP cloud DDNS is not enabled." true;
} }
# Get the current ip address from cloud # Get the current ip address from cloud

View file

@ -16,11 +16,11 @@
:global CharacterReplace; :global CharacterReplace;
:global DeviceInfo; :global DeviceInfo;
:global SendNotification; :global SendNotification;
:global LogAndError; :global LogPrintExit;
:if ($BackupSendBinary != true && \ :if ($BackupSendBinary != true && \
$BackupSendExport != true) do={ $BackupSendExport != true) do={
$LogAndError error ("Configured to send neither backup nor config export."); $LogPrintExit error ("Configured to send neither backup nor config export.") true;
} }
# filename based on identity # filename based on identity