Sha256: 5b724cb44d96ebfc416a1a5beb82c8c920840d0ad4ae53bbe72be6f8c56556ab

Contents?: true

Size: 850 Bytes

Versions: 2

Compression:

Stored size: 850 Bytes

Contents

module Zeusd
  module Log
    class Tailer
      attr_reader :file, :lines

      def initialize(file)
        @file  = file
        @lines = []
      end

      def on_update(&block)
        @on_update = block if block_given?
        @on_update
      end

      def following?
        !!@thread
      end

      def restart!
        stop!.start!
      end

      def start!
        @thread = Thread.new do
          File.open(file) do |log|
            log.extend(File::Tail)
            log.interval = 0.1
            log.backward(0)
            log.tail do |line|
              @lines << line
              on_update.call(line) if on_update
            end
          end
        end
        self
      end

      def stop!
        if @thread
          @thread.terminate
          @thread = nil
        end
        self
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zeusd-0.3.1 lib/zeusd/log/tailer.rb
zeusd-0.3.0 lib/zeusd/log/tailer.rb