module Tailog::WatchMethods

Constants

RAW_METHOD_PREFIX

Attributes

inject_options[RW]

Public Class Methods

logger() click to toggle source
# File lib/tailog/watch_methods.rb, line 11
def logger
  return @logger if @logger
  @logger = Logger.new(File.join Tailog.log_path, "watch_methods.log")
  @logger.formatter = proc do |severity, datetime, progname, message|
    content = ""
    content << "[#{datetime.strftime("%Y-%m-%d %H:%M:%S")}]"
    content << "[#{Tailog.request_id}]" if Tailog.request_id
    content << " #{severity.rjust(5)}"
    content << " (#{progname})" if progname
    content << ": #{message.gsub(/\n\s*/, " ")}"
    content << "\n"
    content
  end
  @logger
end

Public Instance Methods

cleanup(targets) click to toggle source
# File lib/tailog/watch_methods.rb, line 54
def cleanup targets
  WatchMethods.logger.debug "Cleanup #{targets}."
  targets.each do |target|
    if target.include? "#"
      cleanup_instance_method target
    elsif target.include? "."
      cleanup_class_method target
    else
      cleanup_constant target
    end
  end
end
inject(targets, options = {}) click to toggle source
# File lib/tailog/watch_methods.rb, line 36
def inject targets, options = {}
  WatchMethods.logger.debug "Inject #{targets} with options #{options}."
  options = Tailog::WatchMethods.inject_options.merge(options)
  targets.each do |target|
    begin
      if target.include? "#"
        inject_instance_method target, options
      elsif target.include? "."
        inject_class_method target, options
      else
        inject_constant target, options
      end
    rescue => error
      WatchMethods.logger.error "Inject #{target} FAILED: #{error.class}: #{error.message}."
    end
  end
end

Private Instance Methods

build_cleanup_method(target, method) click to toggle source
# File lib/tailog/watch_methods.rb, line 137
    def build_cleanup_method target, method
      raw_method = "#{RAW_METHOD_PREFIX}#{method}"
      return <<-EOS
        if method_defined? :#{raw_method}
          alias_method :#{method}, :#{raw_method}
          remove_method :#{raw_method}
        end
      EOS
    end
build_watch_method(target, method, options) click to toggle source
# File lib/tailog/watch_methods.rb, line 132
def build_watch_method target, method, options
  raw_method = "#{RAW_METHOD_PREFIX}#{method}"
  ERB.new(WATCH_METHOD_ERB).result(binding)
end
cleanup_class_method(target) click to toggle source
# File lib/tailog/watch_methods.rb, line 109
    def cleanup_class_method target
      klass, _, method = target.rpartition(".")
      klass.constantize.class_eval <<-EOS, __FILE__, __LINE__
        class << self
          #{build_cleanup_method target, method}
        end
      EOS
    end
cleanup_constant(target) click to toggle source
# File lib/tailog/watch_methods.rb, line 90
def cleanup_constant target
  constant = target.constantize
  constant.instance_methods(false).each do |method|
    cleanup_instance_method "#{target}##{method}" unless raw_method? method
  end
  constant.methods(false).each do |method|
    cleanup_class_method "#{target}.#{method}" unless raw_method? method
  end
end
cleanup_instance_method(target) click to toggle source
# File lib/tailog/watch_methods.rb, line 125
    def cleanup_instance_method target
      klass, _, method = target.rpartition("#")
      klass.constantize.class_eval <<-EOS, __FILE__, __LINE__
        #{build_cleanup_method target, method}
      EOS
    end
inject_class_method(target, options) click to toggle source
# File lib/tailog/watch_methods.rb, line 100
    def inject_class_method target, options
      klass, _, method = target.rpartition(".")
      klass.constantize.class_eval <<-EOS, __FILE__, __LINE__
        class << self
          #{build_watch_method target, method, options}
        end
      EOS
    end
inject_constant(target, options) click to toggle source
# File lib/tailog/watch_methods.rb, line 75
def inject_constant target, options
  unless const_defined? target
    WatchMethods.logger.error "Inject #{target} FAILED: NameError: uninitialized constant #{target}."
    return
  end

  constant = target.constantize
  constant.instance_methods(false).each do |method|
    inject_instance_method "#{target}##{method}", options unless raw_method? method
  end
  constant.methods(false).each do |method|
    inject_class_method "#{target}.#{method}", options unless raw_method? method
  end
end
inject_instance_method(target, options) click to toggle source
# File lib/tailog/watch_methods.rb, line 118
    def inject_instance_method target, options
      klass, _, method = target.rpartition("#")
      klass.constantize.class_eval <<-EOS, __FILE__, __LINE__
        #{build_watch_method target, method, options}
      EOS
    end
raw_method?(method) click to toggle source
# File lib/tailog/watch_methods.rb, line 71
def raw_method? method
  method.to_s.start_with? RAW_METHOD_PREFIX
end