<commit_before><?php

/* vim: set noexpandtab tabstop=4 shiftwidth=4 foldmethod=marker: */

require_once 'Swat/SwatErrorDisplayer.php';
require_once 'Swat/SwatErrorLogger.php';

/**
 * An error in Swat
 *
 * Unlike {@link SwatException} objects, errors do not interrupt the flow of
 * execution and can not be caught. Errors in Swat have handy methods for
 * outputting nicely formed error messages and logging errors.
 *
 * Like {@link SwatException}, SwatError filters sensitive data from stack
 * trace parameters. See the class-level documentation of SwatException for
 * details on how this works.
 *
 * @package   Swat
 * @copyright 2006-2007 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SwatError
{
	// {{{ protected properties

	/**
	 * The message of this error
	 *
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var string
	 */
	protected $message;

	/**
	 * The severity of this error
	 *
	 * Error severity should be one of the E_* constants defined by PHP.
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var integer
	 */
	protected $severity;

	/**
	 * The file this error occurred in
	 *
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var string
	 */
	protected $file;

	/**
	 * The line this error occurred at
	 *
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var integer
	 */
	protected $line;

	/**
	 * The backtrace of this error
	 *
	 * This should be an array of the form provided by the built-in PHP
	 * function debug_backtrace().
	 *
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var array
	 */
	protected $backtrace;

	/**
	 * @var SwatErrorDisplayer
	 */
	protected static $displayer = null;

	/**
	 * @var SwatErrorLogger
	 */
	protected static $logger = null;

	/**
	 * @var integer
	 */
	protected static $fatal_severity = E_USER_ERROR;

	// }}}
	// {{{ public static function setLogger()

	/**
	 * Sets the object that logs SwatError objects when they are processed
	 *
	 * For example:
	 * <code>
	 * SwatError::setLogger(new CustomLogger());
	 * </code>
	 *
	 * @param SwatErrorLogger $logger the object to use to log exceptions.
	 */
	public static function setLogger(SwatErrorLogger $logger)
	{
		self::$logger = $logger;
	}

	// }}}
	// {{{ public static function setDisplayer()

	/**
	 * Sets the object that displays SwatError objects when they are
	 * processed
	 *
	 * For example:
	 * <code>
	 * SwatError::setDisplayer(new SilverorangeDisplayer());
	 * </code>
	 *
	 * @param SwatErrorDisplayer $displayer the object to use to display
	 *                                           exceptions.
	 */
	public static function setDisplayer(SwatErrorDisplayer $displayer)
	{
		self::$displayer = $displayer;
	}

	// }}}
	// {{{ public static function setFatalSeverity()

	/**
	 * Sets the severity of SwatError that should be fatal
	 *
	 * @param integer $severity a bitwise combination of PHP error severities
	 *                           to be considered fatal.
	 */
	public static function setFatalSeverity($severity)
	{
		self::$fatal_severity = $severity;
	}

	// }}}
	// {{{ public function __construct()

	/**
	 * Creates a new error object
	 *
	 * Error objects contain methods to display and log all types of errors
	 * that may occur.
	 *
	 * @param integer $severity the error code of this error. This should
	 *                           be one of the E_* constants set by PHP. See
	 *                           {@link
	 *                           http://php.net/manual/en/ref.errorfunc.php
	 *                           Error Handling and Logging Functions}.
	 * @param string $message the error message of this error.
	 * @param string $file the name of the file this error occurred in.
	 * @param integer $line the line number this error occurred at.
	 */
	public function __construct($severity, $message, $file, $line)
	{
		$backtrace = debug_backtrace();
		// remove this method call and the handle() call from the backtrace
		array_shift($backtrace);
		array_shift($backtrace);

		$this->message = $message;
		$this->severity = $severity;
		$this->file = $file;
		$this->line = $line;
		$this->backtrace = &$backtrace;
	}

	// }}}
	// {{{ public function process()

	/**
	 * Processes this error
	 *
	 * Processing involves displaying errors, logging errors and sending
	 * error message emails.
	 *
	 * If a fatal error has occured, this calls exit to ensure no further
	 * processing is done.
	 */
	public function process()
	{
		if (ini_get('display_errors'))
			$this->display();

		if (ini_get('log_errors'))
			$this->log();

		if ($this->severity & self::$fatal_severity)
			exit(1);
	}

	// }}}
	// {{{ public function log()

	/**
	 * Logs this error
	 *
	 * The error is logged to the webserver error log.
	 */
	public function log()
	{
		if (self::$logger === null) {
			error_log($this->getSummary(), 0);
		} else {
			$logger = self::$logger;
			$logger->log($this);
		}
	}

	// }}}
	// {{{ public function display()

	public function display()
	{
		if (self::$displayer === null) {
			if (isset($_SERVER['REQUEST_URI']))
				echo $this->toXHTML();
			else<commit_msg>Two new getter methods so logger can access original error data.<commit_after><?php

/* vim: set noexpandtab tabstop=4 shiftwidth=4 foldmethod=marker: */

require_once 'Swat/SwatErrorDisplayer.php';
require_once 'Swat/SwatErrorLogger.php';

/**
 * An error in Swat
 *
 * Unlike {@link SwatException} objects, errors do not interrupt the flow of
 * execution and can not be caught. Errors in Swat have handy methods for
 * outputting nicely formed error messages and logging errors.
 *
 * Like {@link SwatException}, SwatError filters sensitive data from stack
 * trace parameters. See the class-level documentation of SwatException for
 * details on how this works.
 *
 * @package   Swat
 * @copyright 2006-2007 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SwatError
{
	// {{{ protected properties

	/**
	 * The message of this error
	 *
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var string
	 */
	protected $message;

	/**
	 * The severity of this error
	 *
	 * Error severity should be one of the E_* constants defined by PHP.
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var integer
	 */
	protected $severity;

	/**
	 * The file this error occurred in
	 *
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var string
	 */
	protected $file;

	/**
	 * The line this error occurred at
	 *
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var integer
	 */
	protected $line;

	/**
	 * The backtrace of this error
	 *
	 * This should be an array of the form provided by the built-in PHP
	 * function debug_backtrace().
	 *
	 * Set in {@link SwatError::__construct()}
	 *
	 * @var array
	 */
	protected $backtrace;

	/**
	 * @var SwatErrorDisplayer
	 */
	protected static $displayer = null;

	/**
	 * @var SwatErrorLogger
	 */
	protected static $logger = null;

	/**
	 * @var integer
	 */
	protected static $fatal_severity = E_USER_ERROR;

	// }}}
	// {{{ public static function setLogger()

	/**
	 * Sets the object that logs SwatError objects when they are processed
	 *
	 * For example:
	 * <code>
	 * SwatError::setLogger(new CustomLogger());
	 * </code>
	 *
	 * @param SwatErrorLogger $logger the object to use to log exceptions.
	 */
	public static function setLogger(SwatErrorLogger $logger)
	{
		self::$logger = $logger;
	}

	// }}}
	// {{{ public static function setDisplayer()

	/**
	 * Sets the object that displays SwatError objects when they are
	 * processed
	 *
	 * For example:
	 * <code>
	 * SwatError::setDisplayer(new SilverorangeDisplayer());
	 * </code>
	 *
	 * @param SwatErrorDisplayer $displayer the object to use to display
	 *                                           exceptions.
	 */
	public static function setDisplayer(SwatErrorDisplayer $displayer)
	{
		self::$displayer = $displayer;
	}

	// }}}
	// {{{ public static function setFatalSeverity()

	/**
	 * Sets the severity of SwatError that should be fatal
	 *
	 * @param integer $severity a bitwise combination of PHP error severities
	 *                           to be considered fatal.
	 */
	public static function setFatalSeverity($severity)
	{
		self::$fatal_severity = $severity;
	}

	// }}}
	// {{{ public function __construct()

	/**
	 * Creates a new error object
	 *
	 * Error objects contain methods to display and log all types of errors
	 * that may occur.
	 *
	 * @param integer $severity the error code of this error. This should
	 *                           be one of the E_* constants set by PHP. See
	 *                           {@link
	 *                           http://php.net/manual/en/ref.errorfunc.php
	 *                           Error Handling and Logging Functions}.
	 * @param string $message the error message of this error.
	 * @param string $file the name of the file this error occurred in.
	 * @param integer $line the line number this error occurred at.
	 */
	public function __construct($severity, $message, $file, $line)
	{
		$backtrace = debug_backtrace();
		// remove this method call and the handle() call from the backtrace
		array_shift($backtrace);
		array_shift($backtrace);

		$this->message = $message;
		$this->severity = $severity;
		$this->file = $file;
		$this->line = $line;
		$this->backtrace = &$backtrace;
	}

	// }}}
	// {{{ public function process()

	/**
	 * Processes this error
	 *
	 * Processing involves displaying errors, logging errors and sending
	 * error message emails.
	 *
	 * If a fatal error has occured, this calls exit to ensure no further
	 * processing is done.
	 */
	public function process()
	{
		if (ini_get('display_errors'))
			$this->display();

		if (ini_get('log_errors'))
			$this->log();

		if ($this->severity & self::$fatal_severity)
			exit(1);
	}

	// }}}
	// {{{ public function getMessage()

	/**
	 * Gets the original message string of this error
	 *
	 * @return string original message of this error.
	 */
	public function getMessage()
	{
		return $this->message;
	}

	// }}}
	// {{{ public function getSeverity()

	/**
	 * Gets the severity of this error
	 *
	 * @return integer severity value as E_* PHP constant.
	 */
	public function getSeverity()
	{
		return $this->severity;
	}

	// }}}
	// {{{ public function log()

	/**
	 * Logs this error
	 *
	 * The error is logged to the webserver error log.
	 */
	public function log()
	{
		if (self::$logger === null) {
			error_log($this->getSummary(), 0);
		} else {
			$logger = self::$logger;
			$logger->log($this);
		}
	}

	// }}}
	// {{{ public function display()

	public function display()
	{
		if (self::$displayer === null) {
			if (isset($_SERVER['REQUEST_URI']))
				echo $this->toXHTML();
			else
{'fraction_non_alphanumeric': 0.12479015109121433, 'fraction_numerical': 0.0026114530871106136, 'mean_word_length': 2.831011075384066, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '871733', 'n_tokens_mistral': 3520, 'n_tokens_neox': 3148, 'n_words': 1064}