<commit_before><?php
class Debug {
	static protected $reporting = E_ALL;
	static public    function reporting ( $reporting = null ) {
		if ( ! func_num_args() )
			return self::$reporting;
		self::$reporting = $reporting;
	}
	static public    function log () {
		self::$reporting && 
			print( 
				PHP_EOL . '<pre class="debug log">'
				. implode( 
					'</pre>' . PHP_EOL . '<pre class="log">' 
					, array_map( 'Debug::var_export', func_get_args() )
				)
				. '</pre>'
			);
	}
	static public    function dump () {
		self::$reporting && 
			die( call_user_func_array( 'Debug::log', func_get_args() ) );
	}
	static private   $time;
	static private   $chrono;
	static public    function chrono ( $print = null, $scope = '' ) {
		if ( ! self::$reporting )
			return;
		if ( ! isset( self::$time[ $scope ] ) )
			$chrono [] = '<b class="init">' . $scope . ' chrono init</b>';
		elseif ( is_string( $print ) ) {
			$chrono[] = sprintf('<span class="time">%s -> %s: %fs</span>'
				, $scope
				, $print
				, round( self::$chrono[ $scope ][ $print ] = microtime( true ) - self::$time[ $scope ], 6 )
			);
		} elseif ( $print && isset( self::$chrono[ $scope ] ) ) {
			asort( self::$chrono[ $scope ] );
			$base = reset ( self::$chrono[ $scope ] ); // shortest duration
			foreach( self::$chrono[ $scope ] as $event => $duration )
				$table[] = sprintf( '%5u - %-38.38s <i>%7fs</i>'
					, round( $duration / $base, 2 )
					, $event
					, round( $duration, 3 )
				);
			$chrono[] = '<div class="table"><b>' . $scope . ' chrono table</b>' . PHP_EOL .
				sprintf( '%\'-61s %-46s<i>duration</i>%1$s%1$\'-61s'
					, PHP_EOL
					, 'unit - action'
				) . 
				implode( PHP_EOL, $table ) . PHP_EOL . 
				'</div>';
		}
		echo self::style(), PHP_EOL, '<pre class="debug chrono">', implode( PHP_EOL, $chrono ), '</pre>';
		return self::$time[ $scope ] = microtime( true );
	}
	static private   $registered;
	static public    function register ( $init = true ) {
		if ( $init) {
			if ( ! self::$registered )
				self::$registered = array(
					'error_reporting'  => error_reporting()
					, 'display_errors' => ini_get( 'display_errors' )
					, 'shutdown'       => register_shutdown_function( 'Debug::shutdown' )
				);
			self::$registered[ 'shutdown' ] = true;
			error_reporting( E_ALL );
			set_error_handler( 'Debug::handler', E_ALL );
			set_exception_handler( 'Debug::exception' );
			ini_set( 'display_errors', 0 );
		} else if ( self::$registered ) {
			self::$registered[ 'shutdown' ] = false;
			error_reporting( self::$registered[ 'error_reporting' ] );
			restore_error_handler();
			restore_exception_handler();
			ini_set( 'display_errors', self::$registered[ 'display_errors' ] );
		}
	}
	static protected $error     = array(
		-1                    => 'Exception'
		, E_ERROR             => 'Fatal'
		, E_RECOVERABLE_ERROR => 'Recoverable'
		, E_WARNING           => 'Warning'
		//, E_PARSE             => 'Parse'
		, E_NOTICE            => 'Notice'
		, E_STRICT            => 'Strict'
		, E_DEPRECATED        => 'Deprecated'
		, E_CORE_ERROR        => 'Fatal'
		, E_CORE_WARNING      => 'Warning'
		//, E_COMPILE_ERROR     => 'Compile Fatal'
		//, E_COMPILE_WARNING   => 'Compile Warning'
		, E_USER_ERROR        => 'Fatal'
		, E_USER_WARNING      => 'Warning'
		, E_USER_NOTICE       => 'Notice'
		, E_USER_DEPRECATED   => 'Deprecated'
	);
	static public    function handler ( $type, $message, $file, $line, $scope, $stack = null ) {
		global $php_errormsg; // set global error message regardless track errors settings
		$php_errormsg = preg_replace( '~^.*</a>\]: +(?:\([^\)]+\): +)?~', null, $message ); // clean useless infos
		if ( ! self::$reporting ) // de-activate
			return false;
		$stack = $stack ?: array_slice( debug_backtrace( false ),  $type & E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE ?  2 : 1 ); // clean stack depending if error is user triggered or not
		self::overload( $stack, $file, $line  ); // switch line & file if overloaded method triggered the error
		echo self::style(), PHP_EOL, '<pre class="debug error ',strtolower( self::$error[ $type ] ) ,'">', PHP_EOL, 
			sprintf( '<b>%s</b>: %s in <b>%s</b> on line <b>%s</b>'  // print error
				, self::$error[ $type ] ?: 'Error'
				, $php_errormsg
				, $file
				, $line
			);
		if ( $type & self::$reporting ) // print context
			echo self::context( $stack, $scope );
		echo '</pre>';
		if ( $type & E_USER_ERROR ) // fatal
			exit;
	}
	static public    function shutdown () {
		if ( self::$registered[ 'shutdown' ] && ( $error = error_get_last() ) && ( $error[ 'type' ] & E_ERROR |  E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR ) )
			self::handler( $error[ 'type' ], $error[ 'message' ], $error[ 'file' ], $error[ 'line' ], null );
	}
	static public    function exception ( Exception $exception ) {
		$msg = sprintf( '"%s" with message "%s"', get_class( $exception ), $exception->getMessage() );
		self::handler( -1, $msg, $exception->getFile(), $exception->getLine(), null, $exception->getTrace() );
	}
	static public    $style     = array(
		'debug'         => 'font-size:1em'
		, 'error'       => 'background:#eee;padding:.5em'
		, 'exception'   => 'color:#825'
		//, 'parse'       => 'color:#F07'
		//, 'compile'     => 'color:#F70'
		, 'fatal'       => 'color:#F00'
		, 'recoverable' => 'color:#F22'
		, 'warning'     => 'color:#E44'
		, 'notice'      => 'color:#E66'
		, 'deprecated'  => 'color:#F88'
		, 'strict'      => 'color:#FAA'
		, 'stack'       => 'padding:.2em .8em;color:#444'
		, 'trace'       => 'border-left:1px solid #ccc;padding-left:1em'
		, 'scope'       => 'padding:.2em .8em;color:#666'
		, 'var'         => 'padding-left:1em'
		, 'chrono'      => 'border-left:2px solid #ccc'
		, 'init'        => 'color:#4A6'
		, 'time'        => 'color:#284'
		, 'table'       => 'color:#042'
	);
	static protected function style () {
		static $style;
		if ( $style )
			return;
		foreach ( self::$style as $class => $css )
			$style .= sprintf( '.%s{%s}', $class, $css );
		return PHP_EOL . '<style type="text/css">' . $style . '</style>';<commit_msg>Added log style<commit_after><?php
class Debug {
	static protected $reporting = E_ALL;
	static public    function reporting ( $reporting = null ) {
		if ( ! func_num_args() )
			return self::$reporting;
		self::$reporting = $reporting;
	}
	static public    function log () {
		self::$reporting && 
			print( 
				PHP_EOL . '<pre class="debug log">'
				. implode( 
					'</pre>' . PHP_EOL . '<pre class="log">' 
					, array_map( 'Debug::var_export', func_get_args() )
				)
				. '</pre>'
			);
	}
	static public    function dump () {
		self::$reporting && 
			die( call_user_func_array( 'Debug::log', func_get_args() ) );
	}
	static private   $time;
	static private   $chrono;
	static public    function chrono ( $print = null, $scope = '' ) {
		if ( ! self::$reporting )
			return;
		if ( ! isset( self::$time[ $scope ] ) )
			$chrono [] = '<b class="init">' . $scope . ' chrono init</b>';
		elseif ( is_string( $print ) ) {
			$chrono[] = sprintf('<span class="time">%s -> %s: %fs</span>'
				, $scope
				, $print
				, round( self::$chrono[ $scope ][ $print ] = microtime( true ) - self::$time[ $scope ], 6 )
			);
		} elseif ( $print && isset( self::$chrono[ $scope ] ) ) {
			asort( self::$chrono[ $scope ] );
			$base = reset ( self::$chrono[ $scope ] ); // shortest duration
			foreach( self::$chrono[ $scope ] as $event => $duration )
				$table[] = sprintf( '%5u - %-38.38s <i>%7fs</i>'
					, round( $duration / $base, 2 )
					, $event
					, round( $duration, 3 )
				);
			$chrono[] = '<div class="table"><b>' . $scope . ' chrono table</b>' . PHP_EOL .
				sprintf( '%\'-61s %-46s<i>duration</i>%1$s%1$\'-61s'
					, PHP_EOL
					, 'unit - action'
				) . 
				implode( PHP_EOL, $table ) . PHP_EOL . 
				'</div>';
		}
		echo self::style(), PHP_EOL, '<pre class="debug chrono">', implode( PHP_EOL, $chrono ), '</pre>';
		return self::$time[ $scope ] = microtime( true );
	}
	static private   $registered;
	static public    function register ( $init = true ) {
		if ( $init) {
			if ( ! self::$registered )
				self::$registered = array(
					'error_reporting'  => error_reporting()
					, 'display_errors' => ini_get( 'display_errors' )
					, 'shutdown'       => register_shutdown_function( 'Debug::shutdown' )
				);
			self::$registered[ 'shutdown' ] = true;
			error_reporting( E_ALL );
			set_error_handler( 'Debug::handler', E_ALL );
			set_exception_handler( 'Debug::exception' );
			ini_set( 'display_errors', 0 );
		} else if ( self::$registered ) {
			self::$registered[ 'shutdown' ] = false;
			error_reporting( self::$registered[ 'error_reporting' ] );
			restore_error_handler();
			restore_exception_handler();
			ini_set( 'display_errors', self::$registered[ 'display_errors' ] );
		}
	}
	static protected $error     = array(
		-1                    => 'Exception'
		, E_ERROR             => 'Fatal'
		, E_RECOVERABLE_ERROR => 'Recoverable'
		, E_WARNING           => 'Warning'
		//, E_PARSE             => 'Parse'
		, E_NOTICE            => 'Notice'
		, E_STRICT            => 'Strict'
		, E_DEPRECATED        => 'Deprecated'
		, E_CORE_ERROR        => 'Fatal'
		, E_CORE_WARNING      => 'Warning'
		//, E_COMPILE_ERROR     => 'Compile Fatal'
		//, E_COMPILE_WARNING   => 'Compile Warning'
		, E_USER_ERROR        => 'Fatal'
		, E_USER_WARNING      => 'Warning'
		, E_USER_NOTICE       => 'Notice'
		, E_USER_DEPRECATED   => 'Deprecated'
	);
	static public    function handler ( $type, $message, $file, $line, $scope, $stack = null ) {
		global $php_errormsg; // set global error message regardless track errors settings
		$php_errormsg = preg_replace( '~^.*</a>\]: +(?:\([^\)]+\): +)?~', null, $message ); // clean useless infos
		if ( ! self::$reporting ) // de-activate
			return false;
		$stack = $stack ?: array_slice( debug_backtrace( false ),  $type & E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE ?  2 : 1 ); // clean stack depending if error is user triggered or not
		self::overload( $stack, $file, $line  ); // switch line & file if overloaded method triggered the error
		echo self::style(), PHP_EOL, '<pre class="debug error ',strtolower( self::$error[ $type ] ) ,'">', PHP_EOL, 
			sprintf( '<b>%s</b>: %s in <b>%s</b> on line <b>%s</b>'  // print error
				, self::$error[ $type ] ?: 'Error'
				, $php_errormsg
				, $file
				, $line
			);
		if ( $type & self::$reporting ) // print context
			echo self::context( $stack, $scope );
		echo '</pre>';
		if ( $type & E_USER_ERROR ) // fatal
			exit;
	}
	static public    function shutdown () {
		if ( self::$registered[ 'shutdown' ] && ( $error = error_get_last() ) && ( $error[ 'type' ] & E_ERROR |  E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR ) )
			self::handler( $error[ 'type' ], $error[ 'message' ], $error[ 'file' ], $error[ 'line' ], null );
	}
	static public    function exception ( Exception $exception ) {
		$msg = sprintf( '"%s" with message "%s"', get_class( $exception ), $exception->getMessage() );
		self::handler( -1, $msg, $exception->getFile(), $exception->getLine(), null, $exception->getTrace() );
	}
	static public    $style     = array(
		'debug'         => 'font-size:1em'
		, 'error'       => 'background:#eee;padding:.5em'
		, 'exception'   => 'color:#825'
		//, 'parse'       => 'color:#F07'
		//, 'compile'     => 'color:#F70'
		, 'fatal'       => 'color:#F00'
		, 'recoverable' => 'color:#F22'
		, 'warning'     => 'color:#E44'
		, 'notice'      => 'color:#E66'
		, 'deprecated'  => 'color:#F88'
		, 'strict'      => 'color:#FAA'
		, 'stack'       => 'padding:.2em .8em;color:#444'
		, 'trace'       => 'border-left:1px solid #ccc;padding-left:1em'
		, 'scope'       => 'padding:.2em .8em;color:#666'
		, 'var'         => 'padding-left:1em'
		, 'log'         => 'border-bottom:1px dashed #999'
		, 'chrono'      => 'border-left:2px solid #ccc'
		, 'init'        => 'color:#4A6'
		, 'time'        => 'color:#284'
		, 'table'       => 'color:#042'
	);
	static protected function style () {
		static $style;
		if ( $style )
			return;
		foreach ( self::$style as $class => $css )
			$style .= sprintf( '.%s{%s}', $class, $css );
		return PHP_EOL . '<style type="text/css">' . $style . '</style>';
{'fraction_non_alphanumeric': 0.19564681724845995, 'fraction_numerical': 0.010677618069815195, 'mean_word_length': 2.9353587588881704, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 152, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7598349', 'n_tokens_mistral': 4895, 'n_tokens_neox': 4361, 'n_words': 959}