phpDocumentor phpIcqBot
[ class tree: phpIcqBot ] [ index: phpIcqBot ] [ all elements ]

Source for file logs.class.php

Documentation is available at logs.class.php

  1. <?php
  2.  
  3. define('ICQBOT_DEFAULTLOGDIR','./store/logs/');
  4.  
  5. define('LOGLEVEL_0',0);
  6. define('LOGLEVEL_1',1);
  7. define('LOGLEVEL_2',2);
  8. define('LOGLEVEL_3',3);
  9. define('LOGLEVEL_4',4);
  10. define('LOGLEVEL_5',5);
  11.  
  12. define('LOGLEVEL_NONE',LOGLEVEL_0);
  13. define('LOGLEVEL_GENERAL',LOGLEVEL_1);
  14. //define('LOGLEVEL_')
  15.  
  16. define('LOGLEVEL_DEFAULT',LOGLEVEL_GENERAL);
  17.  
  18. {
  19.  
  20.     private $logdir;
  21.     private $writable;
  22.     private $loglevel;
  23.  
  24.     private $fp_log;
  25.     private $fp_customs; // array( file_name => file_resource, ... );
  26.  
  27.     public function __construct($logdir = ICQBOT_DEFAULTLOGDIR)
  28.     {
  29.         $this->logdir = $logdir; // ? $logdir : ICQBOT_DEFAULTLOGDIR;
  30.  
  31.         if (($this->writable = is_writable($this->logdir)))
  32.         {
  33.             $this->fp_log=fopen($this->logdir.'/icqbot.log','a');
  34.             fputs($this->fp_log,"\r\n");
  35.         }
  36.         else
  37.             echo 'Log directory "'.$this->logdir.'" is not writable! Logs will be unavailable.';
  38.         
  39.         $this->fp_customs = array();
  40.         
  41.         $this->loglevel = defined(LOGLEVEL) ? LOGLEVEL : LOGLEVEL_DEFAULT;
  42.  
  43.         return;
  44.     }
  45.  
  46.     public function write($msg, $loglevel = LOGLEVEL_DEFAULT, $timeformat = '[r]', $custom_file = '')
  47.     {
  48.         
  49.         if ($this->loglevel < $loglevel)
  50.             return false;
  51.  
  52.         if ($custom_file)
  53.             return $this->writeFile($msg, $timeformat, $custom_file);
  54.  
  55.         if (!$this->fp_log)
  56.             return false;
  57.         if (in_array('-v',$_SERVER['argv']))
  58.             echo ($timeformat ? date($timeformat).' ' : ''). $msg. "\r\n";
  59.  
  60.         return fputs($this->fp_log, ($timeformat ? date($timeformat).' ' : ''). $msg. "\r\n");
  61.     }
  62.  
  63.     private function writeFile($msg, $timeformat, $custom_file)
  64.     {
  65.         if (!array_key_exists($custom_file, $this->fp_customs))
  66.         {
  67.             $this->fp_customs[$custom_file] = @fopen($this->logdir.$custom_file,"a");
  68.             if (!$this->fp_customs[$custom_file])
  69.             {
  70.                 $this->write('Custom logfile "'.$custom_file.'" is not writable. Logs will be written here.');
  71.                 $this->fp_customs[$custom_file] = &$this->fp_log;
  72.             }
  73.         }
  74.  
  75.         if (!$this->fp_customs[$custom_file])
  76.             return false;
  77.  
  78.         if ($this->fp_customs[$custom_file] == $this->fp_log)
  79.             $msg= '{'.$custom_file.'} '.$msg;
  80.  
  81.         return fputs($this->fp_customs[$custom_file], ($timeformat ? date($timeformat).' ' : '').$msg."\r\n");
  82.     }
  83.  
  84.     public function __destruct()
  85.     {
  86.         foreach ($this->fp_customs as $v)
  87.             if ($v != $this->fp_log)
  88.                 fclose($v);
  89.  
  90.         if ($this->fp_log)
  91.             fclose($this->fp_log);
  92.  
  93.     }
  94. }

Documentation generated on Mon, 24 Dec 2007 09:34:31 +0500 by phpDocumentor 1.4.0