Class: Cosmos::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 ⇒ Object
readonly
Returns the value of attribute orig_name.
-
#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, logging_enabled = false, cycle_size = 2000000000, log_directory = nil) ⇒ 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, logging_enabled = false, cycle_size = 2000000000, log_directory = nil) ⇒ RawLogger
Returns a new instance of RawLogger.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cosmos/io/raw_logger.rb', line 52 def initialize( log_name, log_type, logging_enabled = false, cycle_size = 2000000000, log_directory = nil ) 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 @cycle_size = ConfigParser.handle_nil(cycle_size) @cycle_size = Integer(@cycle_size) if @cycle_size if ConfigParser.handle_nil(log_directory) @log_directory = log_directory else @log_directory = nil end @mutex = Mutex.new @file = nil @filename = nil @start_time = Time.now @logging_enabled = ConfigParser.handle_true_false(logging_enabled) end |
Instance Attribute Details
#filename ⇒ String (readonly)
Returns The filename of the log.
22 23 24 |
# File 'lib/cosmos/io/raw_logger.rb', line 22 def filename @filename end |
#logging_enabled ⇒ Boolean (readonly)
Returns Is logging enabled?.
28 29 30 |
# File 'lib/cosmos/io/raw_logger.rb', line 28 def logging_enabled @logging_enabled end |
#orig_name ⇒ Object (readonly)
Returns the value of attribute orig_name.
31 32 33 |
# File 'lib/cosmos/io/raw_logger.rb', line 31 def orig_name @orig_name end |
#queue ⇒ Queue (readonly)
Returns Queue for asynchronous logging.
25 26 27 |
# File 'lib/cosmos/io/raw_logger.rb', line 25 def queue @queue end |
Instance Method Details
#clone ⇒ Object
Create a clone of this object with a new name
125 126 127 128 129 |
# File 'lib/cosmos/io/raw_logger.rb', line 125 def clone raw_logger = super() raw_logger.name = raw_logger.orig_name raw_logger end |
#name=(log_name) ⇒ Object
Set the raw logger name
79 80 81 82 |
# File 'lib/cosmos/io/raw_logger.rb', line 79 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.
113 114 115 116 |
# File 'lib/cosmos/io/raw_logger.rb', line 113 def start close_file() unless ((Time.now - @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.
119 120 121 122 |
# File 'lib/cosmos/io/raw_logger.rb', line 119 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cosmos/io/raw_logger.rb', line 93 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}" Cosmos.handle_critical_exception(err) end |