<?php namespace Vn\Lib; class Log { private static $fd = NULL; private static $count; private static $file; static function init($file) { self::close(); self::$file = $file; self::$fd = fopen($file, 'a'); self::rename(); set_error_handler('Vn\Lib\Log::phpHandler', E_ALL); } static function phpHandler($no, $str, $file, $line, $context) { self::write('PHP: %s:%d: %s', $file, $line, $str); switch ($no) { case E_ERROR: case E_PARSE: case E_CORE_ERROR: case E_COMPILE_ERROR: self::write('PHP: Could not continue, exiting.'); self::close(); exit(1); } return TRUE; } static function close() { if (self::$fd != NULL) { fclose(self::$fd); self::$fd = NULL; } } static function write() { if (self::$fd == NULL) return; if (self::$count > 5000) self::rename(); self::$count += fprintf(self::$fd, "%s: %s\n" ,strftime('%Y-%m-%d %T') ,call_user_func_array('sprintf', func_get_args()) ); } static private function rename() { if (filesize(self::$file) > 1000000) { self::close(); $rename = self::$file.'.1'; if (file_exists($rename)) unlink($rename); rename(self::$file, $rename); self::$fd = fopen($file, 'a'); } self::$count = 0; } }