<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\Core;

class LogManager
{
	/**
	 * The Bot object. Used to interact with the main thread.
	 * @var object
	 */
	protected $bot;

	/**
	 * Use a buffer to temporarily store data in. Useful on systems with slow disk access.
	 * @var bool
	 */
	private $useBuffer = false;

	/**
	 * The buffer itself.
	 * @var string
	 */
	private $buffer = '';

	/**
	 * The flush interval, in seconds.
	 * @var int
	 */
	private $flushInterval = 600;

	/**
	 * The time the buffer was last flushed.
	 * @var int
	 */
	private $lastFlushed = 0;

	/**
	 * 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 = '';

	/**
	 * Set up the class.
	 * @param array $logDir The
	 */
	public function __construct($bot, $logDir = WPHP_LOG_DIR)
	{
		// Fetch the configuration.
		$config = $bot->getConfiguration('log');

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

		// Also can't log to a directory we can't write to.
		if (!is_writable($logDir))
			trigger_error('LogManager: A log file cannot be created in the set directory (' . $logDir . '). Please make it writable. Aborting.', E_USER_ERROR);

		// 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'))<commit_msg>Added autocreation of log dir to the bot. Improved error checking.<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\Core;

class LogManager
{
	/**
	 * The Bot object. Used to interact with the main thread.
	 * @var object
	 */
	protected $bot;

	/**
	 * Use a buffer to temporarily store data in. Useful on systems with slow disk access.
	 * @var bool
	 */
	private $useBuffer = false;

	/**
	 * The buffer itself.
	 * @var string
	 */
	private $buffer = '';

	/**
	 * The flush interval, in seconds.
	 * @var int
	 */
	private $flushInterval = 600;

	/**
	 * The time the buffer was last flushed.
	 * @var int
	 */
	private $lastFlushed = 0;

	/**
	 * 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 = '';

	/**
	 * Set up the class.
	 * @param array $logDir The
	 */
	public function __construct($bot, $logDir = WPHP_LOG_DIR)
	{
		// Fetch the configuration.
		$config = $bot->getConfiguration('log');

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

		// 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'))
{'fraction_non_alphanumeric': 0.11695259177273566, 'fraction_numerical': 0.004427227448810182, 'mean_word_length': 3.0342261904761907, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '2838065', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1594, 'n_words': 701}