<?php
/**
 * @author Gregor "hrax" Magdolen", magdolen [at] elepha [dot] info
 * @uses ilog Interface
 */
error_reporting(E_ALL);
require_once(
dirname(__FILE__)."/ilog.php");

/**
 * Usage:
 * $log = new Log(new LogWriter("test", dirname(__FILE__).'/'));
 * $log->logNotice("test3");
 * 
 * $no = rand(0, 5);
 * 
 * $log->recordActionTime('tesing action 1');
 * sleep($no);
 * $log->stopActionTime('tesing action 1');
 * $log->logFatalWithName('todo: ', 'tesing action 1');
 */

/* LogWriter preferences */
define('LOG_NONE',        0);
define('LOG_CUR_DATE',     1);
define('LOG_CUR_MONTH'2);
define('LOG_CUR_YEAR',    3);

define('LOG_WRITE_NOTICE',    10);
define('LOG_WRITE_WARN',    11);
define('LOG_WRITE_ERROR',    12);
define('LOG_WRITE_FATAL',    13);


class 
LogWriter implements iLog{
    
    private 
$logPath;
    private 
$log;
    
    function 
__construct($filename$logPath$logBy LOG_CUR_DATE){
        
$this->log $this->getLogMethod($logBy)."-".$filename.".log";
        
$this->logPath $logPath;
        
$this->logExists();
    }
    
    private function 
logCreate(){
        return 
touch($this->logPath.$this->log);
    }
    
    private function 
logExists(){
        if(
file_exists($this->logPath.$this->log)) return true;
        return 
$this->logCreate();
    }
    
    
    private function 
getLogMethod($logBy){
        switch(
$logBy){
            case 
LOG_NONE:
                
$method "";
            break;
            case 
LOG_CUR_DATE:
                
$method date("Y-m-d"time());
            break;
            case 
LOG_CUR_MONTH:
                
$method date("Y-m"time());
            break;
            case 
LOG_CUR_YEAR:
                
$method date("Y"time());
            break;
        }
        return 
$method;
    }
    
    private function 
getLogType($logType){
        switch(
$logType){
            case 
LOG_WRITE_NOTICE:
                
$type "NOTICE";
            break;
            case 
LOG_WRITE_WARN:
                
$type "WARN";
            break;
            case 
LOG_WRITE_ERROR:
                
$type "ERROR";
            break;
            case 
LOG_WRITE_FATAL:
                
$type "FATAL";
            break;
        }
        return 
$type;
    }
    
    private function 
log($msg$logType LOG_WRITE_NOTICE){
        
$logline date("r")." [".$this->getLogType($logType)."] ".$msg."\n";
        
$ffopen($this->logPath.$this->log,"a");
        
fwrite($f,$logline);
        
fclose($f);
    }
    
    public function 
logNotice($msg){
        
$this->log($msgLOG_WRITE_NOTICE);
    }
    
    public function 
logError($msg){
        
$this->log($msg,  LOG_WRITE_ERROR);
    }
    
    public function 
logWarn($msg){
        
$this->log($msgLOG_WRITE_WARN);
    }
    
    public function 
logFatal($msg){
        
$this->log($msgLOG_WRITE_FATAL);
    }
}

class 
Log implements iLog{
    private 
$writer;
    private 
$action = array();
    
    function 
__construct(LogWriter $writer){
        
$this->writer $writer;
    }
    
    private function 
microtime(){
       list(
$usec$sec) = explode(" "microtime());
       return ((float)
$usec + (float)$sec);
    }
    
    public function 
createAction($action){
        
$this->action[$action] = array(
                
'startTime' => '',
                
'endTime'    => '',
                
'finalTime' => ''
            
);
    }
    
    public function 
recordActionTime($action){
        
$this->createAction($action);
        
$startTime $this->microtime();
        
$this->action[$action]['startTime'] = $startTime;
    }
    
    public function 
stopActionTime($action){
        
$endTime $this->microtime();
        
$this->action[$action]['endTime'] = $endTime;
        
$this->action[$action]['finalTime'] = $this->getFinalTime($action);
    }
    
    private function 
getFinalTime($action){
        return 
$this->action[$action]['endTime'] - $this->action[$action]['startTime'];
    }
    
    public function 
getActionTime($action){
        return 
$this->action[$action]['finalTime'];
    }
    
    public function 
getActionLog($action){
        return 
"Action `".$action."` last for ".$this->getActionTime($action)." sec.";
    }
    
    public function 
logNotice($msg){
        
$this->writer->logNotice($msg);
    }
    
    public function 
logError($msg){
        
$this->writer->logError($msg);
    }
    
    public function 
logWarn($msg){
        
$this->writer->logWarn($msg);
    }
    
    public function 
logFatal($msg){
        
$this->writer->logFatal($msg);
    }
    
    public function 
logNoticeWithName($mgs$action){
        
$this->writer->logNotice($msg.' '.$this->getActionLog($action));
    }
    
    public function 
logErrorWithName($msg$action){
        
$this->writer->logError($msg.' '.$this->getActionLog($action));
    }
    
    public function 
logWarnWithName($msg$action){
        
$this->writer->logWarn($msg.' '.$this->getActionLog($action));
    }
    
    public function 
logFatalWithName($msg$action){
        
$this->writer->logFatal($msg.' '.$this->getActionLog($action));
    }
}
?>