Sha256: 939d1b3644cc8c29e9626773136110a133e76ff19b0d4f26be5f2948b0c1bc89

Contents?: true

Size: 1.68 KB

Versions: 17

Compression:

Stored size: 1.68 KB

Contents

require 'open4'

module EY
  module Serverside
    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::Serverside::LoggedOutput.verbose?
      end

      def warning(msg)
        info "\nWARNING: #{msg}\n".gsub(/^/,'!> ')
      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::Serverside::LoggedOutput.logfile
      end

    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
engineyard-serverside-1.5.25 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.24 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.21 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.20 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.19 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.19.pre lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.17 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.16 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.15 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.13 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.13.servicespike lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.12 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.10 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.9 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.6 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.4 lib/engineyard-serverside/logged_output.rb
engineyard-serverside-1.5.2 lib/engineyard-serverside/logged_output.rb