Sha256: c45011db79fbbbb3424abbfe061d48d04b518983f541032cb81be36210be1fe6

Contents?: true

Size: 1.34 KB

Versions: 17

Compression:

Stored size: 1.34 KB

Contents

module UPM
  class LogParser

    def initialize(klass, log_glob)
      @klass = klass
      @log_glob = log_glob
    end

    def log_events
      return to_enum(:log_events) unless block_given?

      yielder = proc do |io|
        io.each_line do |line|
          if e = @klass.from_line(line.strip)
            yield e
          end
        end
      end

      logs = Dir[@log_glob].sort_by { |path| File.mtime(path) } 

      logs.each do |log|
        if log =~ /\.gz$/
          IO.popen(["zcat", log], &yielder)
        else
          open(log, &yielder)
        end 
      end
    end

    def display
      lesspipe(tail: true) do |less|
        groups = log_events.split_between { |a,b| (b.date.to_i - a.date.to_i) > 60 }

        groups.each do |group|
          first, last = group.first.date, group.last.date
          elapsed     = (last.to_i - first.to_i) / 60

          empty_group = true

          group.each do |ev|
            # Print the header only if the query matched something in this group
            if empty_group
              empty_group = false
              less.puts
              less.puts "<8>== <11>#{first.strftime("<10>%Y-%m-%d <7>at <2>%l:%M %p")} <7>(<9>#{elapsed} <7>minute session) <8>========".colorize
            end

            less.puts ev
          end
        end
      end # lesspipe
    end

  end # LogParser
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
upm-0.1.18 lib/upm/log_parser.rb
upm-0.1.17 lib/upm/log_parser.rb
upm-0.1.16 lib/upm/log_parser.rb
upm-0.1.15 lib/upm/log_parser.rb
upm-0.1.14 lib/upm/log_parser.rb
upm-0.1.13 lib/upm/log_parser.rb
upm-0.1.12 lib/upm/log_parser.rb
upm-0.1.11 lib/upm/log_parser.rb
upm-0.1.10 lib/upm/log_parser.rb
upm-0.1.9 lib/upm/log_parser.rb
upm-0.1.8 lib/upm/log_parser.rb
upm-0.1.7 lib/upm/log_parser.rb
upm-0.1.6 lib/upm/log_parser.rb
upm-0.1.5 lib/upm/log_parser.rb
upm-0.1.3 lib/upm/log_parser.rb
upm-0.1.2 lib/upm/log_parser.rb
upm-0.1.0 lib/upm/log_parser.rb