Snippet: debugging (and a question)

M

Moses Hall

Greetings.

I'm a recent Ruby convert and I absolutely love it. No other lingo looks
so much (or for that matter, even vaguely) like pseudocode! Great stuff.

Anyway, in my code I found myself keeping an array of the methods for
which I wanted verbose debugging info, to get info printed only
during the execution of particular method(s). Previously I would check
in each method whether its name was in the array:

def method1(x)
debug = @@debug.member? "method1"
printf "method1 called with %s\n", x.inspect if debug
...
end

Then I came up with a way to reduce this to a "debug" method, which gets the
stack and grabs the (calling) method's name and then returns true if that
is in the global array o' method names.

It's kinda skanky, anyone have a better, safer, cleaner,
and/or cooler way to do this?
It will break if the format of the stack dump changes.
I like doing this sort of thing because it thins out the source code,
keeps focus on the algorithm not infrastructure.


def debug
method = caller[0]
# A Perly way to get the caller from
# the line "... in `method_name'"
method =~ /in\s`(.+)'/i
return @@debug.member? $1
end

Thanks,

/\/\oses
"(e-mail address removed)".rot13
www.blugs.com
 
M

Mauricio Fernández

Greetings.

I'm a recent Ruby convert and I absolutely love it. No other lingo looks
so much (or for that matter, even vaguely) like pseudocode! Great stuff.

Anyway, in my code I found myself keeping an array of the methods for
which I wanted verbose debugging info, to get info printed only
during the execution of particular method(s). Previously I would check
in each method whether its name was in the array:

def method1(x)
debug = @@debug.member? "method1"
printf "method1 called with %s\n", x.inspect if debug
...
end

Then I came up with a way to reduce this to a "debug" method, which gets the
stack and grabs the (calling) method's name and then returns true if that
is in the global array o' method names.

It's kinda skanky, anyone have a better, safer, cleaner,
and/or cooler way to do this?

You can use aspects (AspectR) to add logging to the desired methods.

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

* dpkg ponders: 'C++' should have been called 'D'
-- #Debian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top