lib/kafo/puppet_log_parser.rb in kafo-2.0.0 vs lib/kafo/puppet_log_parser.rb in kafo-2.0.1

- old
+ new

@@ -3,10 +3,11 @@ def initialize @last_level = nil end def parse(line) + line = normalize_encoding(line) method, message = case when line =~ /^Error:(.*)/i || line =~ /^Err:(.*)/i [:error, $1] when line =~ /^Warning:(.*)/i || line =~ /^Notice:(.*)/i [:warn, $1] @@ -18,8 +19,18 @@ [@last_level.nil? ? :info : @last_level, line] end @last_level = method return [method, message.chomp] + end + + private + + def normalize_encoding(line) + if line.respond_to?(:encode) && line.respond_to?(:valid_encoding?) + line.valid_encoding? ? line : line.encode('UTF-16be', :invalid => :replace, :replace => '?').encode('UTF-8') + else # Ruby 1.8.7, doesn't worry about invalid encodings + line + end end end end