← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #245665]: Logging the errors and failures in a seperate file with line numbers where it is failed

 

Question #245665 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/245665

    Status: Open => Answered

obiwan-92 proposed the following answer:
Hello.

Ok.

So create a sikuli script with this code :

import sys, traceback, datetime

def log(scr, str):
    file = open(scr, 'a')
    file.write(str)
    file.close()

lscript = ['script1.sikuli', 'script2.sikuli'] # The list of the sikuli script you want to launch
NB = 10 # The number of time to loop on the scripts
flog = "C:/logfiel.txt" # the log file

for cpt in NB:
    for scr in lscript:
        t1 = datetime.now()
        try:
            m = __import__(scr, globals(), locals, [], -1)
            if cpt > 0: reload(m)
            t2 = datetime.now()
            log(flog, scr + " : OK in " +str((t2-t1).seconds))
        except FindFailed, Err:
            t2 = datetime.now()
            Err = str(Err).replace('\n', '').replace('!!WHILE IMPORTING!! ','')
            for frame in traceback.extract_tb(sys.exc_info()[2]):
                ferr,lerr,cerr,terr = frame
                if scr+".py" in ferr: 
                    msg = "\n\t in "+ferr+" ("+str(lerr)+") - time: "+str((t2-t1).seconds)
            log(flog, "BaseException : "+Err+" ! "+msg)
        except BaseException, Err:
            t2 = datetime.now()
            Err = str(Err).replace('\n', '').replace('!!WHILE IMPORTING!! ','')
            for frame in traceback.extract_tb(sys.exc_info()[2]):
                ferr,lerr,cerr,terr = frame
                if scr+".py" in ferr: 
                    msg = "\n\t in "+ferr+" ("+str(lerr)+") - time: "+str((t2-t1).seconds)
            log(flog, "BaseException : "+Err+" ! "+msg)

Like I say it's a piece of work. ;)
I wrote it directly in launchpad so maybe it won't work at your first try.
I'm sorry but I don't have any computer, currently. :'(

I could you give a lot of explanation, but it will be faster and easier
if you ask it directly. :)

Regards.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.