#139465 - 2005-05-09 10:15 PM
Help trying to create a function
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
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
|
|
Top
|
|
|
|
#139466 - 2005-05-09 10:32 PM
Re: Help trying to create a function
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
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
Edited by Radimus (2005-05-09 10:33 PM)
|
|
Top
|
|
|
|
#139467 - 2005-05-09 10:39 PM
Re: Help trying to create a function
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
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
|
|
Top
|
|
|
|
#139470 - 2005-05-09 11:59 PM
Re: Help trying to create a function
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
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
|
|
Top
|
|
|
|
#139471 - 2005-05-10 12:42 AM
Re: Help trying to create a function
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
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
|
|
Top
|
|
|
|
#139475 - 2005-05-10 06:28 AM
Re: Help trying to create a function
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
LOL!
|
|
Top
|
|
|
|
#139476 - 2005-05-10 05:07 PM
Re: Help trying to create a function
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
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
Edited by thepip3r (2005-05-10 05:10 PM)
|
|
Top
|
|
|
|
#139480 - 2005-05-11 01:33 AM
Re: Help trying to create a function
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
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?
|
|
Top
|
|
|
|
#139481 - 2005-05-11 04:10 AM
Re: Help trying to create a function
|
Sealeopard
KiX Master
   
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
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.
_________________________
There are two types of vessels, submarines and targets.
|
|
Top
|
|
|
|
#139482 - 2005-05-11 05:55 AM
Re: Help trying to create a function
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
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... ????
|
|
Top
|
|
|
|
#139483 - 2005-05-11 06:42 AM
Re: Help trying to create a function
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
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.
|
|
Top
|
|
|
|
#139484 - 2005-05-11 11:29 AM
Re: Help trying to create a function
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 568 anonymous users online.
|
|
|