/** * Enables displaying or logging errors and exceptions. * @param mixed production, development mode, autodetection or IP address(es) whitelist. * @param string error log directory; enables logging in production mode, FALSE means that logging is disabled * @param string administrator email; enables email sending in production mode * @return void */ public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL) { self::$time = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(TRUE); if (isset($_SERVER['REQUEST_URI'])) { self::$source = (isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $_SERVER['REQUEST_URI']; } else { self::$source = empty($_SERVER['argv']) ? 'CLI' : 'CLI: ' . implode(' ', $_SERVER['argv']); } self::$consoleColors =& Dumper::$terminalColors; error_reporting(E_ALL | E_STRICT); // production/development mode detection if (is_bool($mode)) { self::$productionMode = $mode; } elseif ($mode !== self::DETECT || self::$productionMode === NULL) { // IP addresses or computer names whitelist detection $list = is_string($mode) ? preg_split('#[,\\s]+#', $mode) : (array) $mode; if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $list[] = '127.0.0.1'; $list[] = '::1'; } self::$productionMode = !in_array(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : php_uname('n'), $list, TRUE); } // logging configuration if (is_string($logDirectory)) { self::$logDirectory = realpath($logDirectory); if (self::$logDirectory === FALSE) { echo __METHOD__ . "() error: Log directory is not found or is not directory.\n"; exit(254); } } elseif ($logDirectory === FALSE || self::$logDirectory === NULL) { self::$logDirectory = FALSE; } if (self::$logDirectory) { ini_set('error_log', self::$logDirectory . '/php_error.log'); } // php configuration if (function_exists('ini_set')) { ini_set('display_errors', !self::$productionMode); // or 'stderr' ini_set('html_errors', FALSE); ini_set('log_errors', FALSE); } elseif (ini_get('display_errors') != !self::$productionMode && ini_get('display_errors') !== (self::$productionMode ? 'stderr' : 'stdout')) { // intentionally == echo __METHOD__ . "() error: Unable to set 'display_errors' because function ini_set() is disabled.\n"; exit(254); } if ($email) { if (!is_string($email) && !is_array($email)) { echo __METHOD__ . "() error: Email address must be a string.\n"; exit(254); } self::$email = $email; } if (!self::$enabled) { register_shutdown_function(array(__CLASS__, '_shutdownHandler')); set_exception_handler(array(__CLASS__, '_exceptionHandler')); set_error_handler(array(__CLASS__, '_errorHandler')); foreach (array('Nette\\Diagnostics\\Bar', 'Nette\\Diagnostics\\BlueScreen', 'Nette\\Diagnostics\\DefaultBarPanel', 'Nette\\Diagnostics\\Dumper', 'Nette\\Diagnostics\\FireLogger', 'Nette\\Diagnostics\\Helpers', 'Nette\\Diagnostics\\Logger', 'Nette\\Utils\\Html', 'Nette\\Utils\\Strings') as $class) { class_exists($class); } self::$enabled = TRUE; } }
/** * Static class constructor. * @internal */ public static function _init() { self::$time = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(TRUE); self::$consoleMode = PHP_SAPI === 'cli'; self::$productionMode = self::DETECT; if (self::$consoleMode) { self::$source = empty($_SERVER['argv']) ? 'cli' : 'cli: ' . implode(' ', $_SERVER['argv']); } else { self::$ajaxDetected = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'; if (isset($_SERVER['REQUEST_URI'])) { self::$source = (isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '')) . $_SERVER['REQUEST_URI']; } } self::$logger = new Logger(); self::$logDirectory =& self::$logger->directory; self::$email =& self::$logger->email; self::$mailer =& self::$logger->mailer; self::$emailSnooze =& Logger::$emailSnooze; self::$fireLogger = new FireLogger(); self::$blueScreen = new BlueScreen(); self::$blueScreen->addPanel(function ($e) { if ($e instanceof Nette\Templating\FilterException) { return array('tab' => 'Template', 'panel' => '<p><b>File:</b> ' . Helpers::editorLink($e->sourceFile, $e->sourceLine) . ' <b>Line:</b> ' . ($e->sourceLine ? $e->sourceLine : 'n/a') . '</p>' . ($e->sourceLine ? BlueScreen::highlightFile($e->sourceFile, $e->sourceLine) : '')); } elseif ($e instanceof Nette\Utils\NeonException && preg_match('#line (\\d+)#', $e->getMessage(), $m)) { if ($item = Helpers::findTrace($e->getTrace(), 'Nette\\Config\\Adapters\\NeonAdapter::load')) { return array('tab' => 'NEON', 'panel' => '<p><b>File:</b> ' . Helpers::editorLink($item['args'][0], $m[1]) . ' <b>Line:</b> ' . $m[1] . '</p>' . BlueScreen::highlightFile($item['args'][0], $m[1])); } elseif ($item = Helpers::findTrace($e->getTrace(), 'Nette\\Utils\\Neon::decode')) { return array('tab' => 'NEON', 'panel' => BlueScreen::highlightPhp($item['args'][0], $m[1])); } } }); self::$bar = new Bar(); self::$bar->addPanel(new DefaultBarPanel('time')); self::$bar->addPanel(new DefaultBarPanel('memory')); self::$bar->addPanel(self::$errorPanel = new DefaultBarPanel('errors')); // filled by _errorHandler() self::$bar->addPanel(self::$dumpPanel = new DefaultBarPanel('dumps')); // filled by barDump() }
/** * Static class constructor. * @internal */ public static function _init() { self::$time = microtime(TRUE); self::$consoleMode = PHP_SAPI === 'cli'; self::$productionMode = self::DETECT; if (self::$consoleMode) { self::$source = empty($_SERVER['argv']) ? 'cli' : 'cli: ' . implode(' ', $_SERVER['argv']); } else { self::$ajaxDetected = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'; if (isset($_SERVER['REQUEST_URI'])) { self::$source = (isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '')) . $_SERVER['REQUEST_URI']; } } self::$logger = new Logger(); self::$logDirectory =& self::$logger->directory; self::$email =& self::$logger->email; self::$mailer =& self::$logger->mailer; self::$emailSnooze =& Logger::$emailSnooze; self::$fireLogger = new FireLogger(); self::$blueScreen = new BlueScreen(); self::$blueScreen->addPanel(function ($e) { if ($e instanceof Nette\Templating\FilterException) { return array('tab' => 'Template', 'panel' => '<p><b>File:</b> ' . Helpers::editorLink($e->sourceFile, $e->sourceLine) . ' <b>Line:</b> ' . ($e->sourceLine ? $e->sourceLine : 'n/a') . '</p>' . ($e->sourceLine ? '<pre>' . BlueScreen::highlightFile($e->sourceFile, $e->sourceLine) . '</pre>' : '')); } }); self::$bar = new Bar(); self::$bar->addPanel(new DefaultBarPanel('time')); self::$bar->addPanel(new DefaultBarPanel('memory')); self::$bar->addPanel(self::$errorPanel = new DefaultBarPanel('errors')); // filled by _errorHandler() self::$bar->addPanel(self::$dumpPanel = new DefaultBarPanel('dumps')); // filled by barDump() }