require "drb"

module Log4r
  class DRBViewOutputter < Outputter
    attr :gui
    attr :uri
    attr :buffsize
    attr :buff
    
    
    # ctor - construct a new outputter.
    # [name] Name of this outputter.
    # [hash] holds all needed values. Possible hash values:
    #     *uri - the uri, the gui is listen to.
    #     *buffsize - how many pending logevents until flush (default=1).
    def initialize(name, hash)
      @uri = (hash[:uri] or hash["uri"])
      @buffsize = (hash[:buffsize] or hash['buffsize'] or 1).to_i
      @buff = []
      DRb.start_service
      @gui = DRbObject.new(nil, @uri)
      #gui must be initialized before super is called...
      super(name, hash)
    end

    # Flush any left buffer.
    def flush
      if (@gui)
        synch { send_to_gui }
      end
    end

    protected #===============================================================
    
    # Trigger to be notified about change of loglevel entries.
    def validate_hash(hash)
      super(hash)
      # which loglevel exist?
      @gui.setup_loglevel(Log4r::LNAMES)
    end
    
    private #=================================================================

    # Log event.
    def canonical_log(logevent)
      if (@gui)
        synch {
          @buff.push(logevent)
          send_to_gui() if (@buff.size >= @buffsize)
        }
      end
    end

    # Send to gui.
    def send_to_gui()
      begin
        @gui.add_entries(@buff) if (@buff.size>0)
      rescue
        #not reachable - try later
      end
      @buff.clear
    end
  end
end

# drbviewoutputter.rb   Juli 2002
#
# Copyright (c) Matthias Veit <matthias_veit@yahoo.de>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option) 
# any later version.
#  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
# GNU General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
# 02111-1307, USA.

