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 UrlEncode;
:global WaitForFile;
:global LogAndError;
:global LogAndPut;
:global LogPrintExit;
:local FormatExpire do={
:global CharacterReplace;
@ -23,7 +22,7 @@
}
: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={
@ -31,7 +30,7 @@
:do {
:if ([ :len $CertRenewUrl ] = 0) do={
$LogAndError warning "No CertRenewUrl given.";
$LogPrintExit warning "No CertRenewUrl given." true;
}
:foreach Type in={ ".pem"; ".p12" } do={
@ -86,7 +85,7 @@
"Issuer: " . ([ $ParseKeyValueStore ($CertNewVal->"issuer") ]->"CN") . "\n" . \
"Validity: " . ($CertNewVal->"invalid-before") . " to " . ($CertNewVal->"invalid-after") . "\n" . \
"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={
:log debug ("Could not renew certificate " . ($CertVal->"name") . ".");
}
@ -110,6 +109,6 @@
"Issuer: " . ($CertVal->"ca") . ([ $ParseKeyValueStore ($CertVal->"issuer") ]->"CN") . "\n" . \
"Validity: " . ($CertVal->"invalid-before") . " to " . ($CertVal->"invalid-after") . "\n" . \
"Expires in: " . $ExpiresAfter);
$LogAndPut warning ("The certificate " . ($CertVal->"name") . " " . $State . \
", it is invalid after " . ($CertVal->"invalid-after") . ".");
$LogPrintExit warning ("The certificate " . ($CertVal->"name") . " " . $State . \
", it is invalid after " . ($CertVal->"invalid-after") . ".") false;
}

View file

@ -11,7 +11,7 @@
:global DeviceInfo;
:global ScriptFromTerminal;
:global SendNotification;
:global LogAndError;
:global LogPrintExit;
:local DoUpdate 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 ([ / interface wireless cap get enabled ] = true && \
[ / 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 ];
: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={

View file

@ -14,11 +14,11 @@
:global CharacterReplace;
:global DeviceInfo;
:global LogAndError;
:global LogPrintExit;
:if ($BackupSendBinary != true && \
$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

View file

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

View file

@ -7,10 +7,10 @@
:local PdPrefix $"pd-prefix";
:global ParseKeyValueStore;
:global LogAndError;
:global LogPrintExit;
: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 ];

View file

@ -4,13 +4,13 @@
#
# run scripts on DHCP lease
:global LogAndError;
:global LogPrintExit;
:if ([ :typeof $leaseActIP ] = "nothing" || \
[ :typeof $leaseActMAC ] = "nothing" || \
[ :typeof $leaseServerName ] = "nothing" || \
[ :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;

View file

@ -7,24 +7,24 @@
:global DownloadPackage;
:global ScriptFromTerminal;
:global ScriptLock;
:global LogAndError;
:global LogPrintExit;
$ScriptLock "packages-update";
:local Update [ / system package update get ];
: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={
$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={
:local PkgName [ / system package get $Package name ];
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 \
on-event=(":global RandomDelay; \$RandomDelay 3600; " . \
"/ 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
:global LogAndError;
:global LogPrintExit;
:local Interface $interface;
: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 ];

View file

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

View file

@ -6,12 +6,12 @@
:global SmsAction;
:global LogAndError;
:global LogPrintExit;
:local Action $action;
: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);

View file

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

View file

@ -5,10 +5,10 @@
:global CertificateAvailable;
:global ParseKeyValueStore;
:global LogAndError;
:global LogPrintExit;
: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

View file

@ -16,11 +16,11 @@
:global CharacterReplace;
:global DeviceInfo;
:global SendNotification;
:global LogAndError;
:global LogPrintExit;
:if ($BackupSendBinary != true && \
$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