Page 1 of 2 12>
Topic Options
#139465 - 2005-05-09 10:15 PM Help trying to create a function
thepip3r Offline
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 Offline
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)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#139467 - 2005-05-09 10:39 PM Re: Help trying to create a function
thepip3r Offline
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
#139468 - 2005-05-09 10:43 PM Re: Help trying to create a function
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
What version of KiX?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#139469 - 2005-05-09 10:54 PM Re: Help trying to create a function
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
4.22
Top
#139470 - 2005-05-09 11:59 PM Re: Help trying to create a function
thepip3r Offline
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 Offline
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
#139472 - 2005-05-10 12:56 AM Re: Help trying to create a function
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
Is that more efficient than my code Bryce? If not, why did you post it? Just another way to accomplish my error logging task??
Top
#139473 - 2005-05-10 04:15 AM Re: Help trying to create a function
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Bryce's code is optimized and better written.
_________________________
There are two types of vessels, submarines and targets.

Top
#139474 - 2005-05-10 04:26 AM Re: Help trying to create a function
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
...but it still has vars in strings. tsk, tsk
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#139475 - 2005-05-10 06:28 AM Re: Help trying to create a function
Bryce Offline
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 Offline
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
#139477 - 2005-05-10 06:00 PM Re: Help trying to create a function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Don't sweat Jen's comments. If it works - its good. If it doesn't work - its not as good.
Top
#139478 - 2005-05-11 12:25 AM Re: Help trying to create a function
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#139479 - 2005-05-11 01:16 AM Re: Help trying to create a function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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)

Top
#139480 - 2005-05-11 01:33 AM Re: Help trying to create a function
thepip3r Offline
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 Offline
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 Offline
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 Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#139484 - 2005-05-11 11:29 AM Re: Help trying to create a function
Richard H. Administrator Offline
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
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 641 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.077 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org