Class: Mbeditor::LogTailService
- Inherits:
-
Object
- Object
- Mbeditor::LogTailService
- Defined in:
- app/services/mbeditor/log_tail_service.rb
Overview
Reads a log file incrementally from a byte offset. Pure Ruby, no subprocess.
read_since(offset) -> { lines: Array
Only complete lines (terminated by "\n") are ever returned; the byte offset advances solely past consumed complete lines, so a partial trailing line is held back and delivered once its newline arrives.
Constant Summary collapse
- BYTE_CAP =
256 * 1024
- INITIAL_TAIL_BYTES =
64 * 1024
Instance Method Summary collapse
-
#initialize(log_path) ⇒ LogTailService
constructor
A new instance of LogTailService.
- #read_since(offset) ⇒ Object
Constructor Details
#initialize(log_path) ⇒ LogTailService
Returns a new instance of LogTailService.
20 21 22 |
# File 'app/services/mbeditor/log_tail_service.rb', line 20 def initialize(log_path) @log_path = Pathname.new(log_path.to_s) end |
Instance Method Details
#read_since(offset) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/services/mbeditor/log_tail_service.rb', line 24 def read_since(offset) return empty(0) unless @log_path.exist? size = @log_path.size if offset.nil? start = [size - INITIAL_TAIL_BYTES, 0].max read_range(start, size, reset: true, trim_leading: start.positive?) elsif offset.to_i > size read_range(0, [size, BYTE_CAP].min, reset: true, trim_leading: false) else start = offset.to_i read_range(start, [start + BYTE_CAP, size].min, reset: false, trim_leading: false) end end |