Sikuli logging into file

Asked by Eugene S

Hi RaiMan,

This is the third time I am asking the same question. Sorry for that :)
Here is the link to the last time I asked it:

https://answers.launchpad.net/sikuli/+question/234625

So what I am interested in is to append Sikuli log (whatever is shown on the command line screen) info to an already open file.
So just to keep track on this issue. Is that possible in the last version? Going to be?

Thanks again!
Eugene

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

If we are talking about writing log messages to a file, then what you want is already possible now:

supposing you have your own global function, that writes your log message to a file, then you can use Debug.setLogfile() to define a file, where the Sikuli log messages are written.
Just using Debug.log() ( [debug] ) will write your own messages into the same file.
Debug.log() can be configured to create message timestamps and there are Debug.action(), Debug.error() and Debug.info() that produce different message headers [log], [error] and [info].

So it is not really clear what you are missing.

What is still missing, but has low priority, is to use a standard logging module. But this would not really change the situation for you, since you would have to use Sikuli features for logging, not vice versa.

An additional option, that might make sense, is to accept a callback function from the user, that is called with the log message from SikuliX, instead of printing it or writing it to a file.
Do you think, this makes sense?

Revision history for this message
Eugene S (shragovich) said :
#2

Hi RaiMan,

Thanks for quick answer!
Did you mean that this is available in 1.0.1 or 1.1.0? I am still using 1.0.1 since I wasn't able to install 1.1.0 successfully.
So when I try t set a log file, as you suggested:

Debug.setLogfile("C:\\SikuliX\\Tests\\TRACE.log")

I get the following error:

C:\SikuliX\sikuli-script.jar -r Tests\Test.sikuli
[error] script stopped with error in line 44
[error] AttributeError ( type object 'org.sikuli.basics.Debug' has no attribute
'setLogfile' )

Am I doing something wrong?

And regarding your last question (about accept a callback function from the user), I am not sure what you mean. So if could give a bit more details that will be great!

Thanks again!
Eugene

Revision history for this message
Eugene S (shragovich) said :
#3

UPDATE

The command name was wrong and when I changed it to:

Debug.setLogFile(config.traceFile)

that started working fine.

So what I meant, are not just messages that I create, but things like I see on the screen when the script is running. For example:

[log] ( Meta ) TYPE "r"
[log] TYPE "#ENTER"

and the rest of such output (like highlights info as well)

Hope that makes it clear.

Thanks,
Eugene

Revision history for this message
RaiMan (raimund-hocke) said :
#4

This is the feature:
After having said:
Debug.setLogFile(config.traceFile)

all messages produced by SikuliX (e.g. highlight) are going to this file.

… but to get your own messages to that file, you have to use
Debug.log()
and the other possible functions.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

… so this file cannot be opened/written/closed by yourself additionally.

Revision history for this message
Eugene S (shragovich) said :
#6

Ok, now I get it.

So there is absolutely no way to use Debug.log together with my own logging?
I am trying to thing of some workaround to maybe merge these 2 files somehow but it seems like pain to do that. Maybe you have any ideas?

Thanks!

Revision history for this message
Best RaiMan (raimund-hocke) said :
#7

@ comment #2

-- callback option

something like this:

# your log function
def log(msg):
    global logfile
    try:
        logfile
    except:
        # print "setting logfile"
        logfile = open("SikuliLog.txt", "w")
    print >> logfile, msg

# the function, SikuliX should call instead of printing or writing to log file
def sikuliLog(msg):
    # one might evaluate and adapt msg here
    # or even do some filtering
    log(msg)

setSikuliXLogCallback(sikuliLog) # tell SikuliX to use the callback

log("starting")
find(something).highlight(2)
log("finishing")

this would produce a log file content:
starting
[log] highlight message …
finishing

sikuliLog is the same as the functions used with onAppear, … (there it is named handler in the docs, but it is a callback function)

hope it is clear now ;-)

Revision history for this message
RaiMan (raimund-hocke) said :
#8

@ comment #6

If you show me your code, how you do your logging (might send it to me privately), then I am sure I can tell you what to do.

Revision history for this message
Eugene S (shragovich) said :
#9

Thanks for your answer RaiMan!
I was not familiar with that term (callback function).

I will play with that idea and see how I can implement it.

Revision history for this message
Eugene S (shragovich) said :
#10

Thanks RaiMan, that solved my question.