module Autolog

Constants

VERSION

Attributes

filtered_procs[RW]
last_args[RW]

called procedure instead of proc because set_trace_func proc was calling the proc attribute. Fun!

level[RW]
procedure[RW]
unfiltered_procs[RW]

Public Class Methods

convert_args(*args) click to toggle source
# File lib/autolog.rb, line 67
def convert_args(*args)
  result = []
  args.each do |a|
    case a
    when :trace
    when :c_calls; result << 'c-call'
    when :c_return; result << 'c-return'
    when :c_calls_and_returns; result << 'c-call' << 'c-return'
    when :class_starts; result << 'class'
    when :class_ends; result << 'end'
    when :classes; result << 'class' << 'end'
    when :method_calls; result << 'call'
    when :method_returns; result << 'return'
    when :methods; result << 'call' << 'return'
    when :raises; result << 'raise'
    when :lines; result << 'line'
    else
      a = a.to_s.gsub('_','-') if a.is_a?(Symbol)
      result << a
    end
  end
  result
end
event(*args)
Alias for: events
events(*args) { || ... } click to toggle source

log all specified events

# File lib/autolog.rb, line 22
def events(*args)
  args = convert_args(*args)

  Autolog.last_args = args.dup # to allow access by custom procs later in set_trace_func. only "single-context-safe"

  using = nil
  if args.size > 0 && args.last.is_a?(Hash)
    options = args.pop
    using = options[:using] ? options[:using].to_s.to_sym : options[:format] ? options[:format].to_s.to_sym : nil
  end

  if unfiltered_procs[using]
    # What's up with the Exception hiding in the body of the procs?
    # Ruby bug 7180: can use up 100% cpu in 1.9.3p194 if let anything be raised. We'll silently rescue and ignore issues. Otherwise, it produces a deluge of output.
    eval "set_trace_func proc {|event, file, line, id, binding, classname| begin; Autolog.unfiltered_procs[#{using.inspect}].call(event, file, line, id, binding, classname); rescue SystemExit, Interrupt; raise; rescue Exception; end}"
  else
    if using && !filtered_procs.has_key?(using)
      raise "Unregistered format/using: #{using.inspect}"
    end

    proc_string = using ? "Autolog.filtered_procs[#{using.inspect}]" : 'Autolog.procedure'
    
    if args.size > 0
      # What's up with the Exception hiding in the body of the procs?
      # Ruby bug 7180: can use up 100% cpu in 1.9.3p194 if let anything be raised. We'll silently rescue and ignore issues. Otherwise, it produces a deluge of output.
      if args.size == 1
        eval "set_trace_func proc {|event, file, line, id, binding, classname| begin; #{proc_string}.call(event, file, line, id, binding, classname) if event == #{args[0].inspect}; rescue SystemExit, Interrupt; raise; rescue Exception; end}"
      elsif args.size > 1
        eval "set_trace_func proc {|event, file, line, id, binding, classname| begin; #{proc_string}.call(event, file, line, id, binding, classname) if #{args.inspect}.include?(event); rescue SystemExit, Interrupt; raise; rescue Exception; end}"
      end
    else
      eval "set_trace_func proc {|event, file, line, id, binding, classname| begin; #{proc_string}.call(event, file, line, id, binding, classname); rescue SystemExit, Interrupt; raise; rescue Exception; end}"
    end
  end

  if block_given?
    begin
      yield
    ensure
      off
    end
  end
end
Also aliased as: event
filtered_proc(name, procedure) click to toggle source
# File lib/autolog.rb, line 13
def filtered_proc(name, procedure)
  filtered_procs[name.to_sym] = procedure
end
off(*args) click to toggle source

turn logging off

# File lib/autolog.rb, line 96
def off(*args)
  set_trace_func nil
  Autolog.level = 0
end
unfiltered_proc(name, procedure) click to toggle source
# File lib/autolog.rb, line 17
def unfiltered_proc(name, procedure)
  unfiltered_procs[name.to_sym] = procedure
end