<?php
/**
 * @package		OpenCart
 * @author		Daniel Kerr
 * @copyright	Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
 * @license		https://opensource.org/licenses/GPL-3.0
 * @link		https://www.opencart.com
*/

/**
* Log class
*/
class Log {
	private $handle;
	private $filename;
	
	/**
	 * Constructor
	 *
	 * @param	string	$filename
 	*/
	public function __construct($filename) {
		$this->filename = $filename;
		$path = (defined('DIR_LOGS') ? DIR_LOGS : '') . $filename;
		$this->handle = @fopen($path, 'a');
	}
	
	/**
     * 
     *
     * @param	string	$message
     */
	public function write($message) {
		// Handle açılmadıysa tekrar dene (izin / path sorunlarında warning basmasın)
		if (!is_resource($this->handle)) {
			$path = (defined('DIR_LOGS') ? DIR_LOGS : '') . $this->filename;
			$this->handle = @fopen($path, 'a');
		}

		$line = date('Y-m-d G:i:s') . ' - ' . print_r($message, true) . "\n";

		if (is_resource($this->handle)) {
			@fwrite($this->handle, $line);
		} else {
			// Son çare: PHP error_log'a düş
			error_log($line);
		}
	}
	
	/**
     * 
     *
     */
	public function __destruct() {
		if (is_resource($this->handle)) {
			@fclose($this->handle);
		}
	}
}