module ZDebug

Subversion information $Id: zdebug.rb 337 2011-10-14 16:11:39Z nelsonab $ $Revision: 337 $

Public Instance Methods

debug(level,args={}) click to toggle source

level - level to show message (Integer) :var - variable to show (Object) :msg - message to be prepended before variable (String) :truncate - truncate var if it is over N characters :overload - do not show or error if debug_level is not set :facility - Which debug facility level should also be used :stack_pos - Stack position to use for calling function information (0==last function) :trace_depth - Show a backtrace of :trace_depth starting at :stack_pos

# File zbxapi/zdebug.rb, line 58
def debug(level,args={})
  variable=args[:var] || :zzempty
  message=args[:msg] || nil
  facility=args[:facility] || nil
  raise "Facility must be a symbol" if facility && facility.class!=Symbol
  truncate=args[:truncate] || 0
  raise "Truncate must be an Integer" if truncate.class!=Integer
  overload=(!args[:overload].nil? && args[:overload]==true) || false
  stack_pos=args[:stack_pos] || 0
  raise ":stack_pos must be an Integer" if stack_pos.class!=Integer
  backtrace_depth=args[:trace_depth] || 0

  return if overload
  raise "Call set_debug before using debug" if !defined?(@@debug_level)

  if facility
    facility_level=@@facility_level[facility]
    raise("Unknown facility type: #{facility.to_s}") if facility_level.nil?
    show_debug=level<=facility_level
  else
    show_debug=level<=@@debug_level
  end

  if show_debug

    if facility
      header="D#{level}(#{facility.to_s})"
    else
      header="D#{level}"
    end

    #Example:  "./libs/lexer.rb:650:in `item'"
    #parse the caller array to determine who called us, what line, and what file
    caller[stack_pos]=~/(.*):(\d+):.*`(.*?)'/

    if $1
      #sometimes the debug function gets called from within an exception block, in which cases the backtrace is not
      #available.
      path=$1
      debug_line=$2
      debug_func=$3
      path=path.split("/")

      if (len=path.length)>2
        debug_file=".../#{path[len-2]}/#{path[len-1]}"
      else
        debug_file=path
      end

      header+=" #{debug_file}:#{debug_func}:#{debug_line}"
    else
      header+=" --from exception--"
    end

    if variable.nil?
      strval="nil"
    elsif variable==:zzempty || variable==""
      strval=""
    elsif variable.class==String
      strval=variable
    else
      strval=variable.inspect
    end

    if truncate>0 && truncate<strval.length
      o_strval=strval
      strval=o_strval[0..(truncate/2)]
      strval+= "  .....  "
      strval+=o_strval[(o_strval.length-(truncate/2))..o_strval.length]
    end

    if message
      strval = variable==:zzempty ?
          message :
          message + ": " + strval
    end
    puts "#{header} #{strval}"

    if backtrace_depth>0
      backtrace=[]
      (stack_pos..stack_pos+backtrace_depth-1).each do |pos|
        pos+=2 #Need to offset current block
        next if pos>=caller.length

        caller[pos]=~/(.*):(\d+):.*`(.*?)'/
        if $1
          #sometimes the debug function gets called from within an exception block, in which cases the backtrace is not
          #available.
          debug_line=$2
          debug_func=$3
          path=$1.split("/")  #global vars are changed by split

          path= (len=path.length)>2 ? ".../#{path[len-2]}/#{path[len-1]}" : path
          backtrace<<"#{path}:#{debug_func}:#{debug_line}"
        else
          backtrace<<"Unknown"
        end
      end
      puts " Backtrace(#{stack_pos}..#{stack_pos+backtrace_depth-1}):["+backtrace.join("],[")+"]"
    end
  end
end
debug_facility(facility,level,variable="",message=nil,truncate=nil) click to toggle source
# File zbxapi/zdebug.rb, line 161
def debug_facility(facility,level,variable="",message=nil,truncate=nil)
  debug(level, :var=>variable, :msg=>message, :truncate=>truncate, :stack_pos=>1)
end
debug_level() click to toggle source
# File zbxapi/zdebug.rb, line 46
def debug_level
  @@debug_level
end
set_debug_level(level) click to toggle source

Either set_debug_level or set_facility_debug_level must be called before the debug functions can be used.

# File zbxapi/zdebug.rb, line 28
def set_debug_level(level)   # sets the current debug level for printing messages
  @@debug_level=level

  # Create faciltiy_level if it's not created already
  @@facility_level= {} if !defined?(@@facility_level)
end
set_facility_debug_level(facility,level) click to toggle source

facility is a symbol, level is an integer

# File zbxapi/zdebug.rb, line 36
def set_facility_debug_level(facility,level)
  # Create faciltiy_level if it's not created already
  @@facility_level= {} if !defined?(@@facility_level)

  @@facility_level[facility]=level

  # Create debug level if it's not already created
  @@debug_level=0 if !defined?(@@debug_level)
end