Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Log.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Log information class
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.Log.php,v 1.15 2008/10/31 16:57:18 jerome Exp $
12  * @license http://www.gnu.org/licenses/lgpl-3.0.html GNU Lesser General Public License
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 // ---------------------------------------------------------------------------
19 // $Id: Class.Log.php,v 1.15 2008/10/31 16:57:18 jerome Exp $
20 // yannick.lebriquer@anakeen.com
21 // ---------------------------------------------------------------------------
23 
24 class Log
25 {
26 
27  public $loghead;
28  public $application;
29  public $function;
30  // ------------------------------------------------------------------------
31  function Log($logfile = "", $application = "", $function = "")
32  {
33  $this->usesyslog = 0;
34  if ($logfile == "") {
35  $this->usesyslog = 1;
36  } else {
37  $fd = fopen($logfile, "a");
38  if (!$fd) {
39  $this->usesyslog = 1;
40  $this->error("Can't access $logfile, using syslog");
41  } else {
42  $this->logfile = $logfile;
43  fclose($fd);
44  }
45  }
46  $this->application = $application;
47  $this->function = $function;
48  }
49  // ------------------------------------------------------------------------
50  function debug($string, $args = NULL)
51  {
52  $this->wlog("D", $string);
53  }
54  function callstack($string, $args = NULL)
55  {
56  $this->wlog("C", $string);
57  }
58  function info($string, $args = NULL)
59  {
60  $this->wlog("I", $string);
61  }
62  function warning($string, $args = NULL)
63  {
64  $this->wlog("W", $string);
65  }
66  function error($string, $args = NULL)
67  {
68  $this->wlog("E", $string);
69  }
70  function fatal($string, $args = NULL)
71  {
72  $this->wlog("F", $string);
73  }
74  function deprecated($string, $args = NULL)
75  {
76  $this->wlog("O", $string);
77  }
78 
79  function start($text = "")
80  {
81  $deb = gettimeofday();
82  $this->deb = $deb["sec"] + $deb["usec"] / 1000000;
83  $this->tic = $this->deb;
84  $this->ptext = $text; // prefix
85 
86  }
87 
88  function tic($text)
89  {
90  $tic = gettimeofday();
91  $now = $tic["sec"] + $tic["usec"] / 1000000;
92  $duree = round($now - $this->tic, 3);
93  $this->info("CHRONO-INT [$this->ptext]/[$text] : $duree");
94  $this->tic = $now;
95  }
96 
97  function end($text)
98  {
99  $fin = gettimeofday();
100  $this->fin = $fin["sec"] + $fin["usec"] / 1000000;
101  $duree = round($this->fin - $this->deb, 3);
102  $this->info("CHRONO [$this->ptext]/[$text] : $duree");
103  }
104 
105  function push($string)
106  {
107  global $CORE_LOGLEVEL;
108  if (isset($CORE_LOGLEVEL) && is_int(strpos($CORE_LOGLEVEL, "C"))) {
109  global $call_ind, $call_stack, $call_pre, $call_reqid;
110  if (!isset($call_ind)) $call_ind = 0;
111  if (!isset($call_pre)) $call_pre = "-";
112  if (!isset($call_reqid)) $call_reqid = rand(1, 100);
113  $this->callstack("($call_reqid) $call_pre : entering $string");
114  $call_stack[$call_ind] = $string;
115  $call_ind+= 1;
116  $call_pre = $call_pre . "-";
117  }
118  }
119 
120  function pop()
121  {
122  global $CORE_LOGLEVEL;
123  if (isset($CORE_LOGLEVEL) && is_int(strpos($CORE_LOGLEVEL, "C"))) {
124  global $call_ind, $call_stack, $call_pre, $call_reqid;
125  $call_pre = substr($call_pre, 0, strlen($call_pre) - 1);
126  $call_ind-= 1;
127  $this->callstack("($call_reqid) $call_pre : exiting {$call_stack[$call_ind]}");
128  }
129  }
130  // ------------------------------------------------------------------------
131  function wlog($sta, $str, $args = NULL, $facility = LOG_LOCAL6)
132  {
133 
134  global $_SERVER;
135  global $CORE_LOGLEVEL;
136 
137  if (!$str) return;
138  if (is_array($str)) $str = implode(", ", $str);
139  if ($sta == "S" || (isset($CORE_LOGLEVEL) && is_int(strpos($CORE_LOGLEVEL, $sta)))) {
140  $addr = $_SERVER["REMOTE_ADDR"];
141  $appf = "[{$sta}] Dynacase";
142  $appf.= ($this->application != "" ? ":" . $this->application : "");
143  $appf.= ($this->function != "" ? ":" . $this->function : "");
144  $str = ' ' . $this->loghead . ': ' . $str;
145  if (!$this->usesyslog) {
146  $xx = date("d/m/Y H:i:s", time()) . " {$appf} [{$addr}] ";
147  $xx = $xx . $str . "\n";
148  $fd = fopen($this->logfile, "a");
149  fputs($fd, $xx);
150  fclose($fd);
151  } else {
152  switch ($sta) {
153  case "D":
154  $pri = LOG_DEBUG;
155  break;
156 
157  case "O":
158  $td = @debug_backtrace(false);
159  $str.= sprintf("%s called in %s%s%s(), file %s:%s", $td[3]["function"], $td[4]["class"], $td[4]["class"] ? '::' : '', $td[4]["function"], $td[4]["file"], $td[4]["line"]);
160  case "I":
161  $pri = LOG_INFO;
162  break;
163 
164  case "W":
165  $pri = LOG_WARNING;
166  break;
167 
168  case "E":
169  $pri = LOG_ERR;
170  break;
171 
172  case "F":
173  $pri = LOG_ALERT;
174  break;
175 
176  default:
177  $pri = LOG_NOTICE;
178  }
179  if ($_SERVER['HTTP_HOST'] == "") {
180  error_log(sprintf("%s LOG::$appf %s", date("d/m/Y H:i:s", time()) , $str));
181  }
182  openlog("{$appf}", 0, $facility);
183  syslog($pri, "[{$addr}] " . $str);
184  closelog();
185  }
186  }
187  }
188 } // Class.Log
189 
190 ?>
← centre documentaire © anakeen - published under CC License - Dynacase