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.2.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.flushObject

Returns the value of attribute flush.



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

def flush
  @flush
end

.procedureObject

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 procedure
  @procedure
end

Class Method Details

.c_calls(*args) ⇒ Object

log c-call events only



44
45
46
47
48
49
50
# File 'lib/autolog.rb', line 44

def c_calls(*args)
  if block_given?
    events ['c-call', args].flatten, &Proc.new
  else
    events ['c-call', args].flatten
  end
end

.c_calls_and_returns(*args) ⇒ Object

log c-call and c-return events only



62
63
64
65
66
67
68
# File 'lib/autolog.rb', line 62

def c_calls_and_returns(*args)
  if block_given?
    events ['c-call', 'c-return', args].flatten, &Proc.new
  else
    events ['c-call', 'c-return', args].flatten
  end
end

.c_returns(*args) ⇒ Object

log c-return events only



53
54
55
56
57
58
59
# File 'lib/autolog.rb', line 53

def c_returns(*args)
  if block_given?
    events ['c-return', args].flatten, &Proc.new
  else
    events ['c-return', args].flatten
  end
end

.class_ends(*args) ⇒ Object

log end events only



80
81
82
83
84
85
86
# File 'lib/autolog.rb', line 80

def class_ends(*args)
  if block_given?
    events ['end', args].flatten, &Proc.new
  else
    events ['end', args].flatten
  end
end

.class_starts(*args) ⇒ Object

log class events only



71
72
73
74
75
76
77
# File 'lib/autolog.rb', line 71

def class_starts(*args)
  if block_given?
    events ['class', args].flatten, &Proc.new
  else
    events ['class', args].flatten
  end
end

.classes(*args) ⇒ Object

log class and end events only



89
90
91
92
93
94
95
# File 'lib/autolog.rb', line 89

def classes(*args)
  if block_given?
    events ['class', 'end', args].flatten, &Proc.new
  else
    events ['class', 'end', args].flatten
  end
end

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

log all specified events



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/autolog.rb', line 11

def events(*args)
  args.flatten!
  args.collect!{|e|e.to_s.gsub('_','-')}

  # What's up with the Exception hiding?
  # 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; Autolog.procedure.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; Autolog.procedure.call(event, file, line, id, binding, classname) if #{args.inspect}.include?(event); rescue SystemExit, Interrupt; raise; rescue Exception; end}"
  else
    set_trace_func proc {|event, file, line, id, binding, classname| begin; Autolog.procedure.call(event, file, line, id, binding, classname); rescue SystemExit, Interrupt; raise; rescue Exception; end}
  end

  if block_given?
    begin
      yield
    ensure
      off
    end
  end
end

.lines(*args) ⇒ Object

log line events only



134
135
136
137
138
139
140
# File 'lib/autolog.rb', line 134

def lines(*args)
  if block_given?
    events ['line', args].flatten, &Proc.new
  else
    events ['line', args].flatten
  end
end

.method_calls(*args) ⇒ Object

log call events only



98
99
100
101
102
103
104
# File 'lib/autolog.rb', line 98

def method_calls(*args)
  if block_given?
    events ['call', args].flatten, &Proc.new
  else
    events ['call', args].flatten
  end
end

.method_returns(*args) ⇒ Object

log return events only



107
108
109
110
111
112
113
# File 'lib/autolog.rb', line 107

def method_returns(*args)
  if block_given?
    events ['return', args].flatten, &Proc.new
  else
    events ['return', args].flatten
  end
end

.methods(*args) ⇒ Object

log call and return events only



116
117
118
119
120
121
122
# File 'lib/autolog.rb', line 116

def methods(*args)
  if block_given?
    events ['call', 'return'], &Proc.new
  else
    events ['call', 'return', args].flatten
  end
end

.off(*args) ⇒ Object

turn logging off



143
144
145
146
# File 'lib/autolog.rb', line 143

def off(*args)
  # accepts *args to make implementation of autolog in methods easier, but ignores them
  set_trace_func nil
end

.raises(*args) ⇒ Object

log raise events only



125
126
127
128
129
130
131
# File 'lib/autolog.rb', line 125

def raises(*args)
  if block_given?
    events ['raise', args].flatten, &Proc.new
  else
    events ['raise', args].flatten
  end
end

.trace(*args) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/autolog.rb', line 35

def trace(*args)
  if block_given?
    events args, &Proc.new
  else
    events args
  end
end