Comment 5 for bug 1072694

Revision history for this message
corrado maurini (corrado-maurini) wrote : Re: [Bug 1072694] list_timings() cannot be redirected to a file or a variable in python

Johan,

Thanks a lot for taking care of this.

If redirecting ostream through python is too difficult, for the specific case a possible alternative can be to have a function similar to list_timings(), but giving a dolfin Table as output.

Then the Table is a python object and one can use the associated methods to access data or print it out (event in latex format!).

I have not done this directly because I do not if this may conflict with your general design principles for the logger.

Corrado

Le 30 oct. 2012 à 11:02, Marco Morandini a écrit :

> On 10/30/2012 08:00 AM, Johan Hake wrote:
>> Marco, what would the syntax be for adding a file as the log output. Is
>> it possible to add an already existing file pointer or just the file
>> name as a string? This would be the ultimate solution as we then can
>> merge the two log streams from dolfin and logging dependent python
>> modules such as UFL.
>>
>> My boost python skills are really limited so following the inner
>> workings of this small file is hard for me...
>>
>
> In my old code (where everything is wrapped with boost)
>
> after
>
> fileout = ostream(cout_buf())
>
> fileout will point to std::cout,
>
> and after
>
> fileout = ostream('new_file_name.txt')
>
> fileout will point to a newly opened ofstream.
>
> Then, fileout can be passed to any wrapped c++ function that
> has an std::ostream & argument.
>
> I.e., if I have this c++ function
>
> void DxOutput(std::ostream & out, const MyFloat t);
>
> and I wrap it as
>
> BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(ofstream_open_overloads,
> std::ofstream::open, 1, 2);
> BOOST_PYTHON_MODULE_INIT(_python_util)
> {
> ....
>
> the wrapping code I've attached
>
> ....
>
> python::def("DxOutput", DxOutput);
> }
>
> in python I can do
>
> t=0.01
> fileout = ostream('new_file_name.txt')
> DxOutput(fileout, t)
>
> if I want to write with DxOutput into new_file_name.txt,
>
> or
>
> fileout = ostream(cout_buf())
> DxOutput(fileout, t)
>
> If I want DxOuptut's output on the screen.
>
> Note however that I can't write directly from python into fileout
> (and I fear that this is what you are looking for :( .
> At least, I've not even tried to do it (perhaps one should start
> extractig the file descriptor, as explained here:
>
> http://www.ginac.de/~kreckel/fileno/
>
> )
>
> Or
> http://stackoverflow.com/questions/2378005/c-iostreams-and-python
>
>
> Hope this helps.
>
> --
> You received this bug notification because you are a member of DOLFIN
> Team, which is subscribed to DOLFIN.
> https://bugs.launchpad.net/bugs/1072694
>
> Title:
> list_timings() cannot be redirected to a file or a variable in python
>
> Status in DOLFIN:
> Confirmed
>
> Bug description:
> In python it is not possible to redirect the output of list_timings()
> to a file.
>
> set_output_stream(std::ostream& out) in log.ccp is not working in
> python because of problems in wrapping cpp ostream
>
> see also https://answers.launchpad.net/dolfin/+question/211271
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/dolfin/+bug/1072694/+subscriptions