Class: Trace::EventBuffer

Inherits:
Object
  • Object
show all
Defined in:
app/eventbuffer.rb

Defined Under Namespace

Classes: EventStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maxsize = nil) ⇒ EventBuffer

then marks will drop out as they disappear from the buffer



11
12
13
14
# File 'app/eventbuffer.rb', line 11

def initialize(maxsize=nil)
  @maxsize = maxsize
  reset
end

Instance Attribute Details

#bufObject (readonly)

Returns the value of attribute buf.



6
7
8
# File 'app/eventbuffer.rb', line 6

def buf
  @buf
end

#marksObject

User position mark into buffer. If buffer is limited,



7
8
9
# File 'app/eventbuffer.rb', line 7

def marks
  @marks
end

#maxsizeObject (readonly)

Maximum size of buffer or nil if unlimited.



8
9
10
# File 'app/eventbuffer.rb', line 8

def maxsize
  @maxsize
end

#sizeObject (readonly)

size of buffer



9
10
11
# File 'app/eventbuffer.rb', line 9

def size
  @size
end

Instance Method Details

#add_markObject

Add mark for the current event buffer position.



34
35
36
# File 'app/eventbuffer.rb', line 34

def add_mark
  @marks << @pos
end

#add_mark_nodupObject

Like add mark, but do only if the last marked position has changed



40
41
42
# File 'app/eventbuffer.rb', line 40

def add_mark_nodup
  @marks << @pos unless @marks[-1] == @pos
end

#append(event, frame, arg) ⇒ Object

Add a new event dropping off old events if that was declared marks are also dropped if buffer has a limit.



25
26
27
28
29
30
31
# File 'app/eventbuffer.rb', line 25

def append(event, frame, arg)
  item = EventStruct.new(event, arg, frame)
  @pos = self.succ_pos
  @marks.shift if @marks[0] == @pos
  @buf[@pos] = item
  @size     += 1 unless @maxsize && @size == @maxsize
end

#each(from = nil, to = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/eventbuffer.rb', line 44

def each(from=nil, to=nil)
  from = self.succ_pos unless from
  to   = @pos unless to
  if from <= to
    from.upto(to).each do |pos|
      yield @buf[pos]
    end
  else
    from.upto(@size-1).each do |pos|
      yield @buf[pos]
    end
    0.upto(@pos).each do |pos|
      yield @buf[pos]
    end
  end
end

#each_with_index(from = nil, to = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/eventbuffer.rb', line 61

def each_with_index(from=nil, to=nil)
  from = succ_pos unless from
  to   = @pos     unless to
  if from <= to
    from.upto(to).each do |pos|
      yield [@buf[pos], pos]
    end
  else
    from.upto(@size-1).each do |pos|
      yield [@buf[pos], pos]
    end
    0.upto(@pos).each do |pos|
      yield [@buf[pos], pos]
    end
  end
end

#format_entry(item, long_format = true) ⇒ Object



78
79
80
81
82
83
84
85
# File 'app/eventbuffer.rb', line 78

def format_entry(item, long_format=true)
  # require 'rbdbgr'; Debugger.debug
  mess = "#{item.event} #{item.frame}"
  # if long_format && item.iseq
  #   mess += "\n\t" + "VM offset #{item.pc_offset} of #{item.iseq.name}"
  # end
  mess
end

#pred_pos(dec = 1) ⇒ Object

Return the next event buffer position taking into account that we may have a fixed-sized buffer ring.



96
97
98
99
# File 'app/eventbuffer.rb', line 96

def pred_pos(dec=1)
  pos = @pos - dec
  @maxsize ? pos % @maxsize : pos 
end

#resetObject



16
17
18
19
20
21
# File 'app/eventbuffer.rb', line 16

def reset
  @buf   = []
  @marks = []
  @pos   = -1
  @size  = 0 
end

#succ_pos(inc = 1) ⇒ Object

Return the next event buffer position taking into account that we may have a fixed-sized buffer ring.



89
90
91
92
# File 'app/eventbuffer.rb', line 89

def succ_pos(inc=1)
  pos = @pos + inc 
  @maxsize ? pos % @maxsize : pos 
end

#zero_posObject

Return the adjusted zeroth position in @buf.



102
103
104
105
106
107
108
# File 'app/eventbuffer.rb', line 102

def zero_pos
  if !@maxsize || @buf.size < @maxsize
    0
  else 
    self.succ_pos
  end
end