=begin
 ******************************************************************************
 * Copyright 1990-2009 MBARI
 * MBARI Proprietary Information. All rights reserved.
 ******************************************************************************
 * Summary  : A Rover-specific version of the thread class, more functionality
 * Filename : rover_thread.rb
 * Author   : Chase/Henthorn
 * Project  : Benthic Rover
 * Version  : 1A
 * Created  : Oct 2008
 * Modified :
 ******************************************************************************
=end

##
# Refer to Rover-code home directory as base
#
require "#{ENV['ROVER_HOME']}/utils/rover_environment"
#

require "utils/misc"
require "utils/datalog"

##########
# This class should be used rather than the standard Thread class so
# that when an Exception is thrown within a thread it will be caught and
# logged before being propagated onward.
#
class RoverThread < Thread
   include SyslogWriter

   def initialize (*args, &block)
     super {
       begin
         yield(*args)

       rescue Exception => e
         syslog("! exception: ", e)
         backtrace = e.backtrace.join(",")
         syslog("! backtrace: ", backtrace)
         raise e
       end
     }
   end
   
end

##
# Standalone unit test (recommended)
#
if __FILE__ == $0
  # Test code here
  outer_thr = RoverThread.new do
    inner_thr = RoverThread.new do
      bogus.do_this
    end
    # 
    inner_thr.join
  end
  outer_thr.join
end
