<commit_before><?php namespace CodeIgniter\Log\Handlers;

use CodeIgniter\HTTP\RequestInterface;

/**
 * Class ChromeLoggerHandler
 *
 * Allows for logging items to the Chrome console for debugging.
 * Requires the ChromeLogger extension installed in your browser.
 *
 * @see https://craig.is/writing/chrome-logger
 *
 * @package CodeIgniter\Log\Handlers
 */
class ChromeLoggerHandler extends BaseHandler implements HandlerInterface
{
	/**
	 * Version of this library - for ChromeLogger use.
	 *
	 * @var float
	 */
	const VERSION = 1.0;

	/**
	 * The number of strack frames returned from the backtrace.
	 *
	 * @var int
	 */
	protected $backtraceLevel = 0;

	/**
	 * The final data that is sent to the browser.
	 *
	 * @var array
	 */
	protected $json = [
		'version' => self::VERSION,
		'columns' => ['log', 'backtrace', 'type'],
		'rows'    => [],
	];

	/**
	 * The header used to pass the data.
	 *
	 * @var string
	 */
	protected $header = 'X-ChromeLogger-Data';

	/**
	 * Maps the log levels to the ChromeLogger types.
	 *
	 * @var array
	 */
	protected $levels = [
		'emergency' => 'error',
		'alert'     => 'error',
		'critical'  => 'error',
		'error'     => 'error',
		'warning'   => 'warn',
		'notice'    => 'warn',
		'info'      => 'info',
		'debug'     => 'info',
	];

	//--------------------------------------------------------------------

	public function __construct(array $config = [])
	{
		parent::__construct($config);

		global $request;

		$this->json['request_uri'] = (string)$request->uri;

		// @todo Tap us into the the PostController hook when we have that available.
	}

	//--------------------------------------------------------------------

	/**
	 * Handles logging the message.
	 * If the handler returns false, then execution of handlers
	 * will stop. Any handlers that have not run, yet, will not
	 * be run.
	 *
	 * @param $level
	 * @param $message
	 *
	 * @return bool
	 */
	public function handle($level, $message): bool
	{
		// Grab the app's main request object.
		global $request;

		// Format our message
		$message = $this->format($message);

		// Generate Backtrace info
		$backtrace = end(debug_backtrace(false, $this->backtraceLevel));

		$backtraceMessage = 'unknown';
		if (isset($backtrace['file']) && isset($backtrace['line']))
		{
			$backtraceMessage = $backtrace['file'].':'.$backtrace['line'];
		}

		// Default to 'log' type.
		$type = '';

		if (in_array($level, $this->levels))
		{
			$type = $this->levels[$level];
		}

		$this->rows[] = [$message, $backtraceMessage, $type];

		// Don't continue
		return false;
	}

	//--------------------------------------------------------------------

	/**<commit_msg>Caught a simple bug in ChromeLoggerHandler<commit_after><?php namespace CodeIgniter\Log\Handlers;

use CodeIgniter\HTTP\RequestInterface;

/**
 * Class ChromeLoggerHandler
 *
 * Allows for logging items to the Chrome console for debugging.
 * Requires the ChromeLogger extension installed in your browser.
 *
 * @see https://craig.is/writing/chrome-logger
 *
 * @package CodeIgniter\Log\Handlers
 */
class ChromeLoggerHandler extends BaseHandler implements HandlerInterface
{
	/**
	 * Version of this library - for ChromeLogger use.
	 *
	 * @var float
	 */
	const VERSION = 1.0;

	/**
	 * The number of strack frames returned from the backtrace.
	 *
	 * @var int
	 */
	protected $backtraceLevel = 0;

	/**
	 * The final data that is sent to the browser.
	 *
	 * @var array
	 */
	protected $json = [
		'version' => self::VERSION,
		'columns' => ['log', 'backtrace', 'type'],
		'rows'    => [],
	];

	/**
	 * The header used to pass the data.
	 *
	 * @var string
	 */
	protected $header = 'X-ChromeLogger-Data';

	/**
	 * Maps the log levels to the ChromeLogger types.
	 *
	 * @var array
	 */
	protected $levels = [
		'emergency' => 'error',
		'alert'     => 'error',
		'critical'  => 'error',
		'error'     => 'error',
		'warning'   => 'warn',
		'notice'    => 'warn',
		'info'      => 'info',
		'debug'     => 'info',
	];

	//--------------------------------------------------------------------

	public function __construct(array $config = [])
	{
		parent::__construct($config);

		global $request;

		$this->json['request_uri'] = (string)$request->uri;

		// @todo Tap us into the the PostController hook when we have that available.
	}

	//--------------------------------------------------------------------

	/**
	 * Handles logging the message.
	 * If the handler returns false, then execution of handlers
	 * will stop. Any handlers that have not run, yet, will not
	 * be run.
	 *
	 * @param $level
	 * @param $message
	 *
	 * @return bool
	 */
	public function handle($level, $message): bool
	{
		// Grab the app's main request object.
		global $request;

		// Format our message
		$message = $this->format($message);

		// Generate Backtrace info
		$backtrace = end(debug_backtrace(false, $this->backtraceLevel));

		$backtraceMessage = 'unknown';
		if (isset($backtrace['file']) && isset($backtrace['line']))
		{
			$backtraceMessage = $backtrace['file'].':'.$backtrace['line'];
		}

		// Default to 'log' type.
		$type = '';

		if (in_array($level, $this->levels))
		{
			$type = $this->levels[$level];
		}

		$this->json['rows'][] = [$message, $backtraceMessage, $type];

		// Don't continue
		return false;
	}

	//--------------------------------------------------------------------

	/**
{'fraction_non_alphanumeric': 0.22358859698155395, 'fraction_numerical': 0.0011179429849077697, 'mean_word_length': 2.3054187192118225, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 2, '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': '2001590', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1575, 'n_words': 453}