<?php
/* Logger class - basic plain text file logger with permission checking and verbosity levels.  

Set DEBUG level as follow:
DEBUG 	= 1;	// Most Verbose
INFO 	= 2;	// ...
WARN 	= 3;	// ...
ERROR 	= 4;	// ...
FATAL 	= 5;	// Least Verbose
OFF 	= 6;	// Nothing at all.

Usage: 
		$log = new KLogger ( "log.txt" , Logger::INFO );
		$log->LogInfo("Returned a million search results");	//Prints to the log file
		$log->LogFatal("Oh dear."); //Prints to the log file
		$log->LogDebug("x = 5"); //Prints nothing due to priority setting
*/

class Logger {
	
	const DEBUG = 1;
	const INFO = 2;
	const WARN = 3;
	const ERROR = 4;
	const FATAL = 5;
	const OFF = 6;
	
	const LOG_OPEN = 1;
	const OPEN_FAILED = 2;
	const LOG_CLOSED = 3;
		
	var $Log_Status = Logger::LOG_CLOSED;
	var $DateFormat = "Y-m-d G:i:s";
	var $MessageQueue;
	var $log_file;
	var $priority = Logger::INFO;
	var $file_handle;
	var $last_message;
		
	function __construct($filepath, $priority) {
		
		if($priority == Logger::OFF) return;
		$this->log_file = $filepath;
		$this->MessageQueue = array();
		$this->priority = $priority;
			
		if(file_exists($this->log_file)) {
			if (!is_writable($this->log_file)) {
				$this->Log_Status = Logger::OPEN_FAILED;
				$this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
				trigger_error("The file exists, but could not be opened for writing. Check that appropriate permissions have been set.", E_USER_WARNING);
				return;
			}
		}
			
		if($this->file_handle = fopen($this->log_file, "a")) {
			$this->Log_Status = Logger::LOG_OPEN;
			$this->MessageQueue[] = "The log file was opened successfully.";
		} else {
			$this->Log_Status = Logger::OPEN_FAILED;
			$this->MessageQueue[] = "The file could not be opened. Check permissions.";
			trigger_error("The file could not be opened. Check permissions.", E_USER_WARNING);
			
		}
		
		return;
	}
		
	function __destruct() {
		
		if($this->file_handle) {
			fclose($this->file_handle);
		}
	}
	
	function GetLastMessage() {
		return $this->last_message;
	}
		
	function LogInfo($line) {
		$this->Log($line, Logger::INFO);
	}
		
	function LogDebug($line) {
		$this->Log($line, Logger::DEBUG);
	}
		
	function LogWarn($line) {
		$this->Log($line, Logger::WARN);	
	}
		
	function LogError($line) {
		$this->Log($line, Logger::ERROR);		
	}

	function LogFatal($line) {
		$this->Log($line, Logger::FATAL);
	}
		
	function Log($line, $priority) {
		if($this->priority <= $priority) {
			$status = $this->getTimeLine($priority);
			$this->WriteFreeFormLine("$status $line \n");
			$this->last_message = $line;
			//print "<br>" . $line;
			
		}
	}
	
	function WriteFreeFormLine($line) {
		if($this->Log_Status == Logger::LOG_OPEN && $this->priority != Logger::OFF) {
			if(fwrite($this->file_handle, $line) === false) {
				$this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
				trigger_error("The file could not be written to. Check that appropriate permissions have been set.", E_USER_WARNING);
			}
		}
	}
		
	function getTimeLine($level) {
			
		$time = date($this->DateFormat);
		switch($level) {
			case Logger::INFO:
				return "$time - INFO -";
			case Logger::WARN:
				return "$time - WARN -";				
			case Logger::DEBUG:
				return "$time - DEBUG -";				
			case Logger::ERROR:
				return "$time - ERROR -";
			case Logger::FATAL:
				return "$time - FATAL -";
			default:
				return "$time - LOG -";
		}
	}	
}
?>