Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Reference sub name

by martin (Pilgrim)
on Jul 27, 2008 at 10:10 UTC ( #700349=note: print w/ replies, xml ) Need Help??


in reply to Reference sub name

The caller function will let you do that.

Example:

sub foo { my $calling_sub = (caller 1)[3]; $calling_sub = 'the main block' if !defined $calling_sub; print "called from: $calling_sub\n"; } sub bar { foo(); } bar(); # prints: called from: main::bar foo(); # prints: called from: the main block

Personally, when I need additional log information depending on context, I prefer to use explicit parameters for the logging function. This way, I have more control on the logging protocol, and the individual context tags at each logger call make it easy to map log messages back to the related pieces of code without exposing program internals in the logs. I do not depend on a one-to-one relationship between subroutine names and things worth while to log, either.

Example:

# using Sys::Syslog log levels sub open_db { my $dbname = shift; # ... if (defined $dbh) { log_this(LOG_NOTICE, 'OPEN_DB', $dbname, 'db connection establ +ished'); } else { log_this(LOG_ERR, 'OPEN_DB_NOK', $dbname, 'db connection faile +d'); } }


Comment on Re: Reference sub name
Select or Download Code
Reaped: Accidental duplicate. Please delete.
by NodeReaper (Curate) on Jul 27, 2008 at 10:41 UTC
Re^2: Reference sub name
by BrowserUk (Pope) on Jul 27, 2008 at 10:41 UTC
    ... without exposing program internals in the logs.

    Why? Who is reading your logs and what do you think that they might discover?

    (That they could just as easily discover by reading the sources which they presumably would also have access to.)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Exposing internals, in some cases, is not only a matter of secrecy. If your logs are not mainly intended for the purpose of debugging code, but for keeping track of any kind of events, internals might be irrelevant, needlessly verbose, and inconveniently varying with software versions. In any case simpler log file contents make log file evaluation simpler.

      The original post mentioned a logging interface already being in place, which supports the notion of a more general attitude.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-12-27 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls