class BitGirder::Event::Logger::Engine

An Engine coordinates dispatching of logged events a dynamic set of listeners. All methods are safe to call from concurrent threads.

Public Instance Methods

add_listener( l ) click to toggle source

Adds the given listener to this engine if it is not already present. All subsequent calls to log_event will dispatch to l until a call is made to remove_listener

This method returns l to allow callers to chain listener creation/addition:

list = eng.add_listener( SomeListener.new( ... ) )
# File lib/bitgirder/event/logger.rb, line 114
def add_listener( l )

    guarded do
        if l.respond_to?( :event_logged ) 
            @listeners << l unless @listeners.include?( l )
        else
            raise "Not an event listener: #{l} (#{l.class})" 
        end
    end

    l
end
listener_count() click to toggle source

Gets the number of listeners managed by this engine

# File lib/bitgirder/event/logger.rb, line 68
def listener_count
    guarded { @listeners.size }
end
log_event( ev ) click to toggle source

Logs an event. After this method returns all listeners will have received the event.

# File lib/bitgirder/event/logger.rb, line 85
def log_event( ev )

    # Accumulate failures here so we can process them after releasing the
    # lock
    failures = []

    guarded do 
        @listeners.each do |l| 
            begin 
                l.event_logged( ev )
            rescue Exception => ex
                failures << [ l, ev, ex ]
            end
        end
    end

    failures.each { |arr| listener_failed( *arr ) }
end
remove_listener( l ) click to toggle source

Removes l from the dispatch set. It is not an error to call this method for an unregistered listener.

# File lib/bitgirder/event/logger.rb, line 130
def remove_listener( l )
    guarded { @listeners.delete( l ) }
end

Private Instance Methods

guarded( &blk ) click to toggle source

In case we later decide to allow @mut to be optional or of varying implementations (not all apps which log will necessarily be threaded) we’ll only need to modify this block to run code which should be threadsafe

# File lib/bitgirder/event/logger.rb, line 62
def guarded( &blk )
    @mut.synchronize( &blk )
end
listener_failed( l, ev, ex ) click to toggle source
# File lib/bitgirder/event/logger.rb, line 73
def listener_failed( l, ev, ex )
    
    if @error_handler.respond_to?( :listener_failed )
        @error_handler.listener_failed( l, ev, ex )
    else
        warn( ex, "Listener #{l} failed for event #{ev}" )
    end
end