Sha256: d1d32cbf621cd836fa38d092fd9e7bbeb1841d9712c599c48aa08f46dd7e1263

Contents?: true

Size: 1.59 KB

Versions: 31

Compression:

Stored size: 1.59 KB

Contents

module ASIR
  # !SLIDE
  # Diagnostic Logging
  #
  # Logging mixin.
  module Log
    attr_accessor :_logger

    def self.included target
      super
      target.send(:extend, ClassMethods)
    end

    @@enabled = false
    def self.enabled
      @@enabled
    end
    def self.enabled= x
      @@enabled = x
    end

    module ClassMethods
      def _log_enabled= x
        (Thread.current[:'ASIR::Log.enabled'] ||= { })[self] = x
      end
      def _log_enabled?
        (Thread.current[:'ASIR::Log.enabled'] ||= { })[self]
      end
    end

    def _log_enabled= x
      @_log_enabled = x
    end

    def _log_enabled?
      ASIR::Log.enabled || 
        @_log_enabled || 
        self.class._log_enabled?
    end

    def _log msg = nil
      return unless _log_enabled?
      msg ||= yield if block_given?
      msg = String === msg ? msg : _log_format(msg)
      msg = "  #{$$} #{Module === self ? self : self.class} #{msg}"
      case @_logger
      when Proc
        @_logger.call msg
      when IO
        @_logger.puts msg
      else
        $stderr.puts msg
      end
      nil
    end

    def _log_result msg
      _log { 
        msg = String === msg ? msg : _log_format(msg);
        "#{msg} => ..." }
      result = yield
      _log { "#{msg} => \n    #{result.inspect}" }
      result
    end

    def _log_format obj
      case obj
      when Exception
        msg = "#{obj.inspect}"
        msg << "\n    #{obj.backtrace * "\n    "}" if false
        msg
      when Array
        obj.map { | x | _log_format x } * ", "
      else
        obj.inspect
      end
    end
  end # module
end # module



Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
asir-1.2.11 lib/asir/log.rb
asir-1.2.10 lib/asir/log.rb
asir-1.2.9 lib/asir/log.rb
asir-1.2.8 lib/asir/log.rb
asir-1.2.7 lib/asir/log.rb
asir-1.2.6 lib/asir/log.rb
asir-1.2.5 lib/asir/log.rb
asir-1.2.3 lib/asir/log.rb
asir-1.2.2 lib/asir/log.rb
asir-1.2.1 lib/asir/log.rb
asir-1.2.0 lib/asir/log.rb
asir-1.1.12 lib/asir/log.rb
asir-1.1.11 lib/asir/log.rb
asir-1.1.10 lib/asir/log.rb
asir-1.1.9 lib/asir/log.rb
asir-1.1.8 lib/asir/log.rb
asir-1.1.7 lib/asir/log.rb
asir-1.1.6 lib/asir/log.rb
asir-1.1.5 lib/asir/log.rb
asir-1.1.4 lib/asir/log.rb