Is there a way in C++ to tell if std::cout and std::cerr are pointing to the same destination?
That is, I'd like to be able to distinguish when the program is launched like
program
or program > log 2>&1
or program &> log
versus
program > log
or program 2> errors
or program > log 2> errors
(The use case is a situation where we'd like error information to be printed to both stdout and stderr when they are separate, but want to print a slightly differently formatted output (not just a concatenation) if they both go to the same destination. -- Yes, I'm aware this isn't ideal, and isn't the officially recommended way to do things, and shouldn't be looked on as a standard way of doing things. But please just trust me, though, that we've taken time to think things through, and for our particular use case this is the best option.)
For our purposes, we can assume that nothing has been done with cout/cerr redirection within the program itself (just the typical shell-level command line redirection), so if there's C-level functionality which looks at stdout/stderr directly (rather than the std::cout and std::cerr streams proper), that would likely work too.
cout
andcerr
end up writing to the same destination or not? – BdelliumGetStdHandle
withGetFileNameFromHandle
to get actual file names for both standard error and standard output; for sure, there are similar Linux functionality. – Berberidaceous