Class: OpenC3::OperatorProcessIO

Inherits:
Tempfile show all
Defined in:
lib/openc3/operators/operator.rb

Overview

Class to prevent an infinitely growing log file

Instance Attribute Summary

Attributes inherited from Tempfile

#filename

Instance Method Summary collapse

Constructor Details

#initialize(label, max_start_lines: 100, max_end_lines: 100) ⇒ OperatorProcessIO

Returns a new instance of OperatorProcessIO.



31
32
33
34
35
36
37
# File 'lib/openc3/operators/operator.rb', line 31

def initialize(label, max_start_lines: 100, max_end_lines: 100)
  super(label)
  @max_start_lines = max_start_lines
  @max_end_lines = max_end_lines
  @start_lines = []
  @end_lines = []
end

Instance Method Details

#extractObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openc3/operators/operator.rb', line 39

def extract
  rewind()
  data = read()
  truncate(0)
  rewind()

  # Save a set number of lines for unexpected death messages
  lines = data.split("\n")
  lines.each do |line|
    if @start_lines.length < @max_start_lines
      @start_lines << line
    else
      @end_lines << line
    end
  end
  if @end_lines.length > @max_end_lines
    @end_lines = @end_lines[(@end_lines.length - @max_end_lines)..-1]
  end

  return data
end

#finalizeObject



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

def finalize
  extract()
  close()
  unlink()

  output = ''
  output << @start_lines.join("\n")
  if @end_lines.length >= @max_end_lines
    output << "\n...\n"
    output << @end_lines.join("\n")
  elsif @end_lines.length > 0
    output << @end_lines.join("\n")
  end
  output << "\n"
  output
end