Class: RemoteSyslog::MessageGenerator
- Inherits:
-
Object
- Object
- RemoteSyslog::MessageGenerator
- Defined in:
- lib/remote_syslog/message_generator.rb
Constant Summary collapse
- COLORED_REGEXP =
/\e\[(?:(?:[0-9]{1,3});){0,2}(?:[0-9]{1,3})m/
Instance Method Summary collapse
-
#initialize(socket, options = {}) ⇒ MessageGenerator
constructor
A new instance of MessageGenerator.
- #transmit(tag, message) ⇒ Object
Constructor Details
#initialize(socket, options = {}) ⇒ MessageGenerator
Returns a new instance of MessageGenerator.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/remote_syslog/message_generator.rb', line 8 def initialize(socket, = {}) @socket = socket @parse_fields = [:parse_fields] @strip_color = [:strip_color] @exclude_pattern = [:exclude_pattern] @prepend = [:prepend] @max_message_size = [:max_message_size] || 1024 @packet = SyslogProtocol::Packet.new if [:hostname] && [:hostname] != '' local_hostname = [:hostname] else local_hostname = (Socket.gethostname rescue `hostname`.chomp)[/^([^\.]+)/, 1] if local_hostname.nil? || local_hostname == '' local_hostname = 'localhost' end end @packet.hostname = local_hostname @packet.facility = [:facility] || 'user' @packet.severity = [:severity] || 'notice' end |
Instance Method Details
#transmit(tag, message) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/remote_syslog/message_generator.rb', line 34 def transmit(tag, ) return if @exclude_pattern && =~ @exclude_pattern = .gsub(COLORED_REGEXP, '') if @strip_color = @prepend + if @prepend packet = @packet.dup packet.content = if @parse_fields && =~ @parse_fields packet.hostname = $2 if $2 && $2 != '' tag = $3 if $3 && $3 != '' packet.content = $4 if $4 && $4 != '' if tag packet.tag = tag.gsub(%r{[: \]\[\\]+}, '-') end end unless packet.tag packet.tag = tag end @socket.write(packet.assemble(@max_message_size)) end |