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

Version Path
engineyard-serverside-1.3.7 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.3.6 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.3.5 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.3.4 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.3.4.jruby.2 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.3.3 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.3.2 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.3.1 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.3.0 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.2.2 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.2.1 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.2.0 lib/engineyard-serverside/logged_output.rb
ey-deploy-1.1.3 lib/ey-deploy/logged_output.rb
ey-deploy-1.1.2 lib/ey-deploy/logged_output.rb
ey-deploy-1.1.1 lib/ey-deploy/logged_output.rb
ey-deploy-1.1.0 lib/ey-deploy/logged_output.rb
ey-deploy-1.0.2 lib/ey-deploy/logged_output.rb
ey-deploy-1.0.1 lib/ey-deploy/logged_output.rb
ey-deploy-1.0.0 lib/ey-deploy/logged_output.rb