Class: OpenC3::RawLogger
Overview
Creates a log file of raw data for either reads or writes. Can automatically cycle the log based on when the log file reaches a predefined size.
Constant Summary collapse
- LOG_TYPES =
The allowable log types
[:READ, :WRITE]
- CYCLE_TIME_INTERVAL =
The cycle time interval. Cycle times are only checked at this level of granularity.
60
Instance Attribute Summary collapse
-
#filename ⇒ String
readonly
The filename of the log.
-
#logging_enabled ⇒ Boolean
readonly
Is logging enabled?.
-
#orig_name ⇒ String
readonly
Original name passed to raw logger.
-
#queue ⇒ Queue
readonly
Queue for asynchronous logging.
Instance Method Summary collapse
-
#clone ⇒ Object
Create a clone of this object with a new name.
-
#initialize(log_name, log_type, log_directory, logging_enabled = false, cycle_size = 2000000000) ⇒ RawLogger
constructor
A new instance of RawLogger.
-
#name=(log_name) ⇒ Object
Set the raw logger name.
-
#start ⇒ Object
Starts a new log file by closing the existing log file.
-
#stop ⇒ Object
Stops all logging and closes the current log file.
-
#write(data) ⇒ Object
Write data to the log file.
Constructor Details
#initialize(log_name, log_type, log_directory, logging_enabled = false, cycle_size = 2000000000) ⇒ RawLogger
Returns a new instance of RawLogger.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/openc3/io/raw_logger.rb', line 57 def initialize( log_name, log_type, log_directory, logging_enabled = false, cycle_size = 2000000000 ) raise "log_type must be :READ or :WRITE" unless LOG_TYPES.include? log_type @log_type = log_type @orig_name = log_name @log_name = (log_name.to_s.downcase + '_raw_' + @log_type.to_s.downcase + '_' + self.object_id.to_s).freeze @log_directory = log_directory @cycle_size = ConfigParser.handle_nil(cycle_size) @cycle_size = Integer(@cycle_size) if @cycle_size @mutex = Mutex.new @file = nil @filename = nil @start_time = Time.now.sys @logging_enabled = ConfigParser.handle_true_false(logging_enabled) end |
Instance Attribute Details
#filename ⇒ String (readonly)
Returns The filename of the log.
32 33 34 |
# File 'lib/openc3/io/raw_logger.rb', line 32 def filename @filename end |
#logging_enabled ⇒ Boolean (readonly)
Returns Is logging enabled?.
38 39 40 |
# File 'lib/openc3/io/raw_logger.rb', line 38 def logging_enabled @logging_enabled end |
#orig_name ⇒ String (readonly)
Returns Original name passed to raw logger.
41 42 43 |
# File 'lib/openc3/io/raw_logger.rb', line 41 def orig_name @orig_name end |
#queue ⇒ Queue (readonly)
Returns Queue for asynchronous logging.
35 36 37 |
# File 'lib/openc3/io/raw_logger.rb', line 35 def queue @queue end |
Instance Method Details
#clone ⇒ Object
Create a clone of this object with a new name
128 129 130 131 132 |
# File 'lib/openc3/io/raw_logger.rb', line 128 def clone raw_logger = super() raw_logger.name = raw_logger.orig_name raw_logger end |
#name=(log_name) ⇒ Object
Set the raw logger name
81 82 83 84 |
# File 'lib/openc3/io/raw_logger.rb', line 81 def name=(log_name) @orig_name = log_name @log_name = (log_name.to_s.downcase + '_raw_' + @log_type.to_s.downcase + '_' + self.object_id.to_s).freeze end |
#start ⇒ Object
Starts a new log file by closing the existing log file. New log files are not created until data is written by #write so this does not immediately create a log file on the filesystem.
116 117 118 119 |
# File 'lib/openc3/io/raw_logger.rb', line 116 def start close_file() unless (Time.now.sys - @start_time) < 1.0 # Prevent close/open too fast @mutex.synchronize { @logging_enabled = true } end |
#stop ⇒ Object
Stops all logging and closes the current log file.
122 123 124 125 |
# File 'lib/openc3/io/raw_logger.rb', line 122 def stop @mutex.synchronize { @logging_enabled = false } close_file() end |
#write(data) ⇒ Object
Write data to the log file.
If no log file currently exists in the filesystem, a new file will be created.
Writing a log file is a critical operation so the entire method is wrapped with a rescue and handled with handle_critical_exception
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/openc3/io/raw_logger.rb', line 95 def write(data) if @logging_enabled return if !data or data.length <= 0 need_new_file = false @mutex.synchronize do if !@file or (@cycle_size and (@file.stat.size + data.length) > @cycle_size) need_new_file = true end end start_new_file() if need_new_file @mutex.synchronize { @file.write(data) if @file } end rescue => err Logger.instance.error "Error writing #{@filename} : #{err.formatted}" OpenC3.handle_critical_exception(err) end |