Class: Cosmos::MessageLog
Overview
Handles writing message logs to a file
Instance Attribute Summary collapse
-
#filename ⇒ String
readonly
The name of the message log file.
Instance Method Summary collapse
-
#initialize(tool_name, log_dir = nil) ⇒ MessageLog
constructor
A new instance of MessageLog.
-
#start ⇒ Object
Creates a new message log and sets the filename.
-
#stop ⇒ Object
Closes the message log and marks it read only.
-
#write(message) ⇒ Object
Ensures the log file is opened and ready to write.
Constructor Details
#initialize(tool_name, log_dir = nil) ⇒ MessageLog
Returns a new instance of MessageLog.
28 29 30 31 32 33 34 35 |
# File 'lib/cosmos/utilities/message_log.rb', line 28 def initialize(tool_name, log_dir = nil) @tool_name = tool_name @log_dir = ConfigParser.handle_nil(log_dir) @log_dir = System.paths['LOGS'] unless @log_dir @filename = '' @file = nil @start_time = nil end |
Instance Attribute Details
#filename ⇒ String (readonly)
Returns The name of the message log file. Empty string until the write or start methods are called at which point it is set to the filename. Retains the last filename even after stop is called.
23 24 25 |
# File 'lib/cosmos/utilities/message_log.rb', line 23 def filename @filename end |
Instance Method Details
#start ⇒ Object
Creates a new message log and sets the filename
62 63 64 65 66 67 68 69 70 |
# File 'lib/cosmos/utilities/message_log.rb', line 62 def start # Prevent starting files too fast sleep(0.1) until !File.exist?(File.join(@log_dir, File.([@tool_name, 'messages']))) stop() Cosmos.set_working_dir do @filename = File.join(@log_dir, File.([@tool_name, 'messages'])) @file = File.open(@filename, 'a') end end |
#stop ⇒ Object
Closes the message log and marks it read only
52 53 54 55 56 57 58 59 |
# File 'lib/cosmos/utilities/message_log.rb', line 52 def stop if @file and not @file.closed? @file.close Cosmos.set_working_dir do File.chmod(0444, @filename) end end end |
#write(message) ⇒ Object
Ensures the log file is opened and ready to write. It then writes the message to the log and flushes it to force the write.
41 42 43 44 45 46 47 48 49 |
# File 'lib/cosmos/utilities/message_log.rb', line 41 def write() if @file.nil? or @file.closed? or (not File.exist?(@filename)) start() end @file.write() # While it's nice to flush the IO this is an extreme slowdown #@file.flush end |