<?php
require_once(LIBDIR."/mvcfronk/mfLog/mfLog.php");

class mfLog_File extends mfLog {
	private $logfile;
	private $fh;

	private $instance;
	
	public function init($logpath = false) {
    if($logpath) {
      $this->logfile = $logpath;
    } elseif(defined('LOGFILENAME')) {
      $this->logfile = LOGFILENAME;
		} else {
			//if no Logfile is defined, try to create it in current working directory
			$this->logfile = realpath("./");
			$this->logfile .= "/log";
		}

		if(!$this->openLogFile()) {
			throw new Exception(__CLASS__.": Cannot open Logfile ".$this->logfile);
		}
	}
	
	public function openLogFile() {
		if(!$this->logfile) {
			return false;
		}
		
		$fh=fopen($this->logfile,"a");
		if($fh) {
			$this->fh=$fh;
			return true;
		}
		return false;
	}
	
	public function write($message,$severity="info") {
		if(!$this->fh) {
			return false;
		}
		
		if(!in_array($severity,$this->severity)) {
			$severity="warn";
		}
		
		$time=date('Y-m-d H:i:s');
		$string="$time [$severity] $message";
		
		if(!fwrite($this->fh,$string."\n")) {
			echo "Cannot write to logfile (".$this->logfile.")!";
			return false;
		}
		
		return true;
	}
  
  public function __call($severity,$message) {
		$this->write($message[0],$severity);
	}
}