<commit_before><?php

/**
   Copyright 2012-2013 Brainsware

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/

namespace Bacon;

/**
 * Bacon messages and log handler
 *
 * This class handles the logging (and display) of info and error messages
 *
 * @package System
 */
class Log
{
	const SYSLOG     = 0; // Log by writing to syslog
	const FILESYSTEM = 1; // Log by writing into a file
	const STDERR     = 2; // Log using error_log

	const ERROR   = 0;
	const WARNING = 1;
	const INFO    = 2;
	const DEBUG   = 3;

	private $levels = [
		'error'   => 0,
		'warning' => 1,
		'info'    => 2,
		'debug'   => 3,
	];

	protected $file;
	protected $driver;
	
	public function __construct ($level = self::INFO, $driver = self::FILESYSTEM, $output_file = null)
	{
		$this->levels = A($this->levels);

		if (is_an_array($level)) {
			$options     = $level;
			$level       = $options->level;
			$driver      = $options->driver;
			$output_file = $options->file;
		}

		if (defined('self::' . strtoupper($driver))) {
			$this->driver = constant('self::' . strtoupper($driver));
		} else {
			$this->driver = self::STDERR;
		}

		if (defined('self::' . strtoupper($level))) {
			$this->level = constant('self::' . strtoupper($level));
		} else {
			$this->level = self::INFO;
		}

		if ($this->driver == self::FILESYSTEM) {
			if ($output_file === null) {
				$this->filename = \Sauce\Path::join(APP_ROOT, 'logs', 'application.log');
			} else {
				$this->filename = $output_file;
			}

			$this->open();
		}
	}

	public function __destruct ()
	{
		if (isset($this->file)) {
			fclose($this->file);
		}
	}

	public function __call ($level, $messages)
	{
		$messages = V($messages);

		if (!$this->levels->keys()->includes(strtolower($level))) {
			throw new \BadMethodCallException("Method not found: {$level} (" . $messages->join(', ') . ")");
		}

		$level = $this->levels[strtolower($level)];

		if ($level > $this->level) { return; }

		foreach ($messages as $message) {
			if (is_an_array($message)) {
				$message = var_export($message, true);
			}

			$this->_write($level, $message);
		}
	}

	protected function _write ($level, $message)
	{
		$caller = self::caller();
		$level = strtoupper($this->levels->keys()[$level]);

		$str = sprintf("[%s %s#%s] %s", $level, $caller->class, $caller->method, $message);

		switch ($this->driver) {
			case self::SYSLOG:
				syslog($this->levels[strtolower($level)], sprintf("%s %s", date(DATE_ATOM), $str));
				break;

			case self::FILESYSTEM:
				if (!is_resource($this->file)) { $this->open(); }

				fwrite($this->file, sprintf("%s\n", $str));
				break;

			default:
			case self::STDERR:
				error_log($str);
				break;
		}
	}

	/**
	 * Backtracing
	 *
	 * Gets caller for logging info
	 *
	 * @return string Either the classname or filename of the caller.
	 */<commit_msg>added stdout for logging + timestamp<commit_after><?php

/**
   Copyright 2012-2013 Brainsware

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/

namespace Bacon;

/**
 * Bacon messages and log handler
 *
 * This class handles the logging (and display) of info and error messages
 *
 * @package System
 */
class Log
{
	const SYSLOG     = 0; // Log by writing to syslog
	const FILESYSTEM = 1; // Log by writing into a file
	const STDERR     = 2; // Log using error_log
	const STDOUT     = 3; // Log using echo

	const ERROR   = 0;
	const WARNING = 1;
	const INFO    = 2;
	const DEBUG   = 3;

	private $levels = [
		'error'   => 0,
		'warning' => 1,
		'info'    => 2,
		'debug'   => 3,
	];

	protected $file;
	protected $driver;
	
	public function __construct ($level = self::INFO, $driver = self::FILESYSTEM, $output_file = null)
	{
		$this->levels = A($this->levels);

		if (is_an_array($level)) {
			$options     = $level;
			$level       = $options->level;
			$driver      = $options->driver;
			$output_file = $options->file;
		}

		if (defined('self::' . strtoupper($driver))) {
			$this->driver = constant('self::' . strtoupper($driver));
		} else {
			$this->driver = self::STDERR;
		}

		if (defined('self::' . strtoupper($level))) {
			$this->level = constant('self::' . strtoupper($level));
		} else {
			$this->level = self::INFO;
		}

		if ($this->driver == self::FILESYSTEM) {
			if ($output_file === null) {
				$this->filename = \Sauce\Path::join(APP_ROOT, 'logs', 'application.log');
			} else {
				$this->filename = $output_file;
			}

			$this->open();
		}
	}

	public function __destruct ()
	{
		if (isset($this->file)) {
			fclose($this->file);
		}
	}

	public function __call ($level, $messages)
	{
		$messages = V($messages);

		if (!$this->levels->keys()->includes(strtolower($level))) {
			throw new \BadMethodCallException("Method not found: {$level} (" . $messages->join(', ') . ")");
		}

		$level = $this->levels[strtolower($level)];

		if ($level > $this->level) { return; }

		foreach ($messages as $message) {
			if (is_an_array($message)) {
				$message = var_export($message, true);
			}

			$this->_write($level, $message);
		}
	}

	protected function _write ($level, $message)
	{
		$caller = self::caller();
		$level = strtoupper($this->levels->keys()[$level]);

		$str = sprintf("%s [%s %s#%s] %s", date(DATE_ATOM), $level, $caller->class, $caller->method, $message);

		switch ($this->driver) {
			case self::SYSLOG:
				syslog($this->levels[strtolower($level)], sprintf("%s", $str));
				break;

			case self::FILESYSTEM:
				if (!is_resource($this->file)) { $this->open(); }

				fwrite($this->file, sprintf("%s\n", $str));
				break;

			case self::STDOUT:
				echo($str);
				break;

			default:
			case self::STDERR:
				error_log($str);
				break;
		}
	}

	/**
	 * Backtracing
	 *
	 * Gets caller for logging info
	 *
	 * @return string Either the classname or filename of the caller.
	 */
{'fraction_non_alphanumeric': 0.16489204377403135, 'fraction_numerical': 0.006950606329488317, 'mean_word_length': 3.234815278647464, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, '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': '574947', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2233, 'n_words': 620}