lib/sawmill/parser.rb in sawmill-0.0.3 vs lib/sawmill/parser.rb in sawmill-0.0.4
- old
+ new
@@ -76,19 +76,25 @@
end
@levels = opts_[:levels] || STANDARD_LEVELS
@emit_incomplete_records_at_eof = opts_[:emit_incomplete_records_at_eof]
@current_record_id = nil
@parser_directives = {}
+ @encoding = opts_[:encoding]
+ @internal_encoding = opts_[:internal_encoding]
+ if defined?(::Encoding)
+ @encoding = ::Encoding.find(@encoding) if @encoding && !@encoding.kind_of?(::Encoding)
+ @internal_encoding = ::Encoding.find(@internal_encoding) if @internal_encoding && !@internal_encoding.kind_of?(::Encoding)
+ end
end
# Parse one log entry from the stream and emit it to the processor.
# Also returns the log entry.
# Returns nil if EOF has been reached.
def parse_one_entry
- str_ = @io.gets
+ str_ = _get_next_line
entry_ = nil
if str_
match_ = LINE_REGEXP.match(str_)
if match_
level_ = @levels.get(match_[1])
@@ -111,11 +117,11 @@
str_ = match_[16]
if str_ =~ /(\\+)$/
count_ = $1.length
str_ = $` + "\\"*(count_/2)
while count_ % 2 == 1
- str2_ = @io.gets
+ str2_ = _get_next_line
if str2_ && str2_ =~ /(\\*)\n?$/
count_ = $1.length
str_ << "\n" << $` << "\\"*(count_/2)
else
break
@@ -169,9 +175,22 @@
# Parse the rest of the stream until EOF is reached, and emit the log
# entries to the processor.
def parse_all
while parse_one_entry; end
+ end
+
+
+ private
+
+
+ def _get_next_line # :nodoc:
+ str_ = @io.gets
+ if str_
+ str_.force_encoding(@encoding) if @encoding
+ str_.encode!(@internal_encoding) if @internal_encoding
+ end
+ str_
end
end