<commit_before><?php

/**
 * This file contains a (static) class that will track some debug information
 * if debug is on.
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This file contains code covered by:
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.1 beta 1
 *
 */

if (!defined('ELK'))
	die('No access...');

/**
 * Stored debugging information.
 */
class Debug
{
	/**
	 * This is used to remember if the debug is on or off
	 * @var bool
	 */
	private $_track = true;

	/**
	 * A list of known debug entities (here to preserve a kind of order)
	 * @var mixed[]
	 */
	private $_debugs = array(
		'templates' => array(),
		'sub_templates' => array(),
		'language_files' => array(),
		'sheets' => array(),
		'javascript' => array(),
		'hooks' => array(),
		'files_included' => array(),
		'tokens' => array(),
	);

	/**
	 * Holds the output ot the getrusage php function
	 * @var mixed[]
	 */
	private $_rusage = array();

	/**
	 * Holds All the cache hits for a page load
	 * @var string[]
	 */
	private $_cache_hits = array();

	/**
	 * Number of times the cache has been used
	 * @var int
	 */
	private $_cache_count = 0;

	/**
	 * All the queries executed
	 * @var mixed[]
	 */
	private $_db_cache = array();

	/**
	 * Number of queries
	 * @var int
	 */
	private $_db_count = 0;

	/**
	 * Some generic "system" debug info
	 * @var string[]
	 */
	private $_system = array();

	/**
	 * The instance of the class
	 * @var object
	 */
	private static $_instance = null;


	/**
	 * Adds a new generic debug entry
	 *
	 * @param string $type the kind of debug entry
	 * @param string|string[] string or array of the entry to show
	 */
	public function add($type, $value)
	{
		if (!$this->_track)
			return;

		if (is_array($value))
			$this->_debugs[$type] = array_merge($this->_debugs[$type], $value);
		else
			$this->_debugs[$type][] = $value;
	}

	/**
	 * Adds a new cache hits
	 *
	 * @param mixed[] $value contains the relevant cache info, in the form:
	 *         d => method: put or get
	 *         k => cache key
	 *         t => time taken to get/put the entry
	 *         s => length of the serialized value
	 */
	public function cache($value)
	{
		if (!$this->_track)
			return;

		$this->_cache_hits[] = $value;
		$this->_cache_count++;
	}

	/**
	 * Return the number of cache hits
	 *
	 * @return int
	 */
	public function cache_count()
	{
		return $this->_cache_count;
	}

	/**
	 * Adds a new database query
	 *
	 * @param mixed[] $value contains the relevant queries info, in the form:
	 *         q => the query string (only for the first 50 queries, after that only a "...")
	 *         f => the file in which the query has been executed
	 *         l => the line at which the query has been executed
	 *         s => seconds at which the query has been executed into the request
	 *         t => time taken by the query
	 */
	public function db($value)
	{
		if (!$this->_track)
			return;

		$this->_db_cache[] = $value;
		$this->_db_count++;
	}

	/**
	 * Merges the values passed with the current database entries
	 *
	 * @param mixed[] $value An array of queries info, see the db method for details
	 */
	public function merge_db($value)
	{
		if (!$this->_track)
			return;

		$this->_db_cache = array_merge($value, $this->_db_cache);
		$this->_db_count = count($this->_db_cache) + 1;
	}

	/**
	 * Return the current database entries
	 *
	 * @return array
	 */
	public function get_db()
	{
		if (!$this->_track)
			return array();

		return $this->_db_cache;
	}

	/**
	 * Adds a new getrusage value (by default two are added: one at the beginning
	 * of the script execution and one at the end)
	 *
	 * @param string $point can be end or start depending on when the function
	 *               is called
	 * @param string[]|null $rusage value of getrusage or null to let the method call it
	 */
	public function rusage($point, $rusage = null)
	{
		// getrusage is missing in php < 7 on Windows
		if (!function_exists('getrusage') || !$this->_track)
			return;

		if ($rusage === null)
			$this->_rusage[$point] = getrusage();
		else
			$this->_rusage[$point] = $rusage;
	}

	/**
	 * Enables tracking of debug entries
	 */
	public function on()
	{
		$this->_track = true;
	}

