<?php
/**
 * This file is part of the Logger library and defines the {@link FileLogger}
 * concrete class.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package Logger
 * @version $Revision: 1.5 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */

require_once(dirname(__FILE__) . '/../../interface/logger.php');
require_once(dirname(__FILE__) . '/../../interface/message.php');

/**
 * The FileLogger class is a concrete implementation of the abstract
 * {@link Logger} class that stores log messages in files.
 * 
 * Each message ends up in a file named after the level of the message.
 * 
 * 3 levels of messages are maintained by default:
 * - info: normal behavior information
 * - warning: problems that may be worth noting but are ignored by the method
 * that logs the message
 * - error: problems that lead to an error in the application.
 * 
 * The class requires the existence of a "log" subfolder, and needs to have
 * write-access to that subfolder.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package Logger
 * @version $Revision: 1.5 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 *
 */
class FileLogger extends Logger {
	/**
	 * @var the path of the log folder
	 */
	private static $source;
	
	/**
	 * Builds the logger by setting the source folder and the supported 
	 * levels.
	 */
	public function __construct(){
		if(!isset(self::$source)){
			self::$source = dirname(__FILE__).'/log/';
		}
		$this->levels = array(
				'info',
				'warning',
				'error'
			);
	}
	
	/**
	 * Adds a message to the set of message
	 * 
	 * @param $message_content the content of a message in a string.
	 * @param $level the level of the message (by default : warning).
	 */
	function add_message($message_content, $level='warning'){
		if(!in_array($level, $this->levels)){
			$message = new Message("Fail to wite a log message : the '.$level.' level doesn't exist. Original message was : ".$message_content, 'warning');
			$level = 'warning';
		}
		else{
			$message = new Message($message_content, $level);
		}
		$handle = @fopen(self::$source.$level.'.log', 'a');
		@fwrite($handle, $message->get_message()."\n");
		@fclose($handle);
	}
	
	/**
	 * Retirves all messages to the set of messages which are in
	 * the given level. 
	 * 
	 * @param $level_value the level to search messages. If 'all', 
	 * retrieve all the messages. By default, the value is 'all'.
	 * @return a set of Message object.
	 */
	function retrieve_messages($level_value='all'){
		$messages = array();
		$files = array();
		if($level_value == 'all'){
			foreach($this->levels as $level){
				$files[$level] = self::$source . $level . '.log';
			}
		}
		else{
			$files[$level_value] = self::$source . $level_value . '.log';
		}
		
		foreach($files as $level=>$file){
			if(file_exists($file)){
				$handle = fopen($file, 'r');
				while(!feof($handle)){
					$buffer = fgets($handle, 4096);
					
					$date = substr($buffer, 0, 31);
			        $content = substr($buffer, 34);
			        
			        $message = new Message($content, $level);
			        $message->set_date($date);
			        
			        $messages[] = $message;
				}
				fclose($handle);
			}
		}
		
		return $messages;
	}

	/**
	 * Clears all messages to the set of messages which are in the
	 * given level.
	 * 
	 * @param $level_value the level to search messages. If 'all', 
	 * retrieve all the messages. By default, the value is 'all'.
	 */
	function clear_messages($level_value='all'){
		$files = array();
		if($level_value == 'all'){
			foreach($this->levels as $level){
				$files[] = self::$source . $level . '.log';
			}
		}
		else{
			$files[] = self::$source . $level_value . '.log';
		}
		
		foreach($files as $file){
			if(file_exists($file)){
				@unlink($file);
			}
		}
	}
}


?>