B
Ben Giddings
Hi everybody,
Growing out of the discussions about $DEBUG and $VERBOSE, and how
they're set from the commandline, I've created a dead-simple
"debugprint" library package. You can find it on RAA at:
But since it's so tiny, here's the entire code:
module Kernel
def info(*args)
puts(*args) if $VERBOSE
end
def debug(*args)
puts(*args) if $DEBUG
end
module_function :info, :debug
end
This time, I actually followed the KISS principle.
I'm hoping this can save a few keystrokes in its current form. But I
also have visions for something more ambitious, so I'd like to discuss
a little.
In C, I have some debug macros that look like this:
#define printlnDebug(fmt, ...) \
printf("[D]%s#%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__,
__VA_ARGS__)
#define printvarDebug(var) \
printf("[D]%s#%s:%d %s=%d\n", __FILE__, __FUNCTION__, __LINE__, #var,
var)
The end result is that if I have these lines of c code:
state = "running";
count = 5;
printlnDebug("state is: %s", state);
printvarDebug(count);
I get this result:
[D]main.c#doSomething:80 state is: running
[D]main.c#doSomething:81 count=5
I have ambitions to do something similar with Ruby. My idea is that
you could set a format string, similar to the one in the Time class and
use that when you print out debug info, the default format string would
simply include the debug message.
So, some questions:
1) is it possible for a method to get access to the context that called
it so it can fill in things like source file name, method name, and
line number
2) is there a way to get the name of a variable into a string, like the
#var construct does in C macros
Could the ruby commandline syntax be modified so that they can do more
than just set $DEBUG and $VERBOSE to true/false? For example, maybe
allowing multiple flags so that -ddd (or -d -d -d) would set $DEBUG to
3. Or, possibly adding an optional parameter with the same syntax as
the -e flag that would allow you to set $DEBUG or $VERBOSE to any value
you want. As others have mentioned, these changes would keep backwards
compatibility with scripts that say "if $DEBUG", which seems to be the
most common usage. On the other hand, might they have an effect on
other things, like C code that compiles to a Ruby library. I have no
idea how $DEBUG and $VERBOSE are used here.
Comments?
Ben
Growing out of the discussions about $DEBUG and $VERBOSE, and how
they're set from the commandline, I've created a dead-simple
"debugprint" library package. You can find it on RAA at:
But since it's so tiny, here's the entire code:
module Kernel
def info(*args)
puts(*args) if $VERBOSE
end
def debug(*args)
puts(*args) if $DEBUG
end
module_function :info, :debug
end
This time, I actually followed the KISS principle.
I'm hoping this can save a few keystrokes in its current form. But I
also have visions for something more ambitious, so I'd like to discuss
a little.
In C, I have some debug macros that look like this:
#define printlnDebug(fmt, ...) \
printf("[D]%s#%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__,
__VA_ARGS__)
#define printvarDebug(var) \
printf("[D]%s#%s:%d %s=%d\n", __FILE__, __FUNCTION__, __LINE__, #var,
var)
The end result is that if I have these lines of c code:
state = "running";
count = 5;
printlnDebug("state is: %s", state);
printvarDebug(count);
I get this result:
[D]main.c#doSomething:80 state is: running
[D]main.c#doSomething:81 count=5
I have ambitions to do something similar with Ruby. My idea is that
you could set a format string, similar to the one in the Time class and
use that when you print out debug info, the default format string would
simply include the debug message.
So, some questions:
1) is it possible for a method to get access to the context that called
it so it can fill in things like source file name, method name, and
line number
2) is there a way to get the name of a variable into a string, like the
#var construct does in C macros
Could the ruby commandline syntax be modified so that they can do more
than just set $DEBUG and $VERBOSE to true/false? For example, maybe
allowing multiple flags so that -ddd (or -d -d -d) would set $DEBUG to
3. Or, possibly adding an optional parameter with the same syntax as
the -e flag that would allow you to set $DEBUG or $VERBOSE to any value
you want. As others have mentioned, these changes would keep backwards
compatibility with scripts that say "if $DEBUG", which seems to be the
most common usage. On the other hand, might they have an effect on
other things, like C code that compiles to a Ruby library. I have no
idea how $DEBUG and $VERBOSE are used here.
Comments?
Ben