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

Is there a way to control when output gets printed to a file?

by djw (Vicar)
on Nov 28, 2001 at 22:20 UTC ( [id://128124]=perlquestion: print w/replies, xml ) Need Help??

djw has asked for the wisdom of the Perl Monks concerning the following question:

I am using this application to capture, log, and graph out ethernet utilization information on a couple network devices at work. The application collects data at five minute intervals and prints out the result from each "run" to the logfile.

Initially I had it print out each run line (with data) to the console when I was building the tool (so I could watch it), but now I am using a logfile so that I can do weekly/monthly reports etc. The application creates a new logfile for each day at 8am and will continue to run till 5pm and then exit. Here is a snippet of log information:
    Begin data collection for 0.0.0.0 - Runs: 108
    Starting run 1\108 - In: 0.00 Out: 0.00 Time: 8:00
    Starting run 2\108 - In: 0.05 Out: 0.10 Time: 8:05
    ...
    Starting run 108\108 - In: 0.27 Out: 0.01 Time: 16:55
    Utilization Graph Completed - Run length: 9 hours
    Generation complete.

Now when I go and look at the current logfile for the day, I notice that no data actually gets printed to the file until run #74 (always) for each device (all seperate scripts in seperate dir's, with different logfiles). It prints up to the same point on line #74 and then doesn't print the remaining log info until program exit.

It looks to me like its waiting until some kind of buffer reaches a certain point, then dumps it out, then continues without printing until my program exists. In my current program I am writing to the logs like this:

open(LOG, "+>$logFile") || die "Can't open logfile: ($!)\n"; print LOG "Begin data collection for $host - Runs: $numRuns\n"; for ($run = 1; $run <= $numRuns; $run++) { print LOG "Starting run $run\\$numRuns - "; snmpRun(); unless ($run == $numRuns) { sleep("$pauseTime"); } }

This prints out the first line of my logfile, then calls the snmpRun sub from the loop. Inside my sub is this bit of code:
sub snmpRun { ... print LOG "In: $octetInUtil Out: $octetOutUtil Time: $time\n"; ... }

Is there another way to do this so that the data does get written to the file as it hits the print lines? This isn't a big deal, but I'm curious to know.

djw

Replies are listed 'Best First'.
Re: Is there a way to control when output gets printed to a file?
by runrig (Abbot) on Nov 28, 2001 at 22:25 UTC
    How do I flush/unbuffer an output filehandle? Why must I do this?
    And 'perldoc -q buffer' would've told you the same thing :-)

    However, it is the opinion of some that you should explicitly open and close the log file on every print (or group of prints) so that you can rotate/move or (accidentally?) delete the log file while the program is running, and not lose any further output and have output continue to go to a file with the same name. And if it doesn't spend most of its time making log entries, it won't slow anything down significantly.

Re: Is there a way to control when output gets printed to a file?
by Zed_Lopez (Chaplain) on Nov 28, 2001 at 23:32 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found