Duplicate stdout and stderr from fork process to files

后端 未结 1 1408
不思量自难忘°
不思量自难忘° 2021-01-26 07:31

I need to duplicate stdout and stderr of a child proccess to multiple files.
I understand that i can use tee(), but I haven\'t found e

相关标签:
1条回答
  • 2021-01-26 08:00

    Using tee and splice to write data from a handle to multiple other handles:

    // Needs at least two targets, sentinel is INVALID_HANDLE_VALUE
    int push_to_all(int source, ssize_t count, ...) {
        va_list vl;
        va_start(vl, count);
        int target = va_arg(vl, int), next = va_arg(vl, int);
        count = tee(source, target, count, SPLICE_F_NONBLOCK);
        if(count <= 0) { va_end(vl); return count; }
        while((target = next, next = va_arg(vl, int)) > 0) {
            tee(source, target, count, 0);
        va_end(vl);
        return splice(source, 0, target, 0, count, 0);
    }
    
    0 讨论(0)
提交回复
热议问题