1. Printing the line number where the log_error was called without including it in the argument
The four Carp routines and the two Carp::Heavy routines provide this info (using caller).
use Carp ();
sub log_error {
goto(&Carp::carp) if pop(@_) >= THRESHOLD;
}
I think Log::Log4perl also prints the file name and the line number. The above and Log::Log4perl don't meet your second requirement, however.
2. I'd like anything < THRESHOLD for the current run to be removed at compile time if possible to avoid a performance hit...not sure if can really do that...or if it makes a difference.
That's not possible if the level is passed as an argument. Your syntax would have to be
sub DEBUG () { 0 }
sub FATAL () { 4 }
sub THRESHOLD () { 0 }
log_error($msg) if FATAL >= THRESHOLD;
Well, either that or use a source filter.
Maybe you could compile out debug messages, but leave the other ones in?
use Carp ();
sub WARN () { 1 }
sub ERROR () { 2 }
sub DEBUG () { 0 } # True of false.
sub THRESHOLD () { ERROR } # Or use $THRESHOLD. No real diff.
sub log_error {
my $level = (@_==2 ? pop(@_) : THRESHOLD);
return if $level < THRESHOLD;
goto &Carp::carp;
}
log_error($msg) if DEBUG; # Whole statement removed at compile time.
log_error($msg, WARN); # Calls log_error, but not carp.
log_error($msg, FATAL); # Calls log_error and carp.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.