Class: Lumberjack::Device::Buffer::EntryBuffer
- Inherits:
-
Object
- Object
- Lumberjack::Device::Buffer::EntryBuffer
- Defined in:
- lib/lumberjack/device/buffer.rb
Overview
Internal class that manages the entry buffer and flushing logic.
Instance Attribute Summary collapse
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#last_flushed_at ⇒ Object
readonly
Returns the value of attribute last_flushed_at.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #<<(entry) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #empty? ⇒ Boolean
- #flush ⇒ Object
-
#initialize(device, size, before_flush) ⇒ EntryBuffer
constructor
A new instance of EntryBuffer.
- #reopen ⇒ Object
Constructor Details
#initialize(device, size, before_flush) ⇒ EntryBuffer
Returns a new instance of EntryBuffer.
23 24 25 26 27 28 29 30 31 |
# File 'lib/lumberjack/device/buffer.rb', line 23 def initialize(device, size, before_flush) @device = device @size = size @before_flush = before_flush if before_flush.respond_to?(:call) @lock = Mutex.new @entries = [] @last_flushed_at = Time.now @closed = false end |
Instance Attribute Details
#device ⇒ Object (readonly)
Returns the value of attribute device.
21 22 23 |
# File 'lib/lumberjack/device/buffer.rb', line 21 def device @device end |
#last_flushed_at ⇒ Object (readonly)
Returns the value of attribute last_flushed_at.
21 22 23 |
# File 'lib/lumberjack/device/buffer.rb', line 21 def last_flushed_at @last_flushed_at end |
#size ⇒ Object
Returns the value of attribute size.
19 20 21 |
# File 'lib/lumberjack/device/buffer.rb', line 19 def size @size end |
Instance Method Details
#<<(entry) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/lumberjack/device/buffer.rb', line 33 def <<(entry) return if closed? @lock.synchronize do @entries << entry end flush if @entries.size >= @size end |
#close ⇒ Object
69 70 71 72 |
# File 'lib/lumberjack/device/buffer.rb', line 69 def close @closed = true flush end |
#closed? ⇒ Boolean
74 75 76 |
# File 'lib/lumberjack/device/buffer.rb', line 74 def closed? @closed end |
#empty? ⇒ Boolean
82 83 84 |
# File 'lib/lumberjack/device/buffer.rb', line 82 def empty? @entries.empty? end |
#flush ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lumberjack/device/buffer.rb', line 43 def flush entries = nil if closed? @before_flush&.call entries = @entries @entries = [] else @lock.synchronize do @before_flush&.call entries = @entries @entries = [] end end @last_flushed_at = Time.now return if entries.nil? entries.each do |entry| @device.write(entry) rescue => e warn("Error writing log entry from buffer: #{e.inspect}") end end |
#reopen ⇒ Object
78 79 80 |
# File 'lib/lumberjack/device/buffer.rb', line 78 def reopen @closed = false end |