Reputation: 8273
I have a program that executes various shell commands via system()
and occasionally prints to cout
. I want to redirect all output coming from system()
calls to a log file so they don't clutter up the normal output. Can I do this without having to append > log
to all my system commands?
Upvotes: 1
Views: 1378
Reputation: 215221
Using system
is just a bad idea, period. If you use fork
and execve
or posix_spawn
, you can easily make the necessary redirections and avoid all sorts of vulnerabilities from shell quoting issues.
Upvotes: 2
Reputation: 2886
Close the stdio file descriptors (0, 1, and 2) and re-open them on whatever output device you like.
Upvotes: 2
Reputation: 15180
If you can use a library that wrap process call. It is hard to code from posix. I use boost.process, it works fine. you can simply tell the lib how you want the output to be redirected...
my2c
Upvotes: 0