7

I'm attempting redirect the Stderr file descriptor from inside the process, but there seems to be no implementations of it, and I don't see a clear route to anything similar to dup2 from C/C++.

I've tried:

  • directly implementing Read (impl Read for Stderr), but it'd take an entire library of code to cover.
  • consuming the data in the file descriptor, then into File, then into ReadBuf

    trait FDReader {
        fn consume(&mut self);
    }
    
    impl FDReader for Stderr {
        fn consume(&mut self) {
            let f = std::fs::File::from_raw_fd(self.as_raw_fd());
            let mut extract = String::new();
            BufReader::new(f).read_to_string(&mut extract);
        }
    }
    

    I focused on consume due to the fact I didn't have to exactly return anything when I was testing my code, though this didn't work.

  • since I'm running on a Linux system, and I don't plan to publish the code, I also considered redirecting /proc/self/fd/2 -> /dev/null and then return the original pointer reference when I wanted to write to there. This was way over the top for this scope.

I also thought about using libc::dup2 directly - though I'm weary of it.

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
Skarlett
  • 520
  • 8
  • 21
  • "anything similar to dup2 from C/C++", 1. C/C++ is not a thing 2. `dup2()` is neither a function present in C or C++ standard library. 3. So as in C, your solution will be implementation behavior, thus I think you could find a crate that will do the job for you. – Stargateur Jan 29 '19 at 21:53
  • 1
    @Stargateur - [dup2](http://man7.org/linux/man-pages/man2/dup.2.html) (in both C & C++) is a function provided under unistd.h in the posix implementation. I know the implementation was wrong, which is what I asking for help with. Do you have any crates you'd recommend? Might I also add your comment adds no real context to the conversation. – Skarlett Jan 29 '19 at 22:02
  • I am unclear on what you are trying to do here. Are you trying to read the stderr of a child process, or read back what your own process is writing to stderr, or ...? – harmic Jan 29 '19 at 22:54
  • @harmic I'm attempting to read (or redirect) my own process's stderr pipe – Skarlett Jan 29 '19 at 22:59

1 Answers1

8

There is no way to do this in the standard library1. The gag crate allows redirecting stderr or stdout either to a file or to nothing, but it only works on *nix systems.

In a different view of the problem, I'd encourage you to not use stdout or stderr directly at all. Instead, use dependency injection to pass down values that can be written to. Instead of using println, use writeln.

See also:


1 This isn't strictly true. Have you ever noticed that stdout and stderr are not output during tests? That's because the compiler (and the test suite) make use of a pair of unstable, hidden functions that allow changing the thread-local instances of stdout and stderr.

See also:

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159