Class: OpenC3::StreamLog

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

Overview

Creates a log file of stream data for either reads or writes. Can automatically cycle the log based on when the log file reaches a predefined size or based on time.

Constant Summary collapse

LOG_TYPES =

The allowable log types

[:READ, :WRITE]

Constants inherited from LogWriter

LogWriter::CLEANUP_DELAY, LogWriter::CYCLE_TIME_INTERVAL

Instance Attribute Summary collapse

Attributes inherited from LogWriter

#cleanup_offsets, #cleanup_times, #cycle_hour, #cycle_minute, #cycle_size, #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_timestamp, #graceful_kill, #last_timestamp, #prepare_write, #shutdown, #start, #start_new_file, #stop

Constructor Details

#initialize(log_name, log_type, cycle_time = 600, cycle_size = 50_000_000, cycle_hour = nil, cycle_minute = nil) ⇒ StreamLog

Returns a new instance of StreamLog.

Parameters:

  • log_name (String)

    The name of the stream log. Typically matches the name of the corresponding interface

  • log_type (Symbol)

    The type of log to create. Must be :READ or :WRITE.

  • cycle_time (Integer) (defaults to: 600)

    The amount of time in seconds before creating a new log file. This can be combined with cycle_size.

  • cycle_size (Integer) (defaults to: 50_000_000)

    The size in bytes before creating a new log file. This can be combined with cycle_time.

  • cycle_hour (Integer) (defaults to: nil)

    The time at which to cycle the log. Combined with cycle_minute to cycle the log daily at the specified time. If nil, the log will be cycled hourly at the specified cycle_minute.

  • cycle_minute (Integer) (defaults to: nil)

    The time at which to cycle the log. See cycle_hour for more information.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openc3/logs/stream_log.rb', line 44

def initialize(
  log_name,
  log_type,
  cycle_time = 600, # 10 minutes, matches time in target_model
  cycle_size = 50_000_000, # 50MB, matches size in target_model
  cycle_hour = nil,
  cycle_minute = nil
)
  raise "log_type must be :READ or :WRITE" unless LOG_TYPES.include? log_type

  super(
    "#{ENV['OPENC3_SCOPE']}/stream_logs/",
    true, # Start with logging enabled
    cycle_time,
    cycle_size,
    cycle_hour,
    cycle_minute
  )

  @log_type = log_type
  self.name = log_name
end

Instance Attribute Details

#orig_nameString (readonly)

Returns Original name passed to stream log.

Returns:

  • (String)

    Original name passed to stream log



26
27
28
# File 'lib/openc3/logs/stream_log.rb', line 26

def orig_name
  @orig_name
end

Instance Method Details

#bucket_filenameObject



108
109
110
# File 'lib/openc3/logs/stream_log.rb', line 108

def bucket_filename
  "#{first_timestamp}__#{@log_name}" + extension
end

#cloneObject

Create a clone of this object with a new name



75
76
77
78
79
# File 'lib/openc3/logs/stream_log.rb', line 75

def clone
  stream_log = super()
  stream_log.name = stream_log.orig_name
  stream_log
end

#extensionObject



112
113
114
# File 'lib/openc3/logs/stream_log.rb', line 112

def extension
  '.bin'.freeze
end

#name=(log_name) ⇒ Object

Set the stream log name

Parameters:

  • log_name (String)

    new name



69
70
71
72
# File 'lib/openc3/logs/stream_log.rb', line 69

def name=(log_name)
  @orig_name = log_name
  @log_name = (log_name.to_s.downcase + '_stream_' + @log_type.to_s.downcase).freeze
end

#write(data) ⇒ Object

Write to the log file.

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

Parameters:

  • data (String)

    String of data



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/openc3/logs/stream_log.rb', line 87

def write(data)
  return if !@logging_enabled
  return if !data or data.length <= 0

  @mutex.synchronize do
    time_nsec_since_epoch = Time.now.to_nsec_from_epoch
    prepare_write(time_nsec_since_epoch, data.length)
    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



101
102
103
104
105
106
# File 'lib/openc3/logs/stream_log.rb', line 101

def write_entry(time_nsec_since_epoch, data)
  @file.write(data)
  @file_size += data.length
  @first_time = time_nsec_since_epoch unless @first_time
  @last_time = time_nsec_since_epoch
end