minrk / wurlitzer

Capture C-level stdout/stderr in Python
MIT License
194 stars 24 forks source link

Tee support #56

Open minrk opened 2 years ago

minrk commented 2 years ago

Sometimes when I capture output, I want to not just redirect it, but tee it (write to both the original and redirected files, instead of only the redirected file). This could probably be a simple flag, since we already have an internal handle on the original FD for restoring later.

laf070810 commented 6 days ago

Currently is there a way to tee to both console output and file output in jupyter notebook? In jupyter notebook, we have cell output, console output and file output. By doing like

from wurlitzer import pipes, STDOUT
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.FileHandler("mycode.log"))

with pipes(logger, stderr=STDOUT):
    call_some_c_function()

I can tee to cell output and file output. But I haven't found a way to tee to the console output and file output. Using like logger.addHandler(logging.StreamHandler(sys.__stdout__)) will cause infinite loop.