#-- # ============================================================================= # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu) # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # * The names of its contributors may not be used to endorse or promote # products derived from this software without specific prior written # permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ============================================================================= #++ require 'copland/impl/include-exclude' module Copland module Implementation # This is the implementation of the service factory that provides # instances of logging interceptors. It includes the IncludeExcludeFactory # functionality. class LoggingInterceptorFactory include IncludeExcludeFactory # Returns a new LoggingInterceptor instance, initialized with the given # parameters. def create_instance( point, parms ) includes = build_map( parms[ "include" ] ) excludes = build_map( parms[ "exclude" ] ) return LoggingInterceptor.new( point, includes, excludes ) end end # A LoggingInterceptor is an interceptor object that logs method # invocations and exceptions. It includes the IncludeExclude functionality. class LoggingInterceptor include IncludeExclude # The log used by this interceptor to log messages. attr_reader :log # The array of patterns of methods to log. attr_reader :includes # The array of patterns of methods to _not_ log. attr_reader :excludes # Create a new LoggingInterceptor for the given service point, with the # given list of include and exclude patterns. The logger object will be # created as well, named with the service point's full name. def initialize( point, includes, excludes ) @log = point.owner.registry.logs.get( point.full_name ) @includes = includes @excludes = excludes end # Processes a method invocation context. If the current log has debugging # activated, and the requested method is not excluded by the # interceptor's exclude and include patterns, then a message will be # written for the method's invocation, its return code, and any exception # that is thrown. def process( chain, context ) if @log.debug? && match( context ) args = context.args.map { |i| i.inspect } .join( ', ' ) @log.debug "#{context.sym}( #{args} )" begin result = chain.process_next( context ) rescue Exception => e @log.debug "#{context.sym} raised #{e.message.inspect} (#{e.class})" raise end @log.debug "#{context.sym} => #{result.inspect}" return result else return chain.process_next( context ) end end end end end