Your code will work perfectly fine as it is. However, I'd recommend passing the values for a couple of reasons.
It is good practice (programatically speaking) for functions to have clear input and output parameters. The error code for your function is clearly an input, so you should have it declared as such.
More usefully however is that you may decide to pass your own error codes and macros to the function, errors that are not actually raised by KiXtart.
This example allows you to use any combination if you wish:
Code:
Function ErrorLog(Optional $sMsg,Optional $iERROR, Optional $sERROR)
Dim $asLogFiles,$iIndex,$fh,$
; If error is not set, use @ERROR macro.
If Not VarType($iERROR) $iERROR=@ERROR EndIf
; If error message is not set, use @SERROR macro.
If Not VarType($sERROR)
$iERROR=Execute("Exit "+$iERROR)
$sERROR=@SERROR
EndIf
; Array of log files to try in order
$asLogFiles= "\\server\SoftwarePushes\sms\logging\"+@YEAR+"_"+@MONTHNO+"_"+@MDAYNO+".log",
"\\server\SoftwarePushes\sms\logging\"+@YEAR+"_"+@MONTHNO+"_"+@MDAYNO+"-"+@WKSTA+".log"
; Safe assign of file handle
$fh=FreeFileHandle()
If Not $fh Exit 1 EndIf
For $iIndex=0 To UBound($asLogFiles)
If Open($fh,$asLogFiles[$iIndex],1+4)
; Error opening this file.
Else
If WriteLine(
$fh,
@DATE
+" "+@TIME
+" "+@WKSTA
+" "+@USERID
+" ["+$iERROR+"] "
+$sERROR
+" "+$sMsg
+@CRLF)
; Urk - write failed
Else
; Successful write - force loop exit
$iIndex=UBound($asLogFiles)+1
EndIf
$=Close($fh)
EndIf
Next
; return sucess / fail status
Exit @ERROR
EndFunction
Examples:
- ErrorLog() - logs @ERROR and @SERROR
- ErrorLog("MyMessage") - as above but includes MyMessage
- ErrorLog("MyMessage",2) - Logs error 2, setting @SERROR to the system defined text for error 2.
- ErrorLog("MyMessage",2,"Bespoke message") - Logs error 2, but uses "Bespoke message" instead of system default.