self::$_level) return; /* time */ $y = date('Y'); $m = date('m'); $d = date('d'); $D = "$y-$m-$d"; $H = date('H'); $M = date('i'); $S = date('s'); $T = "$H:$M:$S"; /* process id */ $p = getmypid(); /* function information */ $F = '(unknown)'; $L = '(unknown)'; $f = '(none)'; $g = '(none)'; $trace = debug_backtrace(); $count = count($trace); for ($i = 0; $i < $count; $i++) { $frame = $trace[$i]; if ($frame['file'] != __FILE__) { $F = $frame['file']; $L = $frame['line']; if ($i < $count - 1) { $frame = $trace[$i + 1]; if (array_key_exists('class', $frame)) $f = $frame['class'] . '::' . $frame['function']; else $f = $frame['function']; $g = preg_replace('/^IMu/', '', $f); } break; } } /* Build the prefix */ $prefix = self::$_prefix; $prefix = preg_replace('/%y/', $y, $prefix); $prefix = preg_replace('/%m/', $m, $prefix); $prefix = preg_replace('/%d/', $d, $prefix); $prefix = preg_replace('/%D/', $D, $prefix); $prefix = preg_replace('/%H/', $H, $prefix); $prefix = preg_replace('/%M/', $M, $prefix); $prefix = preg_replace('/%S/', $S, $prefix); $prefix = preg_replace('/%T/', $T, $prefix); $prefix = preg_replace('/%p/', $p, $prefix); $prefix = preg_replace('/%F/', $F, $prefix); $prefix = preg_replace('/%L/', $L, $prefix); $prefix = preg_replace('/%f/', $f, $prefix); $prefix = preg_replace('/%g/', $g, $prefix); /* Build the string */ $strs = array(); foreach ($args as $arg) $strs[] = print_r($arg, true); $format = "$format"; if (count($args) > 0) $format = vsprintf($format, $strs); $text = $prefix . $format; $text = preg_replace('/\r?\n/', PHP_EOL, $text); $text = preg_replace('/\s+$/', '', $text); $text .= PHP_EOL; /* Write it out */ if (! self::isSTDOUT()) { /* Lock */ if (! flock(self::$_handle, LOCK_EX)) return; /* Append */ if (fseek(self::$_handle, 0, SEEK_END) != 0) { flock(self::$_handle, LOCK_UN); return; } } fwrite(self::$_handle, $text); fflush(self::$_handle); if (! self::isSTDOUT()) flock(self::$_handle, LOCK_UN); } private static $_cli; private static $_file; private static $_handle; private static $_level; private static $_prefix; private static function isSTDOUT() { if (! self::$_cli) return false; return self::$_handle == STDOUT; } } IMuTrace::init(); ?>