Sha256: 87d0bd79346cef25ea053f746a5f2ff347277b7f3741758545f55b2b913fb97e
Contents?: true
Size: 1.43 KB
Versions: 19
Compression:
Stored size: 1.43 KB
Contents
require 'open4' module EY module LoggedOutput class Tee def initialize(*streams) @streams = streams.flatten end def <<(output) @streams.each do |s| s << output s.flush end self end end # Tee @@logfile = File.join(ENV['HOME'], 'ey.log') def self.logfile=(filename) File.unlink filename if File.exist?(filename) # start fresh @@logfile = filename end def self.logfile @@logfile end @@verbose = false def self.verbose=(v) @@verbose = !!v end def self.verbose? @@verbose end def verbose? EY::LoggedOutput.verbose? end def info(msg) with_logfile do |log| Tee.new($stdout, log) << (msg + "\n") end end def debug(msg) with_logfile do |log| log << "#{msg}\n" end end def logged_system(cmd) with_logfile do |log| out = verbose? ? Tee.new($stdout, log) : log err = Tee.new($stderr, log) # we always want to see errors out << ":: running #{cmd}\n" # :quiet means don't raise an error on nonzero exit status status = Open4.spawn cmd, 0 => '', 1 => out, 2 => err, :quiet => true status.exitstatus == 0 end end private def with_logfile File.open(logfile, 'a') {|f| yield f } end def logfile EY::LoggedOutput.logfile end end end
Version data entries
19 entries across 19 versions & 2 rubygems