# File lib/lumberjack/device/writer.rb, line 100
      def flush
        lines = nil
        @lock.synchronize do
          before_flush
          lines = @buffer.pop!
        end
        
        unless lines.empty?
          out = "#{lines.join(Lumberjack::LINE_SEPARATOR)}#{Lumberjack::LINE_SEPARATOR}"
          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
              # to lock the thread on every stream write call.
              @lock.synchronize do
                if stream.closed?
                  raise e
                else
                  stream.write(out)
                end
              end
            end
            stream.flush rescue nil
          rescue => e
            $stderr.write("#{e.class.name}: #{e.message}#{' at ' + e.backtrace.first if e.backtrace}")
            $stderr.write(out)
            $stderr.flush
          end
        end
      end