Writing to both stdout & a file

前端 未结 7 560
耶瑟儿~
耶瑟儿~ 2020-12-07 03:17

I have a parent process which forks out a child to perform execv().

I need the output given by execv() to stdout to be displayed onscreen as also copied to a log fil

相关标签:
7条回答
  • 2020-12-07 03:58

    If you don't want to use a tee, before you write the data, write it to a file, then send it to stdout.

    You should write a logging function that does this for you to make it cleaner.

    0 讨论(0)
  • 2020-12-07 03:58

    Also, you can use fifo's. mkfifo my.fifo; in execv: program > my.fifo; and open fifo as a regular file, reading from it. This way you can have your stdout parsed, but it has minor drawbacks with shared access.

    0 讨论(0)
  • 2020-12-07 04:02

    Do you want only the output of the child to go to the log?

    The tee unix command does exactly what you describe: you pipe in data and it writes to a log and to stdout.

    0 讨论(0)
  • 2020-12-07 04:06

    Pipe it through tee.

    0 讨论(0)
  • 2020-12-07 04:07

    Use Tee like this:

    ./myprog | tee outputfile
    
    0 讨论(0)
  • 2020-12-07 04:10

    You can use dup2() - this link provides an example

    0 讨论(0)
提交回复
热议问题