Module: Autolog

Defined in:
lib/autolog.rb,
lib/autolog/methods.rb,
lib/autolog/version.rb

Defined Under Namespace

Modules: Methods

Constant Summary collapse

VERSION =
'0.3.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.filtered_procsObject

Returns the value of attribute filtered_procs.



10
11
12
# File 'lib/autolog.rb', line 10

def filtered_procs
  @filtered_procs
end

.last_argsObject

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



7
8
9
# File 'lib/autolog.rb', line 7

def last_args
  @last_args
end

.levelObject

Returns the value of attribute level.



8
9
10
# File 'lib/autolog.rb', line 8

def level
  @level
end

.procedureObject

Returns the value of attribute procedure.



9
10
11
# File 'lib/autolog.rb', line 9

def procedure
  @procedure
end

.unfiltered_procsObject

Returns the value of attribute unfiltered_procs.



11
12
13
# File 'lib/autolog.rb', line 11

def unfiltered_procs
  @unfiltered_procs
end

Class Method Details

.convert_args(*args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

.events(*args) ⇒ Object Also known as: event

log all specified events



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

.filtered_proc(name, procedure) ⇒ Object



13
14
15
# File 'lib/autolog.rb', line 13

def filtered_proc(name, procedure)
  filtered_procs[name.to_sym] = procedure
end

.off(*args) ⇒ Object

turn logging off



96
97
98
99
# File 'lib/autolog.rb', line 96

def off(*args)
  set_trace_func nil
  Autolog.level = 0
end

.unfiltered_proc(name, procedure) ⇒ Object



17
18
19
# File 'lib/autolog.rb', line 17

def unfiltered_proc(name, procedure)
  unfiltered_procs[name.to_sym] = procedure
end