Sha256: a9c8e26b59a3f136111fb9fdaf8f8ff3d5331f2f40a4c737aa419e865ed7a1f7

Contents?: true

Size: 1.47 KB

Versions: 10

Compression:

Stored size: 1.47 KB

Contents

require 'socket'
require 'syslog_protocol'

module RemoteSyslog
  class MessageGenerator
    COLORED_REGEXP = /\e\[(?:(?:[0-9]{1,3});){0,2}(?:[0-9]{1,3})m/

    def initialize(socket, options = {})
      @socket = socket

      @parse_fields     = options[:parse_fields]
      @strip_color      = options[:strip_color]
      @exclude_pattern  = options[:exclude_pattern]
      @max_message_size = options[:max_message_size] || 1024

      @packet = SyslogProtocol::Packet.new

      if options[:hostname] && options[:hostname] != ''
        local_hostname = options[: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 = options[:facility] || 'user'
      @packet.severity = options[:severity] || 'notice'
    end

    def transmit(tag, message)
      return if @exclude_pattern && message =~ @exclude_pattern

      message = message.gsub(COLORED_REGEXP, '') if @strip_color

      packet = @packet.dup
      packet.content = message

      if @parse_fields && message =~ @parse_fields
        packet.hostname = $2 if $2 && $2 != ''
        packet.tag      = $3 if $3 && $3 != ''
        packet.content  = $4 if $4 && $4 != ''
      end

      unless packet.tag
        packet.tag = tag
      end

      @socket.write(packet.assemble(@max_message_size))
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
remote_syslog-1.6.9 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.8 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.7.2 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.7.1 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.7 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.7.pre1 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.6.1 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.6 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.6.rc2 lib/remote_syslog/message_generator.rb
remote_syslog-1.6.6.rc1 lib/remote_syslog/message_generator.rb