Method: RunningScript#handle_output_io

Defined in:
lib/openc3/utilities/running_script.rb

#handle_output_io(filename = nil, line_number = nil) ⇒ Object



1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/openc3/utilities/running_script.rb', line 1031

def handle_output_io(filename = nil, line_number = nil)
  filename = @script_status.current_filename if filename.nil?
  line_number = @script_status.line_no if line_number.nil?

  @output_time = Time.now.sys
  if @output_io.string[-1..-1] == "\n"
    time_formatted = Time.now.sys.formatted
    color = 'BLACK'
    lines_to_write = ''
    out_line_number = line_number.to_s
    out_filename = File.basename(filename) if filename

    # Build each line to write
    string = @output_io.string.clone
    @output_io.string = @output_io.string[string.length..-1]
    line_count = 0
    string.each_line(chomp: true) do |out_line|
      begin
        json = JSON.parse(out_line, :allow_nan => true, :create_additions => true)
        time_formatted = Time.parse(json["@timestamp"]).sys.formatted if json["@timestamp"]
        if json["log"]
          out_line = json["log"]
        elsif json["message"]
          out_line = json["message"]
        end
      rescue
        # Regular output
      end

      if out_line.length >= 25 and out_line[0..1] == '20' and out_line[10] == ' ' and out_line[23..24] == ' ('
        line_to_write = out_line
      else
        if filename
          line_to_write = time_formatted + " (#{out_filename}:#{out_line_number}): " + out_line
        else
          line_to_write = time_formatted + " (SCRIPTRUNNER): " + out_line
          color = 'BLUE'
        end
      end
      lines_to_write << (line_to_write + "\n")
      line_count += 1
    end # string.each_line

    if lines_to_write.length > @@max_output_characters
      # We want the full @@max_output_characters so don't subtract the additional "ERROR: ..." text
      published_lines = lines_to_write[0...@@max_output_characters]
      published_lines << "\nERROR: Too much to publish. Truncating #{lines_to_write.length} characters of output to #{@@max_output_characters} characters.\n"
    else
      published_lines = lines_to_write
    end
    running_script_anycable_publish("running-script-channel:#{@script_status.id}", { type: :output, line: published_lines.as_json(:allow_nan => true), color: color })
    # Add to the message log
    message_log.write(lines_to_write)
  end
end