Self Numbering Debug Lines - a little script

People,

There are probably already things around that do what this little program does and do it more efficiently and elegantly but it was an interesting little exercise for me and some others might find it useful. This effort was not precipitated by a "Doh!" forehead slapping bug but more of a "WTF! I have no idea what is going on here . ." bug. In the end the bug was an odd thing to do with the "dialog" utility and I have worked out how to get around it but it started me on this exercise . .

The background: I struggled to update a little, old NetBook with F23 (underpowered hardware and with the usual hassles with the Broadcom B43 chip) but I finally got it going. Now I am normally just using the console terminals and only occasionally get into X - I have set it up to stream music into the kitchen from a bedroom / office at the other end of the house via WiFi and to use "motion" and the built in camera for motion-detected video capture. So I have been running my little Ruby script for streaming the music quite happily for some weeks, and then suddenly, with no relevant changes (that I was aware of), the script started spitting errors like "cannot access /dev/tty".

I had no idea what was causing the problem or where the problem was in the code so I started inserting debug lines like:

   puts "reached here!"

then:

   puts "1 <-----------"

etc

but with no resolution of the problem this rapidly became tedious . . so I thought of something a little nicer:

Here is a trivial example for "HelloWorld.rb":

#!/usr/bin/ruby

##sdbg

puts "Hello World!"

t = rand( 10 )

if t < 5
   ##sdbg
   puts "The random number is less than five"
else
   ##sdbg
   puts "The random number is NOT less than five"
   ##sdbg print "BTW t = "
   ##sdbg puts t
end

and running:

   sdbg.rb HelloWorld.rb

with sdbg.rb:

#!/usr/bin/ruby

arg = ARGV.shift
outname = arg.gsub( /.rb$/, "_out.rb" )
sdbg_arr = IO.readlines arg

outfile = File.open(outname, "w")

sdbg_arr.each_with_index { |line, i|

   if line.match( /##sdbg$/ ) or line.match( /##sdbg\s$/ )
     line = sprintf( "puts \'%d <----------------------------------------\'", i )
   elsif line.match( /##sdbg\s\S/ )
     rest = line.gsub( /##sdbg/, '' )
     line = sprintf( "puts \'%d <----------------------------------------\' ; #{rest}", i )
   end

   outfile.puts line
}

outfile.close

system( "chmod 755 ./#{outname}" )

produces HelloWorld_out.rb:

#!/usr/bin/ruby

puts '2 <----------------------------------------'

puts "Hello World!"

if rand( 10 ) < 5
   puts '7 <----------------------------------------'
   puts "The random number is less than five"
else
   puts '10 <----------------------------------------'
   puts "The random number is NOT less than five"
   puts '12 <----------------------------------------' ; print "BTW t = "
   puts '13 <----------------------------------------' ; puts t
end

So, the original HelloWorld script runs with the debugging statements and running the file through sdbg.rb produces an output file that also runs but with all the appropriately numbered debug lines. Except for the fact that they inhibit the readability of the script, the debugging lines need not be taken out of the original file if it something that is going to be continually worked and the debug lines could come in handy again . . or they could also just all be stripped out with a single vim command . .

Regards,

Phil.

···

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Are you aware of the built in debugger (start with "-r debug") and
#set_trace_func? I think with these debugging might be much easier.

Kind regards

robert

···

On Sat, Jan 9, 2016 at 11:03 AM, Philip Rhoades <phil@pricom.com.au> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/