thepip3r
|
(Hey THIS is FUN)
|
2005-05-09 10:15 PM
|
|
|
|
|
Help trying to create a function
|
|
This code runs fine by it's self:
Code:
DIM $LogFile,$LogError,$AltLogFile
$LogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year.log" $AltLogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year-@WKSTA.log" $LogError = OPEN (5,$LogFile,5)
If $LogError=0 $ = WRITELINE(5,"Error @ERROR: @SERROR, @WKSTA, @USERID, @DATE, @TIME, @CRLF") Else $ = OPEN (6,$AltLogFile,5) $ = WRITELINE(6,"Error @ERROR: @SERROR, @WKSTA, @USERID, @DATE, @TIME, @CRLF") $ = CLOSE (6) EndIf $ = CLOSE(5)
but when I try to make it into a function so I can streamline my code, my kix scripts errors out saying that [errorlog] is an unknown command. can anyone help??
Code:
ErrorLog()
Function ErrorLog() DIM $LogFile,$LogError,$AltLogFile
$LogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year.log" $AltLogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year-@WKSTA.log" $LogError = OPEN (5,$LogFile,5)
If $LogError=0 $ = WRITELINE(5,"Error @ERROR: @SERROR, @WKSTA, @USERID, @DATE, @TIME, @CRLF") Else $ = OPEN (6,$AltLogFile,5) $ = WRITELINE(6,"Error @ERROR: @SERROR, @WKSTA, @USERID, @DATE, @TIME, @CRLF") $ = CLOSE (6) EndIf $ = CLOSE(5) EndFunction
|
Radimus
|
(KiX Supporter)
|
2005-05-09 10:32 PM
|
|
|
|
|
Re: Help trying to create a function
|
|
you can't pass @error and @serror the way you are there
Function ErrorLog($error,$serror, optional $logfile) if $logfile $log= $logfile else $log= "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year.log" endif $f=freefilehandle() $openerr=OPEN($f,$Log,5) if $openerr = 0 $ = WRITELINE($f,"Error $ERROR: $SERROR, @WKSTA, @USERID, @DATE, @TIME, @CRLF") else exit 1 endif $=close($f) Endfunction
|
thepip3r
|
(Hey THIS is FUN)
|
2005-05-09 10:39 PM
|
|
|
|
|
Re: Help trying to create a function
|
|
Tried to remod it and I'm still getting the same error:
Code:
DIM $error,$serror $error = @ERROR $serror = @SERROR ErrorLog($error,$serror)
Function ErrorLog($error,$serror) DIM $LogFile,$LogError,$AltLogFile
$LogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year.log" $AltLogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year-@WKSTA.log" $LogError = OPEN (5,$LogFile,5)
If $LogError=0 $ = WRITELINE(5,"Error $error: $serror, @WKSTA, @USERID, @DATE, @TIME, @CRLF") Else $ = OPEN (6,$AltLogFile,5) $ = WRITELINE(6,"Error $error: $serror, @WKSTA, @USERID, @DATE, @TIME, @CRLF") $ = CLOSE (6) EndIf $ = CLOSE(5) EndFunction
|
Les
|
(KiX Master)
|
2005-05-09 10:43 PM
|
|
|
|
|
Re: Help trying to create a function
|
|
What version of KiX?
|
thepip3r
|
(Hey THIS is FUN)
|
2005-05-09 10:54 PM
|
|
|
|
|
Re: Help trying to create a function
|
|
4.22
|
thepip3r
|
(Hey THIS is FUN)
|
2005-05-09 11:59 PM
|
|
|
|
|
Re: Help trying to create a function
|
|
ok, I don't know what I was doing wrong but this is what ended up working:
Code:
function ErrorLog($ErrorMsg) DIM $LogFile,$LogError,$AltLogFile,$error,$serror,$ErrorString
$error = @ERROR $serror = @SERROR $ErrorString = "Error $error, $serror, $ErrorMsg, @WKSTA, @USERID, @DATE, @TIME, @CRLF" $LogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year.log" $AltLogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year-@WKSTA.log" $LogError = OPEN (5,$LogFile,5)
If $LogError=0 $ = WRITELINE(5,$ErrorString) Else $ = OPEN (6,$AltLogFile,5) $ = WRITELINE(6,$ErrorString) $ = CLOSE (6) EndIf $ = CLOSE(5) endfunction
|
Bryce
|
(KiX Supporter)
|
2005-05-10 12:42 AM
|
|
|
|
|
Re: Help trying to create a function
|
|
Code:
ErrorLog(@ERROR,@SERROR)
Function ErrorLog($error,$serror) DIM $LogFile, $fh, $i $LogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year.log", "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year-@WKSTA.log", '.\@MDayNo_@MonthNo_@Year-@WKSTA.log'
For $i = 0 To UBound($logfile) $fh = FreeFileHandle If Open($fh,$LogFile[$i],5) = 0 $ = WriteLine($fh,"Error $error: $serror, @WKSTA, @USERID, @DATE, @TIME, @CRLF") $i = UBound($logfile) $ = Close($fh) EndIf Next EndFunction
|
thepip3r
|
(Hey THIS is FUN)
|
2005-05-10 12:56 AM
|
|
|
|
|
Re: Help trying to create a function
|
|
Is that more efficient than my code Bryce? If not, why did you post it? Just another way to accomplish my error logging task??
|
|
Re: Help trying to create a function
|
|
Bryce's code is optimized and better written.
|
Les
|
(KiX Master)
|
2005-05-10 04:26 AM
|
|
|
|
|
Re: Help trying to create a function
|
|
...but it still has vars in strings. tsk, tsk
|
Bryce
|
(KiX Supporter)
|
2005-05-10 06:28 AM
|
|
|
|
|
Re: Help trying to create a function
|
|
LOL!
|
thepip3r
|
(Hey THIS is FUN)
|
2005-05-10 05:07 PM
|
|
|
|
|
Re: Help trying to create a function
|
|
if his code is better, can someone (maybe Bryce) explain what his code is doing and why it is better? What is he doing with the $LogFile var by setting it equal to the three different files?? I can see the benefit to just putting @ERROR and @SERROR as the passed vars for the function but other than that, are the two functions serving the same purpose?
Code:
function ErrorLog($error,$serror,optional $ErrorMsg) DIM $LogFile,$LogError,$AltLogFile,$error,$serror,$ErrorString $ErrorString = "Error $error, $serror, $ErrorMsg, @WKSTA, @USERID, @DATE, @TIME, @CRLF" $LogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year.log" $AltLogFile = "\\server\SoftwarePushes\sms\logging\@MDayNo_@MonthNo_@Year-@WKSTA.log" $LogError = OPEN (5,$LogFile,5)
If $LogError=0 $ = WRITELINE(5,$ErrorString) Else $ = OPEN (6,$AltLogFile,5) $ = WRITELINE(6,$ErrorString) $ = CLOSE (6) EndIf
$ = CLOSE(5)
endfunction
|
Shawn
|
(KiX Supporter)
|
2005-05-10 06:00 PM
|
|
|
|
|
Re: Help trying to create a function
|
|
Don't sweat Jen's comments. If it works - its good. If it doesn't work - its not as good.
|
Les
|
(KiX Master)
|
2005-05-11 12:25 AM
|
|
|
|
|
Re: Help trying to create a function
|
|
Bryce's code uses FreeFileHandle to create one dynamic handle rather than arbitrating two different fixed handles. His code does not try to close a file that failed to open.
Those two IMHO, make his better. Removing vars and macros from the strings would be icing on the cake.
|
Shawn
|
(KiX Supporter)
|
2005-05-11 01:16 AM
|
|
|
|
|
Re: Help trying to create a function
|
|
But by the same token, both these UDF's aren't general purpose anyways - they both have hard-coded path names in them. So who cares if the file handle is hard-coded ? If your going to make something general purpose - go all the way man ;0)
btw - I don't know what my point is (lol)
|
thepip3r
|
(Hey THIS is FUN)
|
2005-05-11 01:33 AM
|
|
|
|
|
Re: Help trying to create a function
|
|
Code:
function ErrorLog(optional $ErrorMsg) DIM $LogFile,$LogError,$AltLogFile,$error,$serror,$ErrorString $error = @ERROR $serror = @SERROR $ErrorString = "Error "+$error+", "+$serror+", "+$ErrorMsg+", "+@WKSTA+", "+@USERID+", "+@DATE+", "+@TIME+", "+@CRLF $LogFile = "\\server\SoftwarePushes\sms\logging\"+@MDayNo+"_"+@MonthNo+"_"+@Year+".log" $AltLogFile = "\\server\SoftwarePushes\sms\logging\"+@MDayNo+"_"+@MonthNo+"_"+@Year+"-@WKSTA.log" $LogError = OPEN (5,$LogFile,5)
If $LogError=0 $ = WRITELINE(5,$ErrorString) $ = CLOSE(5) Else $ = OPEN (6,$AltLogFile,5) $ = WRITELINE(6,$ErrorString) $ = CLOSE (6) EndIf
endfunction
Is that better?
|
|
Re: Help trying to create a function
|
|
You have to pass the error strings into the UDF, otherwise the errors might not get captured correctly, thus Code:
; example use x: '\\server\nonavailable_share' $rc = errorlog(@error,@serror) ; end example function ErrorLog($error, $serror, optional $ErrorMsg) DIM $LogFile,$LogError,$AltLogFile,$ErrorString $ErrorString = "Error "+$error+", "+$serror+", "+$ErrorMsg+", "+@WKSTA+", "+@USERID+", "+@DATE+", "+@TIME+", "+@CRLF $LogFile = "\\server\SoftwarePushes\sms\logging\"+@MDayNo+"_"+@MonthNo+"_"+@Year+".log" $AltLogFile = "\\server\SoftwarePushes\sms\logging\"+@MDayNo+"_"+@MonthNo+"_"+@Year+"-@WKSTA.log" $LogError = OPEN (5,$LogFile,5)
If $LogError=0 $ = WRITELINE(5,$ErrorString) $ = CLOSE(5) Else $ = OPEN (6,$AltLogFile,5) $ = WRITELINE(6,$ErrorString) $ = CLOSE (6) EndIf
endfunction
You're also not providing a facility to not log error code 0 = success. You're also not handling a potential non-opening og file handle 6. I highly recommend to use FREEFILEHANDLE to assign file handles. The main log file might already be in use if multiple computers report errors, thus I'd rcommend to write to the alternative log file right away to prevent potential ocking issues. You're also not providing an return code for the UDF. Please read 'How to write UDFs' in the FAQ Forum.
|
thepip3r
|
(Hey THIS is FUN)
|
2005-05-11 05:55 AM
|
|
|
|
|
Re: Help trying to create a function
|
|
but in the example specifically addressing the @ERROR, @SERROR suggestion, why wouldn't this work?
Code:
use x: "\\server\unavailableShare" if @ERROR <> 0 errorLog() endif
You're saying that if I don't specifically pass @ERROR and @SERROR to the function, my example wouldn't trap the right errors? I'm using that in my code right now in four different errors, have logging implemented, and each one is working perfectly... ????
|
|
Re: Help trying to create a function
|
|
As a general rule, it is better form to explicitly pass the error value your wish to report. The reason is that if you include a "KiXtart command" your UDF, it may be the case that it will reset the value of @error which could cause your function to log an incorrect error.
The best programming methodology would be to store the value of @error & @serror in variables immediately after the the command you want to check. Then use the variable for further processing to avoid any chance of @error changing values unexpectedly.
|
|
Re: Help trying to create a function
|
|
Quote:
You're saying that if I don't specifically pass @ERROR and @SERROR to the function, my example wouldn't trap the right errors? I'm using that in my code right now in four different errors, have logging implemented, and each one is working perfectly... ????
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.
|
thepip3r
|
(Hey THIS is FUN)
|
2005-05-11 03:40 PM
|
|
|
|
|
Re: Help trying to create a function
|
|
thanx for the explanation Richard. =D
|