Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How do I get Cluck output into a log file?

by steves (Curate)
on Dec 29, 2004 at 13:57 UTC ( [id://417980]=note: print w/replies, xml ) Need Help??


in reply to How do I get Cluck output into a log file?

The messages get output to stdout, but I guess cluck is returning 1 instead of the debugging message to my logMessage() function.

That is correct. cluck() doesn't return a message, it prints one, so the way you're trying to use it will not work. Carp has shortmess and longmess methods that return the message strings instead of outputting them. Those will work for what you're trying to do.

Replies are listed 'Best First'.
But cluck gives you more.
by tphyahoo (Vicar) on Dec 29, 2004 at 14:23 UTC
    Steves, thanks for pointing that out, but I'm still not quite where I want with this.

    Shortmess and longmess output stuff write stuff along the lines of

    shortmess at at testcarp.pl line 4 longmess at at testcarp.pl line 4
    However, they do not mention what line of testcarp.PM, the module, the debugging message occurs at. Whereas cluck does give you this.

    Is there some way I can get this additional info, grabbing it out of cluck, or otherwise?

    thanks!

    thomas.

      longmess and shortmess do include caller information. The key is how deep you are in the stack, as Carp has a set of rules for deciding how much stack trace (if any) to include. I'm not 100% sure the rules are consistent across all the calls. Here's a test program that illustrates. Maybe a monk with more Carp knowledge can comment. I need to think about it some more ... I suspect there's some nonobvious consistency here.

      use strict; use Carp qw(cluck); sub test_errors { cluck "First test message"; my $long = Carp::longmess("Second test message"); my $short = Carp::longmess("Third test message"); print STDERR $long; print STDERR $short; test2(); } sub test2 { cluck "Fourth test message"; my $long = Carp::longmess("Fifth test message"); my $short = Carp::longmess("Sixth test message"); print STDERR $long; print STDERR $short; } test_errors();
      produces:
      First test message at cluckt line 8 main::test_errors() called at cluckt line 30 Second test message at cluckt line 30 Third test message at cluckt line 30 Fourth test message at cluckt line 21 main::test2() called at cluckt line 16 main::test_errors() called at cluckt line 30 Fifth test message at cluckt line 16 main::test_errors() called at cluckt line 30 Sixth test message at cluckt line 16 main::test_errors() called at cluckt line 30

      Basically, longmess and shortmess appear to always include one less stack frame than cluck does. Maybe someone should cluck about this. ;-)

      Note that I got the same results by calling longmess and shortmess directly in the print calls rather than getting the messages into local variables first.

      If I understand your question, I think you are looking for information available in caller. You don't want to know who called you, but who called something that called you.

      use strict; use Carp; trace('foo'); trace('bar'); func(); sub func { trace('in func'); } sub trace { my $string = shift; my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller( +1); if($subroutine) { print "[$subroutine]: $string\n"; } else { print "[**IN MAIN**]: $string\n"; } } __END__ [**IN MAIN**]: foo [**IN MAIN**]: bar [main::func]: in func

      "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


      OSUnderdog

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-03-28 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found