Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Override printing to STDOUT/ERR

by LanX (Cardinal)
on Apr 13, 2018 at 13:57 UTC ( #1212810=note: print w/replies, xml ) Need Help??


in reply to Override printing to STDOUT/ERR

I'm still confused about the use case....

If this is only for debugging purpose to find the line numbers , you can close STDOUT and STDERR and cause warnings. But the string to be printed will be lost.

DB<6> close STDOUT DB<7> use warnings; print 666 print() on closed filehandle STDOUT at (eval 15)

Otherwise I concur with Corion to tie a filehandle. caller will tell you where the tied method was called and you can redirect STDOUT and STDERR to these FHs. (also with select )

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Override printing to STDOUT/ERR
by bliako (Prior) on Apr 13, 2018 at 14:04 UTC

    What I would like to do eventually is to be able to group program output (to STDOUT) depending on which program block produced it.

    Maybe visualising program output as a tree/graph. Which could also be used to selectively switch on and off program output, highligthing certain sections etc. Lots of possibilities.

    I will give more details in the Meditations section once I have a prototype.

    thanks for the input, bliako

      You may want to consider using on of the logging frameworks, available on CPAN, as opposed to a print based approach

      Log::Log4perl, Log::Any, Log::ger are examples that support directing output of the log statements to different locations based upon a category. A category is generally a package but can be explicitly set as desired.

      Here an example from the Log::ger::Manual::Tutorial::05_Category tutorial

      use Log::ger::Output Composite => ( outputs => { File => [ { conf => {path=>'/path/app.log'}, category_level => { Foo => 'off' }, }, { conf => {path=>'/path/foo.log'}, level => 'off', category_level => { Foo => 'trace' }, }, ], }, );

      lbe

        lbe, I am just looking at these modules right now. It is great that one can separate log output depending on categories but it seems to me that they don't actually catch print statements and redirect them but rather handle their own logger messages.

        Ideally what I want is, given a perl program possibly using many modules, to run it and have every single print statement 1) redirected to file and 2) prepended by (filename,modulename,subname,linenumber). If I am given that program, it means I can clone it and replace all print statements with log statements but 1) what about print statements inside 3rd-party modules and 2) extra complication if bleached.

        What I am doing right now and works is 1) redirect STDOUT to file 2) tie STDOUT to a class so when any print is called on that filehandle, I intercept the call and prepend the caller's fingerprint all the way to the top of the frame (<main>).

        The module I mentioned works well but I had to extend it in order to handle STDERR and also for redirecting to a file (implementing OPEN/CLOSE).

        In case you have a suggestion: my current problem is what data structure to use for storing all these print statements' output so that, for example, I make queries like: what is the STDOUT for lines X-Y, filename Z or do not show me output for lines so and so or sub so and so or package so and so. The data is all there but structuring it in order to make these queries is my next stop. I am currently playing with tree. I will sure post a separate request about these problems.

        thanks, bliako

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2020-10-22 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (225 votes). Check out past polls.

    Notices?