	/**
	 * Disables tracking of debug entries
	 */<commit_msg>Invert the sequence of the rusage comparison<commit_after><?php

/**
 * This file contains a (static) class that will track some debug information
 * if debug is on.
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This file contains code covered by:
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.1 beta 1
 *
 */

if (!defined('ELK'))
	die('No access...');

/**
 * Stored debugging information.
 */
class Debug
{
	/**
	 * This is used to remember if the debug is on or off
	 * @var bool
	 */
	private $_track = true;

	/**
	 * A list of known debug entities (here to preserve a kind of order)
	 * @var mixed[]
	 */
	private $_debugs = array(
		'templates' => array(),
		'sub_templates' => array(),
		'language_files' => array(),
		'sheets' => array(),
		'javascript' => array(),
		'hooks' => array(),
		'files_included' => array(),
		'tokens' => array(),
	);

	/**
	 * Holds the output ot the getrusage php function
	 * @var mixed[]
	 */
	private $_rusage = array();

	/**
	 * Holds All the cache hits for a page load
	 * @var string[]
	 */
	private $_cache_hits = array();

	/**
	 * Number of times the cache has been used
	 * @var int
	 */
	private $_cache_count = 0;

	/**
	 * All the queries executed
	 * @var mixed[]
	 */
	private $_db_cache = array();

	/**
	 * Number of queries
	 * @var int
	 */
	private $_db_count = 0;

	/**
	 * Some generic "system" debug info
	 * @var string[]
	 */
	private $_system = array();

	/**
	 * The instance of the class
	 * @var object
	 */
	private static $_instance = null;


	/**
	 * Adds a new generic debug entry
	 *
	 * @param string $type the kind of debug entry
	 * @param string|string[] string or array of the entry to show
	 */
	public function add($type, $value)
	{
		if (!$this->_track)
			return;

		if (is_array($value))
			$this->_debugs[$type] = array_merge($this->_debugs[$type], $value);
		else
			$this->_debugs[$type][] = $value;
	}

	/**
	 * Adds a new cache hits
	 *
	 * @param mixed[] $value contains the relevant cache info, in the form:
	 *         d => method: put or get
	 *         k => cache key
	 *         t => time taken to get/put the entry
	 *         s => length of the serialized value
	 */
	public function cache($value)
	{
		if (!$this->_track)
			return;

		$this->_cache_hits[] = $value;
		$this->_cache_count++;
	}

	/**
	 * Return the number of cache hits
	 *
	 * @return int
	 */
	public function cache_count()
	{
		return $this->_cache_count;
	}

	/**
	 * Adds a new database query
	 *
	 * @param mixed[] $value contains the relevant queries info, in the form:
	 *         q => the query string (only for the first 50 queries, after that only a "...")
	 *         f => the file in which the query has been executed
	 *         l => the line at which the query has been executed
	 *         s => seconds at which the query has been executed into the request
	 *         t => time taken by the query
	 */
	public function db($value)
	{
		if (!$this->_track)
			return;

		$this->_db_cache[] = $value;
		$this->_db_count++;
	}

	/**
	 * Merges the values passed with the current database entries
	 *
	 * @param mixed[] $value An array of queries info, see the db method for details
	 */
	public function merge_db($value)
	{
		if (!$this->_track)
			return;

		$this->_db_cache = array_merge($value, $this->_db_cache);
		$this->_db_count = count($this->_db_cache) + 1;
	}

	/**
	 * Return the current database entries
	 *
	 * @return array
	 */
	public function get_db()
	{
		if (!$this->_track)
			return array();

		return $this->_db_cache;
	}

	/**
	 * Adds a new getrusage value (by default two are added: one at the beginning
	 * of the script execution and one at the end)
	 *
	 * @param string $point can be end or start depending on when the function
	 *               is called
	 * @param string[]|null $rusage value of getrusage or null to let the method call it
	 */
	public function rusage($point, $rusage = null)
	{
		// getrusage is missing in php < 7 on Windows
		if ($this->_track === false || function_exists('getrusage') === false)
		{
			return;
		}

		if ($rusage === null)
			$this->_rusage[$point] = getrusage();
		else
			$this->_rusage[$point] = $rusage;
	}

	/**
	 * Enables tracking of debug entries
	 */
	public function on()
	{
		$this->_track = true;
	}

	/**
	 * Disables tracking of debug entries
	 */
{'fraction_non_alphanumeric': 0.13828482212557153, 'fraction_numerical': 0.00312256049960968, 'mean_word_length': 2.766484670306594, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5074831', 'n_tokens_mistral': 3083, 'n_tokens_neox': 2776, 'n_words': 1022}