<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

class Logger {

	private $log_file_name = 'log.csv';
	private $log_file_path = 'tmp/';
	private $log_file_handler;
	private $full_path;

	private static $instance = false;

	protected function __construct() {
		$this->full_path = './'.$this->log_file_path.$this->log_file_name;

		$this->log_file_handler = fopen($this->full_path, 'a') or die;
	}

	function __destruct() {
		fclose($this->log_file_handler);
	}

	private function __clone() {}

	public static function get_instance() {
		if (self::$instance) {
			return self::$instance;
		} else {
			self::$instance = new Logger();

			return self::$instance;
		}
	}

	public function info($file, $message) {
		$info = 'info; '.SHOP_SYSTEM.'; '.date("y-m-d; H:i:s",time()).'; '.$file.': '.$message."\n";
		fwrite($this->log_file_handler, $info);
	}

	public function warning($file, $message) {
		$warning = 'warning; '.SHOP_SYSTEM.'; '.date("y-m-d; H:i:s",time()).'; '.$file.': '.$message."\n";
		fwrite($this->log_file_handler, $warning);
	}

	public function error($file, $message) {
		$error = 'error; '.SHOP_SYSTEM.'; '.date("y-m-d; H:i:s",time()).'; '.$file.': '.$message."\n";
		fwrite($this->log_file_handler, $error);
	}

	public function dump_var($file, $var) {
		$dump_var = 'dump; '.SHOP_SYSTEM.'; '.date("y-m-d; H:i:s",time()).'; '.$file.': '.$var."\n";
		fwrite($this->log_file_handler, $dump_var);
	}

	public function dump_array($file, $array) {
		$dump_array = 'dump; '.SHOP_SYSTEM.'; '.date("y-m-d; H:i:s",time()).'; '.$file.': '.implode(",", $array)."\n";
		fwrite($this->log_file_handler, $dump_array);
	}

	public function print_log_file() {
		return $this->full_path;
	}

	public function get_last_log() {
		$text = '';
		$lines = file ($this->full_path);
		$start = 0;
		if (count($lines) < 10) {
			$stop = count($lines)-1;
		} else {
			$start = count($lines) - 11;
			$stop = count($lines)-1;
		}
		for ($i = $start;$i <= $stop; $i++) {
			$text .= $lines[$i].'<br>';
		}

		return $text;
	}
}
