Sha256: 2e7323d71a593ec76c357c0da0899b0a9f08ae8bd1b94a2fd7f59a95e64e8715

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

require 'logger'
class Bj
  class Logger < ::Logger
    def self.new *a, &b
      super(*a, &b).instance_eval{ @default_formatter = @formatter = Formatter.new; self }
    end
    def format_message(severity, datetime, progname, msg)
      (@formatter || @default_formatter).call(severity, datetime, progname, msg)
    end

    def device
      @logdev.instance_eval{ @dev }
    end

    def tty?
      device.respond_to?('tty?') and device.tty?
    end

    def turn which
      @logdev.extend OnOff unless OnOff === @logdev
      @logdev.turn which
    end

    module OnOff
      def turn which
        @turned = which.to_s =~ %r/on/i ? :on : :off
      end

      def write message 
        return message.to_s.size if @turned == :off
        super
      end
    end

    def on
      turn :on
    end
    alias_method "on!", "on"
    def self.on *a, &b
      new(*a, &b).instance_eval{ turn :on; self }
    end

    def off
      turn :off
    end
    alias_method "off!", "off"
    def self.off *a, &b
      new(*a, &b).instance_eval{ turn :off; self }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
SciMed-bj-1.3.0 lib/bj/logger.rb
SciMed-bj-1.2.5 lib/bj/logger.rb
SciMed-bj-1.2.4 lib/bj/logger.rb