
#parse args before gtk will do it:
require "getoptlong"
class LogViewMain

  attr :drb
  attr :uri

  def initialize(drb=true)
    @drb=drb
    @uri=nil
  end

  def startDRb()
    require "drb"
    #show view
    view = GUI.new
    #start remote server
    DRb.start_service("druby://:#{@uri}", view)
    Gtk.main
    DRb.stop_service
  rescue Exception => ex
    puts ex.message
    exit
  end

  def startROMP()
    require "romp"
    #show view
    view = GUI.new
    @server = ROMP::Server.new(@uri)
    @server.bind(view, "rlogview")
    Gtk.main
  end

  def parse
    opts = GetoptLong.new(
      [ "--drb",   "-d",  GetoptLong::REQUIRED_ARGUMENT ],
      [ "--romp",  "-r",  GetoptLong::REQUIRED_ARGUMENT ],
      [ "--help",  "-h",  GetoptLong::NO_ARGUMENT ]
    )
    opts.each { |opt, arg|
      if (opt=="--drb")
        @drb = true
        @uri = arg
      elsif (opt=="--romp")
        @drb = false
        @uri = arg
      elsif (opt=="--help")
        usage
      end
    }
    if (@uri.nil?)
      puts "\nNo transport medium given!!\n"
      usage() 
    end
    
  end

  def usage
    puts "\nrlogview\n"
    puts "          --drb  <port>  use distributed ruby with given port\n"
    puts "          --romp <uri>   use ROMP with given uri\n"
    puts "          --help  this help message\n\n"
    exit
  end

  def start
    if (@drb)
      startDRb()
    else
      startROMP()
    end
  end
end
main = LogViewMain.new
main.parse()

#require "gnome"
require "gtk"
require "gdk_imlib"
require "lglade"
require "thread"
require "log4r/logevent"


class GUI
  #loglines scroll to end?
  attr :autoscroll
  attr :loglevels
  #all widget stuff
  attr :glade
  attr :level
  attr :levelcombo
  attr :category
  attr :message
  attr :events
  attr :details
  attr :about
  attr :grey
  #all events
  attr :allevents
  #synchronization variables
  attr :access
  #filter regexps
  attr :levelfilter
  attr :categoryfilter
  attr :messagefilter
  attr :modulo2

  def initialize()
    @glade = GladeXML.new(GladeFile) { |handler| 
      method(handler)
    }
    @level = @glade.getWidget("level")
    @levelcombo = @glade.getWidget("levelcombo")
    @category = @glade.getWidget("category")
    @message = @glade.getWidget("message")
    @events = @glade.getWidget("events")
    @details = @glade.getWidget("details")
    @about = @glade.getWidget("about")
    @grey = Gdk::Color.new(60000, 60000, 60000)
    @allevents = Array.new
    @access = Mutex.new
    @loglevels = ["ALL", "OFF"]
    @levelfilter = 0
    @categoryfilter = nil
    @messagefilter = nil
    @modulo2 = 1
    @autoscroll = false
  end

  def setup_loglevel(level)
    @loglevels = level
    @levelcombo.set_popdown_strings(level)
  end

  def add_entry(event)
    @access.synchronize {
      @allevents.push(event)
      show_event(event) if (visible?(event))
    }
  end

  def add_entries(events)
    @access.synchronize {
      @events.freeze
      events.each { |event|
        @allevents.push(event)
        show_event(event) if (visible?(event))
      }
      @events.get_vadjustment.value= @events.get_vadjustment.upper if @autoscroll
      @events.thaw
    }
  end

  def update_list()
    @access.synchronize {
      @modulo2=1
      @events.freeze
      @events.clear
      @allevents.each { |event|
        show_event(event) if (visible?(event))
      }
      @events.thaw
    }
  end

  def visible?(event)
    result = false
    if (event.level >= @levelfilter)
      if (@categoryfilter.nil? or @categoryfilter.match(event.name))
        if (@messagefilter.nil? or @messagefilter.match(event.data))
          result = true
        end
      end
    end
    return result
  end

  def show_event(event)
    @modulo2 = (@modulo2+1)%2
    node = @events.insert_node(nil, nil, [@loglevels[event.level], event.name, event.data], 5, nil, nil , nil , nil , true, true)
    @events.node_set_row_data(node, event)
    if (@modulo2==1)
      @events.node_set_background(node, @grey)
    end
  end

  def show_event_detail(event)
    result = ""
    result += "Full Name     #{event.fullname}\n" if event.fullname
    result += "Name            #{event.name}\n" if event.name
    result += "Message       #{event.data}" if event.data
    result += "\nTrace\n"+event.tracer.join("\n") if event.tracer
    @details.delete_text(0, @details.get_length)
    @details.insert_text(result, 0)
  end

  def on_level_changed(*params)
    @levelfilter = @loglevels.index(@level.get_text())
    update_list()
  end

  def on_category_activate(*params)
    filter = @category.get_text()
    if (filter.length>0)
      @categoryfilter = Regexp.new(filter)
    else
      @categoryfilter = nil
    end
    update_list()
  rescue Exception => ex
    puts ex
  end

  def on_message_activate(*params)
    filter = @message.get_text()
    if (filter.length>0)
      @messagefilter = Regexp.new(filter)
    else
      @messagefilter = nil
    end
    update_list()
  rescue Exception => ex
    puts ex
  end

  def on_select_event(tree, node, column, user)
    event = @events.node_get_row_data(node)
    show_event_detail(event)
  end

  def on_quit_activate(*params)
    Gtk.main_quit
  end

  def on_autoscroll_toggled(button, user)
    @autoscroll = button.active?
  end

  def on_clear_clicked(*params)
    @access.synchronize {
      @events.clear
      @allevents.clear
    }
  end

  def on_about_activate(*params)
    @about.show
  end

  def on_aboutok_clicked(*params)
    @about.hide
  end

  def on_window_destroy(*params)
    Gtk.main_quit
  end

end

main.start


# rlogview.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.

