Nathan Osman
Nathan Osman

Reputation: 73165

How to get output?

I am using the Python/C API with my app and am wondering how you can get console output with a gui app. When there is a script error, it is displayed via printf but this obviously has no effect with a gui app. I want to be able to obtain the output without creating a console. Can this be done?

Edit - Im using Windows, btw.

Edit - The Python/C library internally calls printf and does so before any script can be loaded and run. If there is an error I want to be able to get it.

Upvotes: 0

Views: 191

Answers (2)

Alex Martelli
Alex Martelli

Reputation: 881565

If by printf you mean exactly thqt call from C code, you need to redirect (and un-buffer) your standard output (file descriptor 0) to somewhere you can pick up the data from -- far from trivial, esp. in Windows, although maybe doable. But why not just change that call in your C code to something more sensible? (Worst case, a geprintf function of your own devising that mimics printf to build a string then directs that string appropriately).

If you actually mean print statements in Python code, it's much easier -- just set sys.stdout to an object with a write method accepting a string, and you can have that method do whatever you want, including logging, writing on a GUI windows, whatever you wish. Ah were it that simple at the C level!-)

Upvotes: 1

D.Shawley
D.Shawley

Reputation: 59553

Use the logging package instead of printf. You can use something similar if you need to log output from a C function.

Upvotes: 1

Related Questions