Sha256: 42c8e06d14a23443a90bfbab5691f842f5c43fb408bb98644a8466c74b31b49e

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

module FlashPlayer

  class LogFile

    attr_accessor :logger

    def initialize
      @config      = MMConfig.new
      @logger      = $stdout
    end

    def tail thread
      @config.create
      tail_path flashlog_path, thread
    end

    private

    def tail_path path, thread=nil
      logger.puts ">> Tailing '#{path}', press CTRL+C to quit"
      create_flashlog_at path
      clear_flashlog_at path
      read_flashlog_at path, thread
    end

    def read_flashlog_at path, thread=nil
      thread ||= Thread.new{}
      lines_put = 0

      trap("INT") { thread.kill }
      
      while thread.alive? do
        File.open(path, 'r') do |file|
          lines_read = 0
          file.readlines.each do |line|
            if(lines_read >= lines_put)
              logger.puts "[trace] #{line}"
              logger.flush
              lines_put += 1
            end
            lines_read += 1
          end
        end
        logger.flush
        sleep(0.2)
      end

      logger.puts ""
      logger.puts ">> Exiting from tailing '#{path}' at user request"
    end

    def flashlog_path
      File.join(FlashPlayer.home, 'Logs', 'flashlog.txt')
    end

    def clear_flashlog_at path
      File.open(path, 'w') do |f|
        f.write('')
      end
    end

    def create_flashlog_at path
      if(!File.exists?(path))
        FileUtils.makedirs(File.dirname(path))
        FileUtils.touch(path)
      end
    end

  end
end

desc "Tail the flashlog.txt and block"
def flashlog args
  task args do
    reader = FlashPlayer::LogFile.new
    reader.tail
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flashplayer-10.1.3.pre lib/flashplayer/log_file.rb
flashplayer-10.1.2.pre lib/flashplayer/log_file.rb
flashplayer-10.1.1.pre lib/flashplayer/log_file.rb