Class: JobManager::TimestampedStream
- Inherits:
-
IO
- Object
- IO
- JobManager::TimestampedStream
- Defined in:
- lib/jobmanager/timestampedstream.rb
Overview
This class represents a stream whose only purpose is to forward messages on to a list of configured streams.
Instance Attribute Summary collapse
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
-
#close ⇒ Object
Description: Close all the configured streams.
-
#flush ⇒ Object
Description: Flush all the configured streams.
-
#initialize(stream, date_time_format = "%F_%T") ⇒ TimestampedStream
constructor
Description: This method creates an TeeStream instance initialized with the hash of streams passed in.
-
#write(str) ⇒ Object
Description: This method forwards the string message to all configured streams.
Constructor Details
#initialize(stream, date_time_format = "%F_%T") ⇒ TimestampedStream
Description:
This method creates an TeeStream instance initialized with the hash of streams passed in. When a message is written to this stream, it will be forwarded to all hash values passed in.
Parameters:
- streams
-
A hash table of stream name to stream.
23 24 25 26 27 28 |
# File 'lib/jobmanager/timestampedstream.rb', line 23 def initialize(stream, date_time_format = "%F_%T") @stream = stream @date_time_format = date_time_format @is_start_of_line = true end |
Instance Attribute Details
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
13 14 15 |
# File 'lib/jobmanager/timestampedstream.rb', line 13 def stream @stream end |
Instance Method Details
#close ⇒ Object
Description:
Close all the configured streams.
85 86 87 |
# File 'lib/jobmanager/timestampedstream.rb', line 85 def close @stream.close() end |
#flush ⇒ Object
Description:
Flush all the configured streams.
77 78 79 |
# File 'lib/jobmanager/timestampedstream.rb', line 77 def flush @stream.flush() end |
#write(str) ⇒ Object
Description:
This method forwards the string message to all configured streams. Note: This class is derived from IO, and thus only needs to implement the write method in order for all of the write-related methods (<<, print, puts, putc, etc) to work free of charge, as all of these methods call write.
Parameters:
- str
-
The string to be written.
Returns:
The length of the string.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jobmanager/timestampedstream.rb', line 43 def write(str) if (str.length() == 0) return 0 end str = str.dup() now = DateTime.now().strftime(@date_time_format) # if string ends with a new line, strip it off and save it. if (str.sub!(/\n\z/, "")) ends_with_new_line = true end # replace all of the new lines with a new line followed by the # current date/time. str.gsub!(/\n/, "\n#{now} ") if (@is_start_of_line) str = "#{now} #{str}" @is_start_of_line = false end if (ends_with_new_line) str += "\n" @is_start_of_line = true end @stream.write(str) end |