0

Here is a cut from my daemon code:

package somepackage;

use Proc::Daemon;
use Time::HiRes qw/usleep/;
use constant{
  LOGFILE => '/var/log/.../work.log'
};

our $LOG;

unless($pid)
  {
  open($LOG,'>>'.LOGFILE);
  my $tm;
  }

while ($pid == 0)
  {

  $tm=usleep(999940); #to be more accurate, 1 sec
  print $somepackage::LOG $tm."\n";

  }

but the problem is, that sometimes print writes to file only after let's say 10 seconds. I know why it happens, because cycle doesn't wait while print writes to file, instead it goes for a new loop and then sleeps. How can I wait, while print finishes writing to a file and then go to the next iteration?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Starmaster
  • 842
  • 1
  • 6
  • 9

1 Answers1

2

I think you're talking about flush.
Call $LOG->autoflush(1) once, right after you open your log.
(You might need to add use IO::Handle too).

joaquin
  • 82,968
  • 29
  • 138
  • 152
berekuk
  • 186
  • 3