Class: RemoteSyslog::Reader
- Inherits:
-
EventMachine::FileTail
- Object
- EventMachine::FileTail
- RemoteSyslog::Reader
- Defined in:
- lib/remote_syslog/reader.rb
Constant Summary collapse
- COLORED_REGEXP =
/\e\[(?:(?:[34][0-7]|[0-9]);){0,2}(?:[34][0-7]|[0-9])m/
Instance Method Summary collapse
-
#initialize(path, destination_address, destination_port, options = {}) ⇒ Reader
constructor
A new instance of Reader.
- #receive_data(data) ⇒ Object
- #transmit(message) ⇒ Object
Constructor Details
#initialize(path, destination_address, destination_port, options = {}) ⇒ Reader
Returns a new instance of Reader.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/remote_syslog/reader.rb', line 11 def initialize(path, destination_address, destination_port, = {}) super(path, -1) @parse_fields = [:parse_fields] @strip_color = [:strip_color] @socket = [:socket] || UdpEndpoint.new(destination_address, destination_port) @buffer = BufferedTokenizer.new @packet = SyslogProtocol::Packet.new local_hostname = [:hostname] || (Socket.gethostname rescue `hostname`.chomp) if local_hostname.nil? || local_hostname.empty? local_hostname = 'localhost' end @packet.hostname = local_hostname @packet.facility = [:facility] || 'user' @packet.severity = [:severity] || 'notice' tag = [:program] || File.basename(path) || File.basename($0) # Make sure the tag isn't too long if tag.length > 32 tag = tag[0..31] end @packet.tag = tag end |
Instance Method Details
#receive_data(data) ⇒ Object
41 42 43 44 45 |
# File 'lib/remote_syslog/reader.rb', line 41 def receive_data(data) @buffer.extract(data).each do |line| transmit(line) end end |
#transmit(message) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/remote_syslog/reader.rb', line 47 def transmit() = .gsub(COLORED_REGEXP, '') if @strip_color packet = @packet.dup packet.content = if @parse_fields if =~ @parse_fields packet.hostname = $2 if $2 && $2 != '' packet.tag = $3 if $3 && $2 != '' packet.content = $4 if $4 && $4 != '' end end @socket.write(packet.assemble) end |