class Lumberjack::Device::Writer

def write_to_stream(lines)

def write_to_stream(lines)
  return if lines.empty?
  lines = lines.first if lines.is_a?(Array) && lines.size == 1
  out = nil
  out = if lines.is_a?(Array)
    "#{lines.join(Lumberjack::LINE_SEPARATOR)}#{Lumberjack::LINE_SEPARATOR}"
  else
    "#{lines}#{Lumberjack::LINE_SEPARATOR}"
  end
  begin
    begin
      stream.write(out)
    rescue IOError => e
      # This condition can happen if another thread closed the stream in the `before_flush` call.
      # Synchronizing will handle the race condition, but since it's an exceptional case we don't
      # want to lock the thread on every stream write call.
      @lock.synchronize do
        if stream.closed?
          raise e
        else
          stream.write(out)
        end
      end
    end
    begin
      stream.flush
    rescue
      nil
    end
  rescue => e
    $stderr.write("#{e.class.name}: #{e.message}#{" at " + e.backtrace.first if e.backtrace}")
    $stderr.write(out)
    $stderr.flush
  end
end