Sha256: faf59ead98a2292a89234ab99d5a804837069a97198219b7a06fe101c6dffc6c

Contents?: true

Size: 1.82 KB

Versions: 16

Compression:

Stored size: 1.82 KB

Contents

# Archive of log items, which can be messages or info items.
# All items are timestamped, and stored chronologically.

module RSMP
  class Archive
    include Inspect

    attr_reader :items

    @@index = 0

    def initialize  max=100
      @items = []
      @max = max
    end

    def inspect
      "#<#{self.class.name}:#{self.object_id}, #{inspector(:@items)}>"
    end

    def self.prepare_item item
      raise ArgumentError unless item.is_a? Hash
    
      cleaned = item.select { |k,v| [:author,:level,:ip,:port,:site_id,:component,:text,:message,:exception].include? k }
      cleaned[:timestamp] = Clock.now
      if item[:message]
        cleaned[:direction] = item[:message].direction 
        cleaned[:component] = item[:message].attributes['cId']
      end

      cleaned
    end

    def self.increase_index
      @@index += 1
    end

    def self.current_index
      @@index
    end

    def by_level levels
      items.select { |item| levels.include? item[:level] }
    end

    def strings
      items.map { |item| item[:str] }
    end

    def add item
      item[:index] = RSMP::Archive.increase_index
      @items << item
      if @items.size > @max
        @items.shift
      end
    end
    
    private

    def find options, &block
      # search backwards from newest to older, stopping once messages
      # are older that options[:earliest]
      out = []
      @items.reverse_each do |item|
        break if options[:earliest] && item[:timestamp] < options[:earliest]
        next if options[:level] && item[:level] != options[:level]
        next if options[:type] && (item[:message] == nil || (item[:message].type != options[:type]))
        next if options[:with_message] && !(item[:direction] && item[:message])
        next if block_given? && block.call != true
        out.unshift item
      end
      out
    end
  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rsmp-0.8.6 lib/rsmp/archive.rb
rsmp-0.8.5 lib/rsmp/archive.rb
rsmp-0.8.4 lib/rsmp/archive.rb
rsmp-0.8.3 lib/rsmp/archive.rb
rsmp-0.8.2 lib/rsmp/archive.rb
rsmp-0.8.1 lib/rsmp/archive.rb
rsmp-0.8.0 lib/rsmp/archive.rb
rsmp-0.7.5 lib/rsmp/archive.rb
rsmp-0.7.4 lib/rsmp/archive.rb
rsmp-0.7.3 lib/rsmp/archive.rb
rsmp-0.7.2 lib/rsmp/archive.rb
rsmp-0.7.1 lib/rsmp/archive.rb
rsmp-0.7.0 lib/rsmp/archive.rb
rsmp-0.6.5 lib/rsmp/archive.rb
rsmp-0.6.4 lib/rsmp/archive.rb
rsmp-0.6.3 lib/rsmp/archive.rb