/Class/Debug/Class.Log.php

https://github.com/CircleCode/dynacase-core · PHP · 310 lines · 252 code · 1 blank · 57 comment · 4 complexity · b6cb1e173368bb245100581db1e4ee31 MD5 · raw file

  1. <?php
  2. /*
  3. * @author Anakeen
  4. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  5. * @package FDL
  6. */
  7. /**
  8. * Log information class
  9. *
  10. * @author Anakeen
  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. * Log manager
  20. * log message according to CORE_LOGLEVEL parameter
  21. * @class Log
  22. *
  23. */
  24. class Log
  25. {
  26. public $loghead;
  27. public $application;
  28. public $function;
  29. private $deb;
  30. private $fin;
  31. private $tic;
  32. private $ptext;
  33. /**
  34. * Constant to set log to debug level
  35. * Debug level is used by Core.
  36. * It's used to assert taht Core works properly
  37. */
  38. const DEBUG = "D";
  39. /**
  40. * Constant to set log to callstack level
  41. */
  42. const CALLSTACK = "C";
  43. /**
  44. * Constant to set log to trace level
  45. * The trace level is a level reserved for user usage.
  46. * Core will never log with this level
  47. */
  48. const TRACE = "T";
  49. /**
  50. * Constant to set log to info level
  51. */
  52. const INFO = "I";
  53. /**
  54. * Constant to set log to warning level
  55. */
  56. const WARNING = "W";
  57. /**
  58. * Constant to set log to error level
  59. */
  60. const ERROR = "E";
  61. /**
  62. * Constant to set log to fatal level
  63. */
  64. const FATAL = "F";
  65. /**
  66. * Constant to set log to deprecated level
  67. */
  68. const DEPRECATED = "O";
  69. // ------------------------------------------------------------------------
  70. /**
  71. * @api initialize log manager
  72. * @param string $logfile
  73. * @param string $application
  74. * @param string $function
  75. */
  76. public function Log($logfile = "", $application = "", $function = "")
  77. {
  78. $this->usesyslog = 0;
  79. if ($logfile == "") {
  80. $this->usesyslog = 1;
  81. } else {
  82. $fd = fopen($logfile, "a");
  83. if (!$fd) {
  84. $this->usesyslog = 1;
  85. $this->error("Can't access $logfile, using syslog");
  86. } else {
  87. $this->logfile = $logfile;
  88. fclose($fd);
  89. }
  90. }
  91. $this->application = $application;
  92. $this->function = $function;
  93. }
  94. /**
  95. * log with debug level
  96. * @api log with debug level
  97. * @param string $string message text
  98. */
  99. public function debug($string)
  100. {
  101. $this->wlog(Log::DEBUG, $string);
  102. }
  103. /**
  104. * @param string $string message text
  105. */
  106. public function callstack($string)
  107. {
  108. $this->wlog(Log::CALLSTACK, $string);
  109. }
  110. /**
  111. * log with trace level
  112. * @api log with trace level
  113. * @param string $string mesage text
  114. */
  115. public function trace($string)
  116. {
  117. $this->wlog(Log::TRACE, $string);
  118. }
  119. /**
  120. * log with info level
  121. * @api log with info level
  122. * @param string $string message text
  123. */
  124. public function info($string)
  125. {
  126. $this->wlog(Log::INFO, $string);
  127. }
  128. /**
  129. * log with warning level
  130. * @api log with warning level
  131. * @param string $string message text
  132. */
  133. public function warning($string)
  134. {
  135. $this->wlog(Log::WARNING, $string);
  136. }
  137. /**
  138. * log with error level
  139. * @api log with error level
  140. * @param string $string message text
  141. */
  142. public function error($string)
  143. {
  144. $this->wlog(Log::ERROR, $string);
  145. }
  146. /**
  147. * log with fatal level
  148. * @api log with fatal level
  149. * @param string $string message text
  150. */
  151. public function fatal($string)
  152. {
  153. $this->wlog(Log::FATAL, $string);
  154. }
  155. /**
  156. * log with deprecated level
  157. * add callstack
  158. * @api log with deprecated level
  159. * @see Log
  160. * @param string $string message text
  161. */
  162. public function deprecated($string)
  163. {
  164. $this->wlog(Log::DEPRECATED, $string);
  165. }
  166. /**
  167. * to set start time
  168. * @param string $text prefix text to set for next tic/end
  169. */
  170. public function start($text = "")
  171. {
  172. $deb = gettimeofday();
  173. $this->deb = $deb["sec"] + $deb["usec"] / 1000000;
  174. $this->tic = $this->deb;
  175. $this->ptext = $text; // prefix
  176. }
  177. /**
  178. * log partial time
  179. * @see start
  180. * @param string $text text to log
  181. */
  182. public function tic($text)
  183. {
  184. $tic = gettimeofday();
  185. $now = $tic["sec"] + $tic["usec"] / 1000000;
  186. $duree = round($now - $this->tic, 3);
  187. $this->info("CHRONO-INT [$this->ptext]/[$text] : $duree");
  188. $this->tic = $now;
  189. }
  190. /**
  191. * log end time from last start
  192. * @param string $text text to log
  193. */
  194. public function end($text)
  195. {
  196. $fin = gettimeofday();
  197. $this->fin = $fin["sec"] + $fin["usec"] / 1000000;
  198. $duree = round($this->fin - $this->deb, 3);
  199. $this->info("CHRONO [$this->ptext]/[$text] : $duree");
  200. }
  201. public function push($string)
  202. {
  203. global $CORE_LOGLEVEL;
  204. if (isset($CORE_LOGLEVEL) && is_int(strpos($CORE_LOGLEVEL, "C"))) {
  205. global $call_ind, $call_stack, $call_pre, $call_reqid;
  206. if (!isset($call_ind)) $call_ind = 0;
  207. if (!isset($call_pre)) $call_pre = "-";
  208. if (!isset($call_reqid)) $call_reqid = rand(1, 100);
  209. $this->callstack("($call_reqid) $call_pre : entering $string");
  210. $call_stack[$call_ind] = $string;
  211. $call_ind+= 1;
  212. $call_pre = $call_pre . "-";
  213. }
  214. }
  215. public function pop()
  216. {
  217. global $CORE_LOGLEVEL;
  218. if (isset($CORE_LOGLEVEL) && is_int(strpos($CORE_LOGLEVEL, "C"))) {
  219. global $call_ind, $call_stack, $call_pre, $call_reqid;
  220. $call_pre = substr($call_pre, 0, strlen($call_pre) - 1);
  221. $call_ind-= 1;
  222. $this->callstack("($call_reqid) $call_pre : exiting {$call_stack[$call_ind]}");
  223. }
  224. }
  225. /**
  226. * main log function
  227. * @param string $sta log code (one character : IWEFDOT)
  228. * @param string $str message to log
  229. * @param null $args unused
  230. * @param int $facility syslog level
  231. */
  232. public function wlog($sta, $str, $args = NULL, $facility = LOG_LOCAL6)
  233. {
  234. global $_SERVER;
  235. global $CORE_LOGLEVEL;
  236. if (!$str) return;
  237. if (is_array($str)) $str = implode(", ", $str);
  238. if ($sta == "S" || (isset($CORE_LOGLEVEL) && is_int(strpos($CORE_LOGLEVEL, $sta)))) {
  239. $addr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : '';
  240. $appf = "[{$sta}] Dynacase";
  241. $appf.= ($this->application != "" ? ":" . $this->application : "");
  242. $appf.= ($this->function != "" ? ":" . $this->function : "");
  243. $str = ' ' . $this->loghead . ': ' . $str;
  244. if (!$this->usesyslog) {
  245. $xx = date("d/m/Y H:i:s", time()) . " {$appf} [{$addr}] ";
  246. $xx = $xx . $str . "\n";
  247. $fd = fopen($this->logfile, "a");
  248. fputs($fd, $xx);
  249. fclose($fd);
  250. } else {
  251. switch ($sta) {
  252. case Log::DEBUG:
  253. $pri = LOG_DEBUG;
  254. break;
  255. case Log::DEPRECATED:
  256. $class = (isset($td[4]["class"])) ? $td[4]["class"] : '';
  257. $td = @debug_backtrace(false);
  258. if ($str) {
  259. $str.= ' ';
  260. }
  261. $str.= sprintf("%s called in %s%s%s(), file %s:%s", isset($td[3]["function"])?$td[3]["function"]:'', $class, $class ? '::' : '', isset($td[4]["function"])?$td[4]["function"]:'', isset($td[4]["file"]) ? $td[4]["file"] : '', isset($td[4]["line"]) ? $td[4]["line"] : '');
  262. $pri = LOG_INFO;
  263. break;
  264. case Log::INFO:
  265. $pri = LOG_INFO;
  266. break;
  267. case Log::WARNING:
  268. $pri = LOG_WARNING;
  269. break;
  270. case Log::ERROR:
  271. $pri = LOG_ERR;
  272. break;
  273. case Log::FATAL:
  274. $pri = LOG_ALERT;
  275. break;
  276. case Log::TRACE:
  277. $pri = LOG_DEBUG;
  278. break;
  279. default:
  280. $pri = LOG_NOTICE;
  281. }
  282. if (empty($_SERVER['HTTP_HOST'])) {
  283. error_log(sprintf("%s LOG::$appf %s", date("d/m/Y H:i:s", time()) , $str));
  284. }
  285. openlog("{$appf}", 0, $facility);
  286. syslog($pri, "[{$addr}] " . $str);
  287. closelog();
  288. if ($sta == "E") {
  289. error_log($str); // use apache syslog also
  290. }
  291. }
  292. }
  293. }
  294. } // Class.Log
  295. ?>