Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Untangling Log Files

by jettero (Monsignor)
on Feb 08, 2007 at 12:11 UTC ( [id://598983]=note: print w/replies, xml ) Need Help??


in reply to Untangling Log Files

I like this problem.

Personally, I would keep a hash of filehandles, but close one at random if there are too many open (hoping the pids are kinda grouped together in the logfile).

use strict; my %handles = (); sub open_one { my $pid = shift; return $handles{$pid} if exists $handles{$pid}; while( (my @k = keys %handles) > $threshold ) { my $toclose = $k[ int rand @k ]; close $handles{$toclose}; delete $handles{$toclose}; } open $handles{$pid}, ">>", "logfile-$pid.log" or die "hrm: $!"; return $handles{$pid}; } sub scanner { # ... boring things if( m/something/ ) { my $pid = $1; my $fh = &open_one($pid); print $fh $stuff; } # boring things... }

-Paul

Replies are listed 'Best First'.
Re^2: Untangling Log Files
by Util (Priest) on Feb 08, 2007 at 15:53 UTC

    ++jettero

    FYI, the core module FileCache will automate the close/re-open of filehandles.

Re^2: Untangling Log Files
by loris (Hermit) on Feb 09, 2007 at 07:23 UTC

    I like the problem, too, and I certainly like your solution. Randomly closing file handles seems like an nice way of making it scalable.

    Thanks,

    loris


    "It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ." (from "Slow Loris" by Alexis Deacon)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-06-17 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.