00001 <?php
00169 class spunQ_Logger {
00170
00174 const VERBOSE_DEBUG = 0;
00175
00179 const VDEBUG = 0;
00180
00184 const DEBUG = 10;
00185
00189 const INFO = 20;
00190
00194 const WARNING = 30;
00195
00199 const ERROR = 40;
00200
00204 const CRITICAL = 50;
00205
00210 const MAX_STACK_SIZE = 10;
00211
00216 private static $instance = NULL;
00217
00222 public static function getInstance() {
00223 if (self::$instance === NULL) {
00224 self::$instance = new self();
00225 }
00226 return self::$instance;
00227 }
00228
00232 private $rules = array();
00233
00241 private $lowestPriorityRule = 100;
00242
00253 protected $messageStack = 0;
00254
00258 protected function __construct() {
00259 return NULL;
00260 }
00261
00267 public function vdebug($message) {
00268 return $this->logMessage(self::VDEBUG, $message);
00269 }
00270
00276 public function debug($message) {
00277 return $this->logMessage(self::DEBUG, $message);
00278 }
00279
00285 public function info($message) {
00286 return $this->logMessage(self::INFO, $message);
00287 }
00288
00294 public function warning($message) {
00295 return $this->logMessage(self::WARNING, $message);
00296 }
00297
00303 public function error($message) {
00304 return $this->logMessage(self::ERROR, $message);
00305 }
00306
00312 public function critical($message) {
00313 return $this->logMessage(self::CRITICAL, $message);
00314 }
00315
00323 public function logMessage($priority, $message) {
00324 if ($priority < $this->lowestPriorityRule) {
00325 return;
00326 }
00327 if ($this->messageStack >= self::MAX_STACK_SIZE) {
00328 return;
00329 }
00330 $this->messageStack++;
00331 $caller = $this->getCaller();
00332 $path = $this->fileToPath($caller['file'], $caller['function']);
00333 $rules = $this->getLoggingRules($path, $priority);
00334 $usedTargets = array();
00335 foreach ($rules as $rule) {
00336 if (in_array($rule->getLogTarget(), $usedTargets)) {
00337 continue;
00338 }
00339 $rule->getLogTarget()->logMessage($path, $priority, $message, $caller['file'], $caller['line']);
00340 $usedTargets[] = $rule->getLogTarget();
00341 }
00342 $this->messageStack--;
00343 return NULL;
00344 }
00345
00353 private function fileToPath($file, $function = NULL) {
00354 $functionString = $function === NULL ? '' : (':' . $function . '()');
00355 $alias = NULL;
00356 if (spunQ::isInitialized()) {
00357 $alias = spunQ_Module::fileToAlias($file);
00358 }
00359 if ($alias === NULL) {
00360 $alias = $file;
00361 }
00362 return $alias . $functionString;
00363 }
00364
00373 private function formatFileToPath($folder, $file) {
00374 $imploded = implode('/', $folder->getBreadcrumbsTo($file));
00375 $withoutExtension = (preg_replace('!\.[^/]*$!', '', $imploded));
00376 return str_replace('/', '.', $withoutExtension);
00377 }
00378
00389 public function addRule(spunQ_LoggerRule $rule) {
00390 if ($rule->getPriority() < $this->lowestPriorityRule) {
00391 $this->lowestPriorityRule = $rule->getPriority();
00392 }
00393 $this->rules[] = $rule;
00394 return $rule;
00395 }
00396
00404 public function removeRule(spunQ_LoggerRule $rule) {
00405 if (($key = array_search($rule, $this->rules, true)) !== FALSE) {
00406 $removed = array_splice($this->rules, $key, 1);
00407 $this->setLowestPriority();
00408 return $removed;
00409 }
00410 return $this;
00411 }
00412
00417 private function setLowestPriority(){
00418 $priorites = array();
00419 if (empty($this->rules)) {
00420 $this->lowestPriorityRule = 100;
00421 return NULL;
00422 }
00423 foreach ($this->rules as $rule) {
00424 $priorites[] = $rule->getPriority();
00425 }
00426 $this->lowestPriorityRule = min($priorities);
00427 return NULL;
00428 }
00429
00442 private function getCaller() {
00443 $trace = new spunQ_Backtrace();
00444 $file = NULL;
00445 $line = NULL;
00446 $function = NULL;
00447 foreach ($trace->getSlices() as $slice) {
00448 if (!preg_match('/(Logger|LogTarget)\.class\.php$/', $slice->getFile())) {
00449 $file = $slice->getFile();
00450 $line = $slice->getLine();
00451 $function = $slice->getScopingFunction();
00452 break;
00453 }
00454 }
00455 return array('file' => $file, 'line' => $line, 'function' => $function);
00456 }
00457
00464 private function getLoggingRules($path, $priority) {
00465 $rules = array();
00466 foreach ($this->rules as $rule) {
00467 $search = array('.', '*', '?', '(', ')');
00468 $replace = array('\.', '.*', '.', '\(', '\)');
00469 if ($rule->getPriority() <= $priority && ($rule->getPath() == NULL || preg_match('!' . str_replace($search, $replace, $rule->getPath()) . '.*!', $path))) {
00470 $rules[] = $rule;
00471 }
00472 }
00473 return $rules;
00474 }
00475
00481 public function clearAllRules() {
00482 $this->rules = array();
00483 $this->lowestPriorityRule = self::ERROR + 1;
00484 return NULL;
00485 }
00486
00487 }
00488
00494 function vd($message) {
00495 return vdebug($message);
00496 }
00497
00503 function vdebug($message) {
00504 spunQ_Logger::getInstance()->vdebug($message);
00505 return $message;
00506 }
00507
00513 function debug($message) {
00514 spunQ_Logger::getInstance()->debug($message);
00515 return $message;
00516 }
00517
00523 function info($message) {
00524 spunQ_Logger::getInstance()->info($message);
00525 return $message;
00526 }
00527
00533 function warning($message) {
00534 spunQ_Logger::getInstance()->warning($message);
00535 return $message;
00536 }
00537
00543 function error($message) {
00544 spunQ_Logger::getInstance()->error($message);
00545 return $message;
00546 }
00547
00553 function critical($message) {
00554 spunQ_Logger::getInstance()->critical($message);
00555 return $message;
00556 }
00557