This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Redirect dolfin output to file?

+1 vote

Hi all,

is there a way of redirecting the output of dolfin functions, such as list_timings(), to files in python? I want to log the complete output of a FEniCS program, and I need the python script to do it, so unfortunately redirecting the shell with &> is not an option.
I tried to redirect sys.stdout (and stderr) but it doesn't work for the dolfin functions. I suspect the wrapped C++ functions simply don't use python's stdout.
Anything I can do?

Minimal example:

    import sys
    from dolfin import *

    sys.stdout = open('test.out', 'w')
    sys.stderr = open('test.err', 'w')
    list_krylov_solver_methods()

Cheers,
David

asked May 17, 2017 by dajuno FEniCS User (4,140 points)

1 Answer

0 votes
 
Best answer

You can use the logging module. See for instance this page.
To save output from list_timings you can do

time_table = timings(TimingClear_keep, [TimingType_wall,])
with open("timings.log", "wb") as out:
    out.write(time_table.str(True))
answered May 17, 2017 by finsberg FEniCS User (2,360 points)
selected May 18, 2017 by dajuno

I didn't know the timings() function, good to know! : ) I guess logging still wouldn't catch the FEniCS output though, would it?

You can at least redirect output from FFC and UFL using the logging module, see e.g the answer to this question.

Okay, that'll do. Thanks

Hi, by any chance, do you know if it is possible to register PETSc residual output during KSP/SNES iterations with a python logger?
Thanks!

...