Class: Fluent::Plugin::TailInput::TailWatcher::FIFO

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/in_tail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_encoding, encoding) ⇒ FIFO

Returns a new instance of FIFO.



948
949
950
951
952
953
954
# File 'lib/fluent/plugin/in_tail.rb', line 948

def initialize(from_encoding, encoding)
  @from_encoding = from_encoding
  @encoding = encoding
  @need_enc = from_encoding != encoding
  @buffer = ''.force_encoding(from_encoding)
  @eol = "\n".encode(from_encoding).freeze
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



956
957
958
# File 'lib/fluent/plugin/in_tail.rb', line 956

def buffer
  @buffer
end

#encodingObject (readonly)

Returns the value of attribute encoding.



956
957
958
# File 'lib/fluent/plugin/in_tail.rb', line 956

def encoding
  @encoding
end

#from_encodingObject (readonly)

Returns the value of attribute from_encoding.



956
957
958
# File 'lib/fluent/plugin/in_tail.rb', line 956

def from_encoding
  @from_encoding
end

Instance Method Details

#<<(chunk) ⇒ Object



958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/fluent/plugin/in_tail.rb', line 958

def <<(chunk)
  # Although "chunk" is most likely transient besides String#force_encoding itself
  # won't affect the actual content of it, it is also probable that "chunk" is
  # a reused buffer and changing its encoding causes some problems on the caller side.
  #
  # Actually, the caller here is specific and "chunk" comes from IO#partial with
  # the second argument, which the function always returns as a return value.
  #
  # Feeding a string that has its encoding attribute set to any double-byte or
  # quad-byte encoding to IO#readpartial as the second arguments results in an
  # assertion failure on Ruby < 2.4.0 for unknown reasons.
  orig_encoding = chunk.encoding
  chunk.force_encoding(from_encoding)
  @buffer << chunk
  # Thus the encoding needs to be reverted back here
  chunk.force_encoding(orig_encoding)
end

#bytesizeObject



1000
1001
1002
# File 'lib/fluent/plugin/in_tail.rb', line 1000

def bytesize
  @buffer.bytesize
end

#convert(s) ⇒ Object



976
977
978
979
980
981
982
983
984
# File 'lib/fluent/plugin/in_tail.rb', line 976

def convert(s)
  if @need_enc
    s.encode!(@encoding, @from_encoding)
  else
    s
  end
rescue
  s.encode!(@encoding, @from_encoding, :invalid => :replace, :undef => :replace)
end

#read_lines(lines) ⇒ Object



986
987
988
989
990
991
992
993
994
995
996
997
998
# File 'lib/fluent/plugin/in_tail.rb', line 986

def read_lines(lines)
  idx = @buffer.index(@eol)

  until idx.nil?
    # Using freeze and slice is faster than slice!
    # See https://github.com/fluent/fluentd/pull/2527
    @buffer.freeze
    rbuf = @buffer.slice(0, idx + 1)
    @buffer = @buffer.slice(idx + 1, @buffer.size)
    idx = @buffer.index(@eol)
    lines << convert(rbuf)
  end
end