<commit_before><?php

/*
	WildPHP - a modular and easily extendable IRC bot written in PHP
	Copyright (C) 2015 WildPHP

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace WildPHP\LogManager;

use WildPHP\Bot;
use WildPHP\Manager;

class LogManager extends Manager
{
	/**
	 * The file handle, used for manipulating the current log file.
	 *
	 * @var resource
	 */
	private $handle = '';

	/**
	 * The path to the current log file.
	 *
	 * @var string
	 */
	private $logFile = '';

	/**
	 * What entries do we write?
	 *
	 * @var string[]
	 */
	protected $write = [];

	/**
	 * And which do we print?
	 *
	 * @var string[]
	 */
	protected $print = [];

	/**
	 * Set up the class.
	 *
	 * @param Bot    $bot    The bot object
	 * @param string $logDir The directory to store logs in
	 */
	public function __construct(Bot $bot, $logDir = WPHP_LOG_DIR)
	{
		parent::__construct($bot);

		// Make sure we end up clean when quitting.
		register_shutdown_function([$this, 'logShutdown']);

		// Fetch the configuration.
		$config = $bot->getConfig('log');

		// Set some flags.
		$this->write = $this->getConfig('log.items');
		$this->print = $this->getConfig('log.print');

		// Can't log to a file not set.
		if (empty($config['file']))
			throw new \RuntimeException('LogManager: A log file needs to be set to use logging. Aborting.');

		// Check for log dir and create it if necessary
		if (!file_exists($logDir))
			if (!mkdir($logDir, 0775))
				throw new \RuntimeException('Log directory (' . $logDir . ') does not exist. Attempt to create it failed.');

		// Check if the log dir is in fact a directory
		if (!is_dir($logDir))
			throw new \RuntimeException($logDir . ': ' . __CLASS__ . ' expected directory.');

		// Also can't log to a directory we can't write to.
		if (!is_writable($logDir))
			throw new \RuntimeException('Log directory (' . $logDir . ') has insufficient write permissions.');

		// Start off with the base path to the file.
		$this->logFile = $logDir . '/' . $config['file'];

		// Now, we're going to count up until we find a file that doesn't yet exist.
		$i = 0;
		do
		{
			$i++;
		}
		while (file_exists($this->logFile . $i . '.log'));

		// And fix up the final log name.
		$this->logFile = $this->logFile . $i . '.log';

		// Ready!
		if ($this->handle = fopen($this->logFile, 'w'))
			$this->log('Using log file ' . $this->logFile);

		// Well this went great...
		else
			trigger_error('LogManager: Cannot create file ' . $this->logFile . '. Aborting.', E_USER_ERROR);
	}

	/**
	 * Add data to the log.
	 *
	 * @param string  $message The data to log.
	 * @param boolean $print   Whether to print the data to screen.
	 * @param boolean $write   Whether to write the data to file.
	 */
	public function log($message, $print = true, $write = true)
	{
		if ($print)
		{
			// Print the message to the console.
			echo trim($message) . PHP_EOL;
		}

		// Otherwise, we can just write it.
		if ($write)
		{<commit_msg>LogManager: Clean up __construct<commit_after><?php

/*
	WildPHP - a modular and easily extendable IRC bot written in PHP
	Copyright (C) 2015 WildPHP

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace WildPHP\LogManager;

use WildPHP\Bot;
use WildPHP\Manager;

class LogManager extends Manager
{
	/**
	 * The file handle, used for manipulating the current log file.
	 *
	 * @var resource
	 */
	private $handle = '';

	/**
	 * The path to the current log file.
	 *
	 * @var string
	 */
	private $logFile = '';

	/**
	 * What entries do we write?
	 *
	 * @var string[]
	 */
	protected $write = [];

	/**
	 * And which do we print?
	 *
	 * @var string[]
	 */
	protected $print = [];

	/**
	 * Set up the class.
	 *
	 * @param Bot    $bot    The bot object
	 * @param string $logDir The directory to store logs in
	 */
	public function __construct(Bot $bot, $logDir = WPHP_LOG_DIR)
	{
		parent::__construct($bot);

		// Make sure we end up clean when quitting.
		register_shutdown_function([$this, 'logShutdown']);

		// Fetch the configuration.
		$config = $bot->getConfig('log');

		// Set some flags.
		$this->write = $this->getConfig('log.items');
		$this->print = $this->getConfig('log.print');

		// Can't log to a file not set.
		if (empty($config['file']))
			throw new \RuntimeException('LogManager: A log file needs to be set to use logging. Aborting.');

		$this->createLogFile($logDir);
	}

	/**
	 * Creates the log file.
	 *
	 * @param string $dir  The directory to store the log in.
	 */
	private function createLogFile($dir)
	{
		$this->checkLogDir($dir);
		$base = $this->getConfig('log.file');

		// Start off with the base path to the file.
		$this->logFile = $dir . '/' . $base;

		// Now, we're going to count up until we find a file that doesn't yet exist.
		$i = 0;
		do
		{
			$i++;
		}
		while (file_exists($this->logFile . $i . '.log'));

		// And fix up the final log name.
		$this->logFile = $this->logFile . $i . '.log';

		// Ready!
		if ($this->handle = fopen($this->logFile, 'w'))
			$this->log('Using log file ' . $this->logFile);

		// Well this went great...
		else
			throw new \RuntimeException('LogManager: Cannot create file ' . $this->logFile . '.');
	}

	/**
	 * Checks a directory, if it is writable etc.
	 *
	 * @param string $dir The directory to check.
	 * @throws \RuntimeException if something is wrong.
	 */
	private function checkLogDir($dir)
	{
		// Check for log dir and create it if necessary
		if (!file_exists($dir) && !mkdir($dir, 0775))
			throw new \RuntimeException('Log directory (' . $dir . ') does not exist. Attempt to create it failed.');

		// Check if the log dir is in fact a directory
		if (!is_dir($dir))
			throw new \RuntimeException($dir . ': ' . __CLASS__ . ' expected directory.');

		// Also can't log to a directory we can't write to.
		if (!is_writable($dir))
			throw new \RuntimeException('Log directory (' . $dir . ') has insufficient write permissions.');
	}

	/**
	 * Add data to the log.
	 *
	 * @param string  $message The data to log.
	 * @param boolean $print   Whether to print the data to screen.
	 * @param boolean $write   Whether to write the data to file.
	 */
	public function log($message, $print = true, $write = true)
	{
		if ($print)
		{
			// Print the message to the console.
			echo trim($message) . PHP_EOL;
		}

		// Otherwise, we can just write it.
		if ($write)
		{
{'fraction_non_alphanumeric': 0.12941176470588237, 'fraction_numerical': 0.00267379679144385, 'mean_word_length': 2.9919957310565635, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 36, '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': '4212566', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2256, 'n_words': 909}