Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
We don't bite newbies here... much
 
PerlMonks  

fast logging showing calling position..

by kosun (Acolyte)
on Sep 01, 2005 at 17:34 UTC ( #488436=perlquestion: print w/ replies, xml ) Need Help??
kosun has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm want some very simple logging that given something like

file.pl 20: log_error( "a error msg", $level )
prints the equivalent of doing this on the same line
print STDERR "a error msg:" . __FILE__ . __LINE__ if $level >= THRESHOLD;
i.e. It prints the line number where the function was called, not the line number from inside the log_error subrountine.

Stuck on how to do this

1. Printing the line number where the log_error was called without including it in the argument

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.

This application has to be very fast I don't need much and prefer to not use any log modules if I don't need to.

Any help appreciated thanks so much!

Kosun

Comment on fast logging showing calling position..
Select or Download Code
Re: fast logging showing calling position..
by ikegami (Pope) on Sep 01, 2005 at 17:51 UTC
    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.
Re: fast logging showing calling position..
by talexb (Canon) on Sep 01, 2005 at 19:20 UTC

    I can highly recommend Log::Log4perl. It matches your requirements very closely. I use it daily and it's terrific.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://488436]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-04-18 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (464 votes), past polls