$ stdout ruby ​​override

#!/usr/bin/ruby

logfile = "/tmp/testruby.log"

@original_stdout = $stderr.dup
@original_stderr = $stderr.dup
$stdout.reopen(logfile, "w")
$stdout.sync = true
$stderr.reopen($stdout)


def restore_stdout
  $stdout.reopen(@original_stdout)
  $stderr.reopen(@original_stderr)
end

def fail_exit(msg)
  puts "- #{msg}" # to the logfile
  restore_stdout
  $stderr.puts "+ #{msg}" # to standard error
  exit!
end

def success_exit(msg)
  puts "- #{msg}" # to the logfile
  restore_stdout  
  $stdout.puts "+ #{msg}" # to standard output
  exit
end

puts "This message goes to the file"

success_exit "A successful exit message"
Mateusz Drewniak