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:

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

In reply to Is there a way to control when output gets printed to a file? by djw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.