Class: OpenC3::StreamLog
- 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
-
#orig_name ⇒ String
readonly
Original name passed to stream log.
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
- #bucket_filename ⇒ Object
-
#clone ⇒ Object
Create a clone of this object with a new name.
- #extension ⇒ Object
-
#initialize(log_name, log_type, cycle_time = 600, cycle_size = 50_000_000, cycle_hour = nil, cycle_minute = nil) ⇒ StreamLog
constructor
A new instance of StreamLog.
-
#name=(log_name) ⇒ Object
Set the stream log name.
-
#write(data) ⇒ Object
Write to the log file.
- #write_entry(time_nsec_since_epoch, data) ⇒ Object
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.
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_name ⇒ String (readonly)
Returns 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_filename ⇒ Object
108 109 110 |
# File 'lib/openc3/logs/stream_log.rb', line 108 def bucket_filename "#{}__#{@log_name}" + extension end |
#clone ⇒ Object
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 |
#extension ⇒ Object
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
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.
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 |