From a68e06fefc0d53cadbf09fde95f1d49bfb74ee06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sat, 6 Aug 2011 00:05:13 +0200 Subject: update dispatcher header --- lib/ayk/dispatcher.rb | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/lib/ayk/dispatcher.rb b/lib/ayk/dispatcher.rb index 2a3d619..1f313bc 100644 --- a/lib/ayk/dispatcher.rb +++ b/lib/ayk/dispatcher.rb @@ -1,15 +1,8 @@ #! /usr/bin/env ruby -# -*- coding: utf-8 -*- - -#---------------------------------------------------------------------------- -# -# File : dispatcher.rb +# -*- coding: UTF-8 -*- # -# Author : Jérémy Zurcher +# Copyright (c) 2009-2011 Jérémy Zurcher # -# Date : 16/10/07 -# -# License : # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including @@ -29,15 +22,12 @@ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # -# Purpose : messages management on a per channel basis with level thresholds, rotations -# #---------------------------------------------------------------------------- - - +# #---------------------------------------------------------------------------- # messages management on a per channel basis with level thresholds, rotations module Dispatcher - + # module Level OFF = 0 ON = 1 @@ -46,7 +36,7 @@ module Dispatcher DBG3 = 4 def format_sev( sev ) ['< OFF >','< ON >','< DBG1 >','< DBG2 >','< DBG3 >'][sev] || ' ' end end - + # # this class is in charge of dispatching an event to all it's registered sinks class Channel attr_reader :id, :sinks @@ -83,7 +73,7 @@ module Dispatcher ret end end - + # # this the base class for simple sinks # you should inherit from Sink and implement +handle+ method class Sink @@ -99,7 +89,7 @@ module Dispatcher @level = level end # pretty output - def to_s + def to_s ret = "#{self.class} #{@id}\n rotation : #{@rotation_scheme.nil? ? 'never' : @rotation_scheme.to_s + " next on #{@next_rotation}" }\n channels :" @channels.each{ |c| ret +="\n\t#{c} " } ret @@ -107,7 +97,7 @@ module Dispatcher # TO BE IMPLEMENTED, should handle data def handle( data, level ); raise NotImplementedError.new end end - + # # this the base class for sinks with time based rotation capabilities # you should inherit from SinkRotate and implement +handle+ and +rotate+ methods class SinkRotate < Sink @@ -154,7 +144,6 @@ module Dispatcher # TO BE IMPLEMENTED, should handle rotation time def rotate( t ); raise NotImplementedError.new end end - # class Dispatcher # @@ -182,8 +171,6 @@ module Dispatcher return @sinks.find{ |s| s.id==id } if what==:sink nil end - # return sink +s_id+ if exists - # pretty output def to_s ret = "####{self.class}" @@ -218,9 +205,9 @@ module Dispatcher @sinks.delete sink end end - + # # TEST classes - + # # a sink which call a hook when activated class CallbackSink < Sink def initialize( id, channels, level=Level::ON, &blk ) @@ -230,7 +217,7 @@ module Dispatcher # output this event and flush the io def handle( data, level ); @blk.call( data, level ) end end - + # # an IO based sink for STDOUT and STDERR class IOSink < Sink # +id+ is the channel id @@ -244,7 +231,7 @@ module Dispatcher # output this event and flush the io def handle( data, level ); @io << (@blk.nil? ? data.to_s : @blk.call(data,level) ); @io.flush end end - + # # simple file sink class FileSink < Sink # +id+ is the channel id @@ -259,7 +246,7 @@ module Dispatcher # output this event and flush the fname def handle( data, level ) open( @fname, 'a') { |f| f << ( @blk.nil? ? data.to_s : @blk.call(data,level) ) } end end - + # # a file path based sink with rotation implemented class FileSinkRotate < SinkRotate # +id+ is the channel id @@ -279,4 +266,5 @@ module Dispatcher def handle( data, level ) open( @fname, 'a') { |f| f << ( @blk.nil? ? data.to_s : @blk.call(data,level) ) } end end end - +# +# EOF -- cgit v1.1-2-g2b99