#!/var/mwt/bin/svc/php = 0; $i--) { debug($i); //(if $i = 0, then it means just $fileprefix) $old = $fileprefix.(($i>0) ? ".".$i : ""); $new = $fileprefix.".".($i+1); if (file_exists($old)) { // rename it fwrite(STDOUT, "renaming ".$old." to ".$new."\n"); rename($old, $new); } } } function debug($text) { if (DEBUG != 0) { fwrite(STDOUT, "::DEBUG:: ".$text."\n"); } } /*** Main code ***/ pcntl_signal(SIGTERM, "signal_handler"); // set signal handlers pcntl_signal(SIGHUP, "signal_handler"); // parse days commmand-line arg if (!isset($_SERVER["argv"][1]) || !is_numeric($_SERVER["argv"][1])) { showHelp(); exit(255); } $days = $_SERVER["argv"][1]; // parse filename command-line arg if (!isset($_SERVER["argv"][2])) { showHelp(); exit(255); } $logfile = $_SERVER["argv"][2]; if (file_exists($logfile)) { debug("filetime ".date("r", filemtime($logfile))); debug("unixdays ".unixdays(filemtime($logfile))); debug("unixdays now ".unixdays(time()) ); if (unixdays(filemtime($logfile)) != unixdays(time())) { // rotate files before we start to write debug("rotating files"); rotate($logfile, $days); } } $lastwrite = time(); // open the log file $lh = fopen($logfile, "a"); while ($data = fgets(STDIN,1024)) { if (strlen($data) > 1) { $time = time(); // check if we need to rotate files if (unixdays($lastwrite) != unixdays(time())) { // rotate rotate($logfile, $days); } $lastwrite = time(); fwrite($lh, date(DATE_FORMAT,$time)." ".$data); } } fclose($lh); ?>