Class: OpenC3::TextLogWriter

Inherits:
LogWriter show all
Defined in:
lib/openc3/logs/text_log_writer.rb

Overview

Creates a text log. Can automatically cycle the log based on an elasped time period or when the log file reaches a predefined size.

Constant Summary

Constants inherited from LogWriter

LogWriter::CLEANUP_DELAY, LogWriter::CYCLE_TIME_INTERVAL

Instance Attribute Summary

Attributes inherited from LogWriter

#cleanup_offsets, #cleanup_times, #cycle_hour, #cycle_minute, #cycle_time, #filename, #logging_enabled, #mutex, #start_time

Instance Method Summary collapse

Methods inherited from LogWriter

#close_file, #create_unique_filename, #cycle_thread_body, #first_time, #first_timestamp, #graceful_kill, #initialize, #last_time, #last_timestamp, #prepare_write, #shutdown, #start, #start_new_file, #stop

Constructor Details

This class inherits a constructor from OpenC3::LogWriter

Instance Method Details

#bucket_filenameObject



59
60
61
62
63
64
65
66
# File 'lib/openc3/logs/text_log_writer.rb', line 59

def bucket_filename
  # Put the name of the redis topic in the filename, but remove the scope
  # because we're already in a directory with the scope name
  redis_topic = @last_offsets.keys[0].to_s
  split_index = redis_topic.index("__") + 2
  topic_name = redis_topic[split_index, redis_topic.length - split_index]
  "#{first_timestamp}__#{last_timestamp}__#{topic_name}" + extension
end

#extensionObject



68
69
70
# File 'lib/openc3/logs/text_log_writer.rb', line 68

def extension
  '.txt'.freeze
end

#write(time_nsec_since_epoch, data, redis_topic, redis_offset) ⇒ Object

Write to the log file.

If no log file currently exists in the filesystem, a new file will be created.

Parameters:

  • time_nsec_since_epoch (Integer)

    64 bit integer nsecs since EPOCH

  • data (String)

    String of data

  • redis_offset (Integer)

    The offset of this packet in its Redis stream



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/openc3/logs/text_log_writer.rb', line 37

def write(time_nsec_since_epoch, data, redis_topic, redis_offset)
  return if !@logging_enabled

  @mutex.synchronize do
    prepare_write(time_nsec_since_epoch, data.length, redis_topic, redis_offset)
    write_entry(time_nsec_since_epoch, data) if @file
  end
rescue => err
  Logger.instance.error "Error writing #{@filename} : #{err.formatted}"
  OpenC3.handle_critical_exception(err)
end

#write_entry(time_nsec_since_epoch, data) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/openc3/logs/text_log_writer.rb', line 49

def write_entry(time_nsec_since_epoch, data)
  @entry.clear
  @entry << "#{time_nsec_since_epoch}\t"
  @entry << "#{data}\n"
  @file.write(@entry)
  @file_size += @entry.length
  @first_time = time_nsec_since_epoch if !@first_time or time_nsec_since_epoch < @first_time
  @last_time = time_nsec_since_epoch if !@last_time or time_nsec_since_epoch > @last_time
end