Can you fool isatty AND log stdout and stderr separately?

前端 未结 4 1773
悲&欢浪女
悲&欢浪女 2020-12-28 12:55

Problem

So you want to log the stdout and stderr (separately) of a process or subprocess, without the output being different from what you\'d see in the terminal i

4条回答
  •  没有蜡笔的小新
    2020-12-28 13:21

    For a simpler use-case (e.g. development testing), use strace (linux) or dtruss (OSX). Of course that won't work in privileged process.

    Here's a sample, you can distinguish stdout fd1 from stderr fd2:

    $ strace -ewrite python2 test.py
    [snip]
    write(1, "This is a real tty :)\n", 22This is a real tty :)
    ) = 22
    write(2, "standard error data", 19standard error data)     = 19
    write(1, "standard output data", 20standard output data)    = 20
    +++ exited with 0 +++
    

    In the sample above you see each standard xxx data doubled, because you can't redirect stdout/stderr. You can, however ask strace to save its output to a file.

    On a theoretical side, if stdout and stderr refer to the same terminal, you can only distinguish between the 2 while still in the context of your process, either in user mode (LD_PRELOAD), or kernel space (ptrace interface that strace tool uses). Once the data hits actual device, real of pseudo, the distinction is lost.

提交回复
热议问题