Franklin said:
Hi All,
I'm in trouble with Debug information in a large program.
Some info I just want to print in degug phase.
Now I do as follow:
###########################################################
...
use vars qw($Debug);
$Debug ||= 0;
...
#Define option -D
$Debug=1 if(defined($opt{'D'});
...
#Print Debug info when necessary
if($Debug) {
print("Debug:xxxxxx\n");
}
############################################################
Do you think it's a good way to deal with Debug info.
Or do you have a better way to deal with it?
Any suggestions are welcome!
This will make your life hell in the long run, because it's an all or
nothing approach. As your codebase grows, you'll have more and more
debugging messages - many of which will be useless when you're trying to
debug specific problems.
Look at Log::Log4perl:
"Logging beats a debugger if you want to know what's going on in your code
during runtime. However, traditional logging packages are too static and
generate a flood of log messages in your log files that won't help you.
Log::Log4perl is different. It allows you to control the amount of logging
messages generated at three different levels:
At a central location in your system (either in a configuration file or in
the startup code) you specify which components (classes, functions) of your
system should generate logs.
You specify how detailed the logging of these components should be by
specifying logging levels.
You also specify which so-called appenders you want to feed your log
messages to ("Print it to the screen and also append it to /tmp/my.log")
and which format ("Write the date first, then the file name and line
number, and then the log message") they should be in.
This is a very powerful and flexible mechanism. You can turn on and off your
logs at any time, specify the level of detail and make that dependent on
the subsystem that's currently executed."
So, in my code I have lines such as:
Log::Log4perl->get_logger(__PACKAGE__)->debug
("conditionally creating home directory: $home") if DEBUG;
In my debug module (heavily edited):
our @EXPORT_OK = qw( DEBUG );
BEGIN
{
if ( $ENV{RIDAS_DEBUG} )
{
*DEBUG = sub () {1};
}
else
{
*DEBUG = sub () {0};
}
}
This now gives me control of the amount of debugging info emitted on a
module by module basis, specified by a simple Log4perl configuration file.
Having the "if DEBUG" conditional allows debugging calls to be optimized
away at compile time when not required, so performance does not suffer.
Sure, this seems like a hastle to begin with, and the Log::Log4perl docs are
heavy in places, but its a well designed framework, based off the widely
used and respected Java log4j project.
That's my suggestion anyway - it's worked very well for me, but others are
sure to have equally good ways of doing things
Cheers,