1
<?php
2
/*
3
  Belokan Micro Framework
4
  Licensed under GNU GPL
5
  http://gitorious.org/belokan
6
  ---
7
  Belokan/Log.php:
8
  Simple log system
9
*/
10
11
class Belokan_Log {
12
  
13
  private static
14
    $_instance;
15
16
  private
17
    $_stacks,
18
    $_logs;
19
  
20
  public static function getInstance ()
21
  {
22
    if (self::$_instance === null)
23
      self::$_instance = new self;
24
    return self::$_instance;
25
  }
26
  
27
  public function __construct ()
28
  {
29
    $this->_stacks = array();
30
    $this->_logs = array();
31
  }
32
  
33
  public function __destruct ()
34
  {
35
    if (Belokan_Config::getInstance()->get('log', 'autosave')) $this->write();
36
  }
37
  
38
  public function add ($log, $str)
39
  {
40
    if (!in_array($log, $this->_logs)) $this->_logs[] = $log;
41
    $this->_stacks[$log][] = '['.date('Y-m-d H:i').'] '.$str;
42
  }
43
  
44
  public function get ($log)
45
  {
46
    if (in_array($log, $this->_logs)) return $this->_stacks[$log];
47
    else return null;
48
  }
49
  
50
  public function write ()
51
  {
52
    if (count($this->_logs) == 0) return;
53
    
54
    foreach ($this->_logs as $log) {
55
      if (count($this->_stacks[$log]) == 0) continue;
56
      $tmp = '';
57
      foreach ($this->_stacks[$log] as $msg) $tmp .= $msg."\n";
58
      $logFile = fopen(Belokan_Config::getInstance()->get('dir', 'logs').$log.'.log', 'a');
59
      if ($logFile !== false) {
60
        fwrite($logFile, $tmp);
61
        fclose($logFile);
62
      }
63
    }
64
    
65
    $this->_stacks = array();
66
  }
67
  
68
}