Hi Adelene,

WrapLogs() is in a way the equivalent of the bash tee command; it allows
you to redirect stderr to a Python stream of your choice, but it does not
suppress the original C++ stderr stream.
If you wish to suppress it, you may redirect your wrap_logs.py script's
stderr to /dev/null in your shell:

$ python wrap_logs.py 2> /dev/null

Alternatively, you may suppress the C++ stderr stream from within your
Python script modifying it as follows:

from rdkit import Chem
from contextlib import redirect_stderr
import ctypes

Chem.WrapLogs()

libc = ctypes.CDLL(None)
c_stderr = ctypes.c_void_p.in_dll(libc, "stderr")
c_freopen = libc.freopen

c_freopen(b"/dev/null", b"w", c_stderr)

with open('log.txt', 'w') as f:
    with redirect_stderr(f):
        mol = Chem.MolFromSmiles("c1ccccc")

Then, when you run

$ python wrap_logs.py

the C++ stderr stream will be redirected to /dev/null, and your log message
will only be visible in log.txt.

Note that the above will work on Linux only.

Cheers,
p.

On Fri, Jun 18, 2021 at 6:22 PM Adelene LAI <adelene....@uni.lu> wrote:

> Hi RDKit Community,
>
>
> I'm trying to run a script in the command line without having any RDKit
> warnings or errors show up in the CL. Instead, I want them written into a
> log.txt
>
>
>
> from rdkit import Chem
> from contextlib import redirect_stderr
>
> Chem.WrapLogs()
>
> with open('log.txt', 'w') as f:
>     with redirect_stderr(f):
>         mol = Chem.MolFromSmiles("c1ccccc")
>
>
>
> The error does indeed get written to the log.txt file (I'm assuming
> warnings would be too).
>
> What is strange is that the stderr still shows up in the command line,
> even though I'd already redirected it to the log.txt.
>
> Does this mean that there are two stderr streams? How is this possible?
>
> I've been reading several old posts on this topic, but none really fits my
> problem:
>
>
> https://sourceforge.net/p/rdkit/mailman/rdkit-discuss/thread/CAOC-GK0oTH36vvL7eVyWMJg4zmERpqctonrgNnxG10QmgYXhdg%40mail.gmail.com/#msg36030331
>
> https://sourceforge.net/p/rdkit/mailman/message/33261506/  <- addressed
> by WrapLogs() I believe
>
>
> <https://sourceforge.net/p/rdkit/mailman/rdkit-discuss/thread/CAOC-GK0oTH36vvL7eVyWMJg4zmERpqctonrgNnxG10QmgYXhdg%40mail.gmail.com/#msg36030331>
> http://rdkit.blogspot.com/2016/03/capturing-error-information.html
>
> https://github.com/rdkit/rdkit/pull/739
>
> Would appreciate any ideas.
>
> Thanks,
> Adelene
>
>
> <https://github.com/rdkit/rdkit/pull/739>
>
>
>
>
>
>
> Doctoral Researcher
>
> Environmental Cheminformatics
>
> UNIVERSITÉ DU LUXEMBOURG
>
>
> Campus Belval | Luxembourg Centre for Systems Biomedicine
>
> 6, avenue du Swing, L-4367 Belvaux
>
> T +356 46 66 44 67 18
>
> https://adelenel.ai
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Rdkit-discuss mailing list
> Rdkit-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to