defaultVarsFormater($vars) function. * * @var unknown CONF_VARS_FORMATER_FUNC */ const CONF_VARS_FORMATER_FUNC = "vars_formater_function"; public static $conf = array ( // Default values self::CONF_REFRESH_EVERY => 500, self::CONF_ENABLE_LOG => true ); public static function defaultVarsFormater($vars) { if ($vars == null || $vars == "") { return null; } if (! is_array ( $vars )) { $vars = array ( $vars ); } $resutl = ""; $first = true; foreach ( $vars as $v ) { $e = htmlspecialchars ( var_export ( $v, true ) ); if (! $first) { $resutl .= ", " . $e; } else { $resutl .= " " . $e; } $first = false; } return $resutl; } /** * Enable log message. */ public static function enable() { self::$conf [self::CONF_ENABLE_LOG] = true; } /** * Disable log message. */ public static function disable() { self::$conf [self::CONF_ENABLE_LOG] = false; } /** * Singleton * * @var PhpConsoleUtil $instance */ private static $instance; public function deleteAll() { $this->db->exec ( "DELETE FROM messages" ); } /** * * @return PhpConsole */ public static function get() { if (null === static::$instance) { static::$instance = new static (); } return static::$instance; } /** * * @var PDO $db */ private $db; private function __construct() { if ( isset ( self::$conf [self::CONF_DB_PATH] )) { $dbPath = self::$conf [self::CONF_DB_PATH]; } else { $dbPath = realpath ( dirname ( __FILE__ ) ) . "/debug_log.sqlite3"; } $this->db = new \PDO ( 'sqlite:' . $dbPath ); $this->db->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $this->db->exec ( "CREATE TABLE IF NOT EXISTS messages ( id INTEGER PRIMARY KEY AUTOINCREMENT , msg TEXT, type TEXT, file TEXT, line INTEGER, html TEXT, time INTEGER)" ); } public function addAll($msg, $file, $line, $type, $html = null) { $stmt = $this->db->prepare ( "INSERT INTO messages (id, msg, file, line, type, html, time) VALUES (NULL, ?, ?, ?, ?, ?, ?)" ); $stmt->execute ( [ $msg, $file, $line, $type, $html, time () ] ); } public function getAllMoreThan($moreThen) { $result = $this->db->query ( "SELECT * FROM messages WHERE id > " . $moreThen . " ORDER BY time ASC LIMIT 200" ); return $result->fetchAll ( PDO::FETCH_ASSOC ); } public function deleteMessageFromSession() { PhpConsole::$instance->deleteAll (); } public function showJson() { header ( 'Content-Type: application/json' ); if (isset ( $_GET ["PhpConsoleLastId"] )) { $lastId = ( int ) $_GET ["PhpConsoleLastId"]; $res = PhpConsole::get ()->getAllMoreThan ( $lastId ); echo json_encode ( $res ); return; } } } function phpconsole_mylog($msg, $type, $vars = null) { if (! PhpConsole::$conf [PhpConsole::CONF_ENABLE_LOG]) { return; } $msg = htmlspecialchars ( $msg ); if (isset ( PhpConsole::$conf [PhpConsole::CONF_VARS_FORMATER_FUNC] )) { $funcFormater = PhpConsole::$conf [PhpConsole::CONF_VARS_FORMATER_FUNC]; $html = $funcFormater ( $vars ); } else { $html = PhpConsole::defaultVarsFormater ( $vars ); } // var_dump($funcFormater); // $html = PhpConsoleUtil::get ()->createHTML ( $vars ); $c = getCaller ( debug_backtrace ( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ) [1] ['function'] ); $pi = pathinfo ( $c ["file"] ); PhpConsole::get ()->addAll ( $msg, $c ["file"], $c ["line"], $type, $html ); } function getCaller($functionName) { $d = debug_backtrace ( DEBUG_BACKTRACE_IGNORE_ARGS ); $num = 0; $numFound = 0; foreach ( $d as $r ) { if ($r ["function"] === $functionName) { $numFound = $num; } $num ++; } $message = array (); $message ["file"] = $d [$numFound] ["file"]; $message ["line"] = $d [$numFound] ["line"]; return $message; } if (isset ( $_GET ["PhpConsoleDelete"] )) { PhpConsole::get ()->deleteMessageFromSession (); return; } if (isset ( $_GET ["PhpConsoleLastId"] )) { // call from js, echo data PhpConsole::get ()->showJson (); return; } // If called directly if (basename ( __FILE__ ) == basename ( $_SERVER ["SCRIPT_FILENAME"] )) { ?>