Sha256: 0d524d7c963b03a1db0cdb09724be84d855c28f6addff7cd86c071766b5bb665

Contents?: true

Size: 846 Bytes

Versions: 12

Compression:

Stored size: 846 Bytes

Contents

require 'singleton'
require 'thread'

module Xi
  class ErrorLog
    include Singleton

    attr_accessor :max_msgs
    attr_reader   :more_errors
    alias_method  :more_errors?, :more_errors

    def initialize(max_msgs: 6)
      @max_msgs = max_msgs

      @mutex = Mutex.new
      @errors = []
      @more_errors = false
    end

    def <<(msg)
      @mutex.synchronize do
        @errors.unshift(msg) unless @errors.include?(msg)
        if @errors.size >= @max_msgs
          @errors.slice!(@max_msgs)
          @more_errors = true
        end
      end
    end

    def each
      return enum_for(:each) unless block_given?

      msgs = @mutex.synchronize do
        res = @errors.dup
        @errors.clear
        @more_errors = false
        res
      end

      while !msgs.empty?
        yield msgs.shift
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
xi-lang-0.2.5 lib/xi/error_log.rb
xi-lang-0.2.4 lib/xi/error_log.rb
xi-lang-0.2.3 lib/xi/error_log.rb
xi-lang-0.2.2 lib/xi/error_log.rb
xi-lang-0.2.1 lib/xi/error_log.rb
xi-lang-0.2.0 lib/xi/error_log.rb
xi-lang-0.1.6 lib/xi/error_log.rb
xi-lang-0.1.5 lib/xi/error_log.rb
xi-lang-0.1.4 lib/xi/error_log.rb
xi-lang-0.1.3 lib/xi/error_log.rb
xi-lang-0.1.2 lib/xi/error_log.rb
xi-lang-0.1.0 lib/xi/error_log.rb