suszterpatt
suszterpatt

Reputation: 8273

Can I globally redirect the output of system() calls?

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

Answers (4)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

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

Conrad Meyer
Conrad Meyer

Reputation: 2886

Close the stdio file descriptors (0, 1, and 2) and re-open them on whatever output device you like.

Upvotes: 2

neuro
neuro

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

SwDevMan81
SwDevMan81

Reputation: 49978

Looks like you can use popen

Upvotes: 4

Related Questions