From ceaa83b83edb069ecf1cca181ec461519f0cc020 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 26 Feb 2020 14:19:54 +0100 Subject: 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. --- check-certificates | 13 ++++++------- check-routeros-update | 6 +++--- email-backup | 4 ++-- global-functions | 33 +++++++++++++++++---------------- ipv6-update | 4 ++-- lease-script | 4 ++-- packages-update | 10 +++++----- ppp-on-up | 4 ++-- script-updates | 18 +++++++++--------- sms-action | 4 ++-- sms-forward | 4 ++-- update-tunnelbroker | 4 ++-- upload-backup | 4 ++-- 13 files changed, 56 insertions(+), 56 deletions(-) diff --git a/check-certificates b/check-certificates index 56e9537..d94c330 100644 --- a/check-certificates +++ b/check-certificates @@ -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; } diff --git a/check-routeros-update b/check-routeros-update index ff5bc38..d622ba3 100644 --- a/check-routeros-update +++ b/check-routeros-update @@ -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={ diff --git a/email-backup b/email-backup index 45e7703..978ac4d 100644 --- a/email-backup +++ b/email-backup @@ -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 diff --git a/global-functions b/global-functions index 7e70d01..ed7363b 100644 --- a/global-functions +++ b/global-functions @@ -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 ]; - - [ :parse (":log " . $Severity . " \$Message") ]; - :error ($Severity . ": " . $Message); -} - -# log and put (print on terminal) same text -:set LogAndPut do={ - :local Severity [ :tostr $1 ]; - :local Message [ :tostr $2 ]; + :local Message [ :tostr $2 ]; + :local Exit [ :tostr $3 ]; + + :if ($Severity ~ "^(error|info)\$") do={ + :if ($Severity = "error" ) do={ :log error $Message; } + :if ($Severity = "info" ) do={ :log info $Message; } + } else={ + :log warning $Message; + } - [ :parse (":log " . $Severity . " \$Message") ]; - :put ($Severity . ": " . $Message); + :if ($Exit = "true") do={ + :error ($Severity . ": " . $Message); + } else={ + :put ($Severity . ": " . $Message); + } } diff --git a/ipv6-update b/ipv6-update index eee3536..7621082 100644 --- a/ipv6-update +++ b/ipv6-update @@ -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 ]; diff --git a/lease-script b/lease-script index 9109ea9..1e73b31 100644 --- a/lease-script +++ b/lease-script @@ -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; diff --git a/packages-update b/packages-update index d57ee5d..b364b18 100644 --- a/packages-update +++ b/packages-update @@ -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; } } diff --git a/ppp-on-up b/ppp-on-up index 415663c..48d3413 100644 --- a/ppp-on-up +++ b/ppp-on-up @@ -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 ]; diff --git a/script-updates b/script-updates index b589b9f..2cdc703 100644 --- a/script-updates +++ b/script-updates @@ -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."); } diff --git a/sms-action b/sms-action index ceb9071..4442baf 100644 --- a/sms-action +++ b/sms-action @@ -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); diff --git a/sms-forward b/sms-forward index f03ab27..e84cfea 100644 --- a/sms-forward +++ b/sms-forward @@ -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 ]; diff --git a/update-tunnelbroker b/update-tunnelbroker index 138fac7..bd36439 100644 --- a/update-tunnelbroker +++ b/update-tunnelbroker @@ -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 diff --git a/upload-backup b/upload-backup index 817a088..fb9f9cd 100644 --- a/upload-backup +++ b/upload-backup @@ -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 -- cgit v1.2.3-54-g00ecf