Class: SyslogProtocol::Packet
- Inherits:
-
Object
- Object
- SyslogProtocol::Packet
- Defined in:
- lib/syslog_protocol_ms/packet.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#facility ⇒ Object
Returns the value of attribute facility.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#severity ⇒ Object
Returns the value of attribute severity.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#time ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
- #assemble(max_size = 1024) ⇒ Object
- #facility_name ⇒ Object
- #generate_timestamp ⇒ Object
- #pri ⇒ Object
- #pri=(p) ⇒ Object
- #severity_name ⇒ Object
- #string_bytesize(string) ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
4 5 6 |
# File 'lib/syslog_protocol_ms/packet.rb', line 4 def content @content end |
#facility ⇒ Object
Returns the value of attribute facility.
3 4 5 |
# File 'lib/syslog_protocol_ms/packet.rb', line 3 def facility @facility end |
#hostname ⇒ Object
Returns the value of attribute hostname.
3 4 5 |
# File 'lib/syslog_protocol_ms/packet.rb', line 3 def hostname @hostname end |
#severity ⇒ Object
Returns the value of attribute severity.
3 4 5 |
# File 'lib/syslog_protocol_ms/packet.rb', line 3 def severity @severity end |
#tag ⇒ Object
Returns the value of attribute tag.
3 4 5 |
# File 'lib/syslog_protocol_ms/packet.rb', line 3 def tag @tag end |
#time ⇒ Object
Returns the value of attribute time.
4 5 6 |
# File 'lib/syslog_protocol_ms/packet.rb', line 4 def time @time end |
Instance Method Details
#assemble(max_size = 1024) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/syslog_protocol_ms/packet.rb', line 10 def assemble(max_size = 1024) unless @hostname and @facility and @severity and @tag raise "Could not assemble packet without hostname, tag, facility, and severity" end data = "<#{pri}>#{} #{@hostname} #{@tag}: #{@content}" if string_bytesize(data) > max_size data = data.slice(0, max_size) while string_bytesize(data) > max_size data = data.slice(0, data.length - 1) end end data end |
#facility_name ⇒ Object
92 93 94 |
# File 'lib/syslog_protocol_ms/packet.rb', line 92 def facility_name FACILITY_INDEX[@facility] end |
#generate_timestamp ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/syslog_protocol_ms/packet.rb', line 112 def time = @time || Time.now # The timestamp format requires that a day with fewer than 2 digits have # what would normally be a preceding zero, be instead an extra space. day = time.strftime("%d") day = day.sub(/^0/, ' ') if day =~ /^0\d/ time.strftime("%b #{day} %H:%M:%S.%3N") end |
#pri ⇒ Object
100 101 102 |
# File 'lib/syslog_protocol_ms/packet.rb', line 100 def pri (@facility * 8) + @severity end |
#pri=(p) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/syslog_protocol_ms/packet.rb', line 104 def pri=(p) unless p.is_a? Integer and (0..191).include?(p) raise ArgumentError.new "PRI must be a number between 0 and 191" end @facility = p / 8 @severity = p - (@facility * 8) end |
#severity_name ⇒ Object
96 97 98 |
# File 'lib/syslog_protocol_ms/packet.rb', line 96 def severity_name SEVERITY_INDEX[@severity] end |
#string_bytesize(string) ⇒ Object
122 123 124 |
# File 'lib/syslog_protocol_ms/packet.rb', line 122 def string_bytesize(string) string.bytesize end |
#to_s ⇒ Object
6 7 8 |
# File 'lib/syslog_protocol_ms/packet.rb', line 6 def to_s assemble end |