filename = $logfile; } public function append(RequestData $request) { $this->append[] = $request; } public function flush() { if (file_exists($this->filename)) { $log = json_decode(file_get_contents($this->filename), true); } else { $log = [ 'lastlog' => null, 'events' => [] ]; } while (count($this->append) > 0) { $append = array_shift($this->append); array_push($log['events'], $append); $json = json_encode($log, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); if (count($log['events']) >= self::MAX_EVENTS_PER_FILE) { do { $newlog = sprintf("%s.%04x", $this->filename, rand(0,65535)); } while (file_exists($newlog)); file_put_contents($newlog, $json); $log['lastlog'] = basename($newlog); $log['events'] = []; } else { file_put_contents($this->filename."~", $json); if (filesize($this->filename."~") > 0) { rename($this->filename."~", $this->filename); } else { fprintf(STDERR, "error: Null size log writing requestlog %s\n", $this->filename); } } } } }