Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error Management
#1
When a function is using a logical test to throw an error, how do you access the _error variable properly so the LogErrors function will write that error to the LogFile?

Code:
Copy      Help
atend LogErrors
Ftp f.Connect(server username password)
if(!f.FilePutFromStr(data ftpfile))
,;??? HOW TO MAKE CUSTOM ERROR TO INSERT INTO ERROR FILE HERE ????

Function LogErrors
Code:
Copy      Help
if(_error.source) ;;macro ended due to an error
,str macroname.getmacro(getopt(itemid 3) 1)
,str functionname.getmacro(_error.iid 1)
,str s.format("Macro %s ended due to an error in %s.[]Error description: %s[]Error line: %s[]" macroname functionname _error.description _error.line)
,LogFile s 1 F"$desktop$\LOGS\{macroname}.txt"

Thanks,
Jim
#2
Function TestError
Code:
Copy      Help
if(!f.FilePutFromStr(data ftpfile))
,_error.description="Could not put data on FTP server"
,_error.line="if(!f.FilePutFromStr(data ftpfile))"
,end _error

How do you get the _error.line to be the line of code that generated it?
#3
end overwrites _error.line.

Append the code line to description. LogErrors can parse it, if need.
Code:
Copy      Help
if mes("Error?" "" "YN")='Y'
,end "Could not put data on FTP server // if(!f.FilePutFromStr(data ftpfile))"

Function ErrDescrAndLine
Code:
Copy      Help
;/
function$ $description [lineOffset]

;Gets previous code line and appends to description. To be used with end().

;description - error description.
;lineOffset - line offset from the line that calls this function. If 0 or omitted, uses -1 (previous line).

;REMARKS
;Uses " // " as separator.

;EXAMPLE
;if mes("Error?" "" "YN")='Y'
,;end ErrDescrAndLine("Error description.")


#if EXE
ret description
#else
if(!lineOffset) lineOffset=-1
if(Statement(1 lineOffset &_s)<0) ret description
str-- s.from(description " // " _s)
ret s
#4
If the error is generated in a function that is being called by another function/macro the str functionname.getmacro(_error.iid 1) line is the name of the calling function in my LogErrors script when an error is generated by ErrDescrAndLine function.

What would be the best mod to get the function name actually generating the error?

Function LogErrors
Code:
Copy      Help
str CSV TimeStamp.timeformat("{yyyyy}{MM}{dd}T{HH}{mm}{ss}Z")
str macroname.getmacro(getopt(itemid 3) 1)
str pattern="(.*) // (.*)"
ARRAY(str) ERROR

if(_error.source) ;;macro ended due to an error
,str functionname.getmacro(_error.iid 1)
,out functionname
,out macroname
,if(findrx(_error.description pattern 0 0 ERROR)<0)
,,ERROR[]=""
,,ERROR[]=_error.description
,,ERROR[]=_error.line
,ERROR[1].findreplace("Failed.")
,ERROR[1].trim
,ERROR[2].findreplace("[9]")
,CSV=F"{TimeStamp}[9]Failed[9]{macroname}[9]{functionname}[9]{ERROR[1]}[9]{ERROR[2]}"
else
,CSV=F"{TimeStamp}[9]Success[9]{macroname}[9][9]"

LogFile(CSV 0 "$desktop$\errorlog.csv")
#5
Also is there a way to log errors through out run-time and log them all into a CSV file at the end?
It would be nice to be able to set levels of error (ie: Fail, Warning, etc)
How would errors be appended using ErrDescrAndLine that wouldn't end the macro if it is only warning?

Thanks,
Jim
#6
The same as with error code line. Let ErrDescrAndLine append caller id (getopt(itemid 1)) to description, and let LogErrors extract it from description.
#7
Nevermind. I'll just use a new function for Warnings that will write to my log file.

Thanks for all the help. I've got it working about how I want it.
#8
If need to collect all warnings of current thread, and then write to file when the thread ends, I would use ICsv variable with thread scope.

Function LogWarning
Code:
Copy      Help
;/
function $text

str s1 s2 s3(text)
s1="time"
s2.getmacro(getopt(itemid 1) 1)

ICsv- t_logErr=CreateCsv(1)
t_logErr.AddRowSA(-1 3 &s1)

Function LogErrors
Code:
Copy      Help
ICsv- t_logErr=CreateCsv(1)

;then append error or success to t_logErr, like in LogWarning

;then append all to file
lock 0 "QM_mutex_LogErrors"
t_logErr.ToFile("$desktop$\errorlog.csv" 1)


Forum Jump:


Users browsing this thread: 1 Guest(s)