Question

I have a Ruby script that I have developed that allows me to install and build multiple C++ packages. I am able to execute the Ruby script and install the packages with no errors.

However, I would like to be able to capture all of the output, including cerr, to a "log" file of my choosing. I am able to redirect Ruby's cerr and standard output, but I cannot capture the bash commands: qmake, make, or make install cerr. The output still flows to the terminal.

I want to be able to run the Ruby script and not see any debug messages from any qmake, make, or make install bash commands, but be able to check a log file later for build results.

Was it helpful?

Solution

you can do

require 'open3'

log = File.new("#{your_log_dir}/script.log", "w+")
command = "make"

Open3.popen3(command) do |stdin, stdout, stderr|
     log.puts "[OUTPUT]:\n#{stdout.read}\n"
     unless (err = stderr.read).empty? then 
          log.puts "[ERROR]:\n#{err}\n"
     end
end

OTHER TIPS

%x[#insert bash command here] captures the response. If you need to handle STDERR you'll want to pipe it to STDOUT I believe.

To directly dump stdout and stderr of a child process to files:

cmd = ['ls', '-ahl', '/my/directory'] # example directory listing
pid = Process.spawn *cmd, out: '/path/to/stdout/log', err: '/path/to/stderr/log'
Process.wait pid

You may also pass file descriptors instead of path strings.

If you're on Mac OS or Linux, you can use standard redirection and a simple shell call if you want to capture the STDOUT and STDERR to a variable in your script:

asdf = `ls foo 2>&1`
asdf # => "ls: foo: No such file or directory\n"

2>&1 simply redirects STDERR in the command output to STDOUT, which is captured when the program runs.

If you want to redirect both STDOUT and STDERR to the same file for later, use:

`ls foo > output.log 2>&1`

The STDOUT has to be redirected before &2>1 will take effect, but that will capture both.

For more information see the "Redirect" section of man sh.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top