source: trunk/www.guidonia.net/wp/wp-content/plugins/fbconnect/fbConnectLog.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 24.5 KB
Line 
1<?php
2/**
3 * @author: Javier Reyes Gomez (http://www.sociable.es)
4 * @date: 05/10/2008
5 * @license: GPLv2
6 */
7
8define('PEAR_LOG_EMERG', 0); /** System is unusable */
9define('PEAR_LOG_ALERT', 1); /** Immediate action required */
10define('PEAR_LOG_CRIT', 2); /** Critical conditions */
11define('PEAR_LOG_ERR', 3); /** Error conditions */
12define('PEAR_LOG_WARNING', 4); /** Warning conditions */
13define('PEAR_LOG_NOTICE', 5); /** Normal but significant */
14define('PEAR_LOG_INFO', 6); /** Informational */
15define('PEAR_LOG_DEBUG', 7); /** Debug-level messages */
16
17define('PEAR_LOG_ALL', bindec('11111111')); /** All messages */
18define('PEAR_LOG_NONE', bindec('00000000')); /** No message */
19
20/* Log types for PHP's native error_log() function. */
21define('PEAR_LOG_TYPE_SYSTEM', 0); /** Use PHP's system logger */
22define('PEAR_LOG_TYPE_MAIL', 1); /** Use PHP's mail() function */
23define('PEAR_LOG_TYPE_DEBUG', 2); /** Use PHP's debugging connection */
24define('PEAR_LOG_TYPE_FILE', 3); /** Append to a file */
25
26/**
27 * The Log:: class implements both an abstraction for various logging
28 * mechanisms and the Subject end of a Subject-Observer pattern.
29 *
30 * @author Chuck Hagenbuch <chuck@horde.org>
31 * @author Jon Parise <jon@php.net>
32 * @since Horde 1.3
33 * @package Log
34 */
35class Log
36{
37 /**
38 * Indicates whether or not the log can been opened / connected.
39 *
40 * @var boolean
41 * @access private
42 */
43 var $_opened = false;
44
45 /**
46 * Instance-specific unique identification number.
47 *
48 * @var integer
49 * @access private
50 */
51 var $_id = 0;
52
53 /**
54 * The label that uniquely identifies this set of log messages.
55 *
56 * @var string
57 * @access private
58 */
59 var $_ident = '';
60
61 /**
62 * The default priority to use when logging an event.
63 *
64 * @var integer
65 * @access private
66 */
67 var $_priority = PEAR_LOG_INFO;
68
69 /**
70 * The bitmask of allowed log levels.
71 *
72 * @var integer
73 * @access private
74 */
75 var $_mask = PEAR_LOG_ALL;
76
77 /**
78 * Holds all Log_observer objects that wish to be notified of new messages.
79 *
80 * @var array
81 * @access private
82 */
83 var $_listeners = array();
84
85 /**
86 * Maps canonical format keys to position arguments for use in building
87 * "line format" strings.
88 *
89 * @var array
90 * @access private
91 */
92 var $_formatMap = array('%{timestamp}' => '%1$s',
93 '%{ident}' => '%2$s',
94 '%{priority}' => '%3$s',
95 '%{message}' => '%4$s',
96 '%{file}' => '%5$s',
97 '%{line}' => '%6$s',
98 '%{function}' => '%7$s',
99 '%\{' => '%%{');
100
101
102 /**
103 * Attempts to return a concrete Log instance of type $handler.
104 *
105 * @param string $handler The type of concrete Log subclass to return.
106 * Attempt to dynamically include the code for
107 * this subclass. Currently, valid values are
108 * 'console', 'syslog', 'sql', 'file', and 'mcal'.
109 *
110 * @param string $name The name of the actually log file, table, or
111 * other specific store to use. Defaults to an
112 * empty string, with which the subclass will
113 * attempt to do something intelligent.
114 *
115 * @param string $ident The identity reported to the log system.
116 *
117 * @param array $conf A hash containing any additional configuration
118 * information that a subclass might need.
119 *
120 * @param int $level Log messages up to and including this level.
121 *
122 * @return object Log The newly created concrete Log instance, or
123 * null on an error.
124 * @access public
125 * @since Log 1.0
126 */
127 function &factory($handler, $name = '', $ident = '', $conf = array(),
128 $level = PEAR_LOG_DEBUG)
129 {
130 $handler = strtolower($handler);
131 $class = 'Log_' . $handler;
132 $classfile = 'Log/' . $handler . '.php';
133
134 /*
135 * Attempt to include our version of the named class, but don't treat
136 * a failure as fatal. The caller may have already included their own
137 * version of the named class.
138 */
139 if (!class_exists($class)) {
140 include_once $classfile;
141 }
142
143 /* If the class exists, return a new instance of it. */
144 if (class_exists($class)) {
145 $obj = &new $class($name, $ident, $conf, $level);
146 return $obj;
147 }
148
149 $null = null;
150 return $null;
151 }
152
153 /**
154 * Attempts to return a reference to a concrete Log instance of type
155 * $handler, only creating a new instance if no log instance with the same
156 * parameters currently exists.
157 *
158 * You should use this if there are multiple places you might create a
159 * logger, you don't want to create multiple loggers, and you don't want to
160 * check for the existance of one each time. The singleton pattern does all
161 * the checking work for you.
162 *
163 * <b>You MUST call this method with the $var = &Log::singleton() syntax.
164 * Without the ampersand (&) in front of the method name, you will not get
165 * a reference, you will get a copy.</b>
166 *
167 * @param string $handler The type of concrete Log subclass to return.
168 * Attempt to dynamically include the code for
169 * this subclass. Currently, valid values are
170 * 'console', 'syslog', 'sql', 'file', and 'mcal'.
171 *
172 * @param string $name The name of the actually log file, table, or
173 * other specific store to use. Defaults to an
174 * empty string, with which the subclass will
175 * attempt to do something intelligent.
176 *
177 * @param string $ident The identity reported to the log system.
178 *
179 * @param array $conf A hash containing any additional configuration
180 * information that a subclass might need.
181 *
182 * @param int $level Log messages up to and including this level.
183 *
184 * @return object Log The newly created concrete Log instance, or
185 * null on an error.
186 * @access public
187 * @since Log 1.0
188 */
189 function &singleton($handler, $name = '', $ident = '', $conf = array(),
190 $level = PEAR_LOG_DEBUG)
191 {
192 static $instances;
193 if (!isset($instances)) $instances = array();
194
195 $signature = serialize(array($handler, $name, $ident, $conf, $level));
196 if (!isset($instances[$signature])) {
197 $instances[$signature] = &Log::factory($handler, $name, $ident,
198 $conf, $level);
199 }
200
201 return $instances[$signature];
202 }
203
204 /**
205 * Abstract implementation of the open() method.
206 * @since Log 1.0
207 */
208 function open()
209 {
210 return false;
211 }
212
213 /**
214 * Abstract implementation of the close() method.
215 * @since Log 1.0
216 */
217 function close()
218 {
219 return false;
220 }
221
222 /**
223 * Abstract implementation of the flush() method.
224 * @since Log 1.8.2
225 */
226 function flush()
227 {
228 return false;
229 }
230
231 /**
232 * Abstract implementation of the log() method.
233 * @since Log 1.0
234 */
235 function log($message, $priority = null)
236 {
237 return false;
238 }
239
240 /**
241 * A convenience function for logging a emergency event. It will log a
242 * message at the PEAR_LOG_EMERG log level.
243 *
244 * @param mixed $message String or object containing the message
245 * to log.
246 *
247 * @return boolean True if the message was successfully logged.
248 *
249 * @access public
250 * @since Log 1.7.0
251 */
252 function emerg($message)
253 {
254 return $this->log($message, PEAR_LOG_EMERG);
255 }
256
257 /**
258 * A convenience function for logging an alert event. It will log a
259 * message at the PEAR_LOG_ALERT log level.
260 *
261 * @param mixed $message String or object containing the message
262 * to log.
263 *
264 * @return boolean True if the message was successfully logged.
265 *
266 * @access public
267 * @since Log 1.7.0
268 */
269 function alert($message)
270 {
271 return $this->log($message, PEAR_LOG_ALERT);
272 }
273
274 /**
275 * A convenience function for logging a critical event. It will log a
276 * message at the PEAR_LOG_CRIT log level.
277 *
278 * @param mixed $message String or object containing the message
279 * to log.
280 *
281 * @return boolean True if the message was successfully logged.
282 *
283 * @access public
284 * @since Log 1.7.0
285 */
286 function crit($message)
287 {
288 return $this->log($message, PEAR_LOG_CRIT);
289 }
290
291 /**
292 * A convenience function for logging a error event. It will log a
293 * message at the PEAR_LOG_ERR log level.
294 *
295 * @param mixed $message String or object containing the message
296 * to log.
297 *
298 * @return boolean True if the message was successfully logged.
299 *
300 * @access public
301 * @since Log 1.7.0
302 */
303 function err($message)
304 {
305 return $this->log($message, PEAR_LOG_ERR);
306 }
307
308 /**
309 * A convenience function for logging a warning event. It will log a
310 * message at the PEAR_LOG_WARNING log level.
311 *
312 * @param mixed $message String or object containing the message
313 * to log.
314 *
315 * @return boolean True if the message was successfully logged.
316 *
317 * @access public
318 * @since Log 1.7.0
319 */
320 function warning($message)
321 {
322 return $this->log($message, PEAR_LOG_WARNING);
323 }
324
325 /**
326 * A convenience function for logging a notice event. It will log a
327 * message at the PEAR_LOG_NOTICE log level.
328 *
329 * @param mixed $message String or object containing the message
330 * to log.
331 *
332 * @return boolean True if the message was successfully logged.
333 *
334 * @access public
335 * @since Log 1.7.0
336 */
337 function notice($message)
338 {
339 return $this->log($message, PEAR_LOG_NOTICE);
340 }
341
342 /**
343 * A convenience function for logging a information event. It will log a
344 * message at the PEAR_LOG_INFO log level.
345 *
346 * @param mixed $message String or object containing the message
347 * to log.
348 *
349 * @return boolean True if the message was successfully logged.
350 *
351 * @access public
352 * @since Log 1.7.0
353 */
354 function info($message)
355 {
356 return $this->log($message, PEAR_LOG_INFO);
357 }
358
359 /**
360 * A convenience function for logging a debug event. It will log a
361 * message at the PEAR_LOG_DEBUG log level.
362 *
363 * @param mixed $message String or object containing the message
364 * to log.
365 *
366 * @return boolean True if the message was successfully logged.
367 *
368 * @access public
369 * @since Log 1.7.0
370 */
371 function debug($message)
372 {
373 return $this->log($message, PEAR_LOG_DEBUG);
374 }
375
376 /**
377 * Returns the string representation of the message data.
378 *
379 * If $message is an object, _extractMessage() will attempt to extract
380 * the message text using a known method (such as a PEAR_Error object's
381 * getMessage() method). If a known method, cannot be found, the
382 * serialized representation of the object will be returned.
383 *
384 * If the message data is already a string, it will be returned unchanged.
385 *
386 * @param mixed $message The original message data. This may be a
387 * string or any object.
388 *
389 * @return string The string representation of the message.
390 *
391 * @access private
392 */
393 function _extractMessage($message)
394 {
395 /*
396 * If we've been given an object, attempt to extract the message using
397 * a known method. If we can't find such a method, default to the
398 * "human-readable" version of the object.
399 *
400 * We also use the human-readable format for arrays.
401 */
402 if (is_object($message)) {
403 if (method_exists($message, 'getmessage')) {
404 $message = $message->getMessage();
405 } else if (method_exists($message, 'tostring')) {
406 $message = $message->toString();
407 } else if (method_exists($message, '__tostring')) {
408 if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
409 $message = (string)$message;
410 } else {
411 $message = $message->__toString();
412 }
413 } else {
414 $message = print_r($message, true);
415 }
416 } else if (is_array($message)) {
417 if (isset($message['message'])) {
418 $message = $message['message'];
419 } else {
420 $message = print_r($message, true);
421 }
422 }
423
424 /* Otherwise, we assume the message is a string. */
425 return $message;
426 }
427
428 /**
429 * Using debug_backtrace(), returns the file, line, and enclosing function
430 * name of the source code context from which log() was invoked.
431 *
432 * @param int $depth The initial number of frames we should step
433 * back into the trace.
434 *
435 * @return array Array containing three strings: the filename, the line,
436 * and the function name from which log() was called.
437 *
438 * @access private
439 * @since Log 1.9.4
440 */
441 function _getBacktraceVars($depth)
442 {
443 /* Start by generating a backtrace from the current call (here). */
444 $backtrace = debug_backtrace();
445
446 /*
447 * If we were ultimately invoked by the composite handler, we need to
448 * increase our depth one additional level to compensate.
449 */
450 if (strcasecmp(@$backtrace[$depth+1]['class'], 'Log_composite') == 0) {
451 $depth++;
452 }
453
454 /*
455 * We're interested in the frame which invoked the log() function, so
456 * we need to walk back some number of frames into the backtrace. The
457 * $depth parameter tells us where to start looking. We go one step
458 * further back to find the name of the encapsulating function from
459 * which log() was called.
460 */
461 $file = @$backtrace[$depth]['file'];
462 $line = @$backtrace[$depth]['line'];
463 $func = @$backtrace[$depth + 1]['function'];
464
465 /*
466 * However, if log() was called from one of our "shortcut" functions,
467 * we're going to need to go back an additional step.
468 */
469 if (in_array($func, array('emerg', 'alert', 'crit', 'err', 'warning',
470 'notice', 'info', 'debug'))) {
471 $file = @$backtrace[$depth + 1]['file'];
472 $line = @$backtrace[$depth + 1]['line'];
473 $func = @$backtrace[$depth + 2]['function'];
474 }
475
476 /*
477 * If we couldn't extract a function name (perhaps because we were
478 * executed from the "main" context), provide a default value.
479 */
480 if (is_null($func)) {
481 $func = '(none)';
482 }
483
484 /* Return a 3-tuple containing (file, line, function). */
485 return array($file, $line, $func);
486 }
487
488 /**
489 * Produces a formatted log line based on a format string and a set of
490 * variables representing the current log record and state.
491 *
492 * @return string Formatted log string.
493 *
494 * @access private
495 * @since Log 1.9.4
496 */
497 function _format($format, $timestamp, $priority, $message)
498 {
499 /*
500 * If the format string references any of the backtrace-driven
501 * variables (%5, %6, %7), generate the backtrace and fetch them.
502 */
503 if (strpos($format, '%5') || strpos($format, '%6') || strpos($format, '%7')) {
504 list($file, $line, $func) = $this->_getBacktraceVars(2);
505 }
506
507 /*
508 * Build the formatted string. We use the sprintf() function's
509 * "argument swapping" capability to dynamically select and position
510 * the variables which will ultimately appear in the log string.
511 */
512 return sprintf($format,
513 $timestamp,
514 $this->_ident,
515 $this->priorityToString($priority),
516 $message,
517 isset($file) ? $file : '',
518 isset($line) ? $line : '',
519 isset($func) ? $func : '');
520 }
521
522 /**
523 * Returns the string representation of a PEAR_LOG_* integer constant.
524 *
525 * @param int $priority A PEAR_LOG_* integer constant.
526 *
527 * @return string The string representation of $level.
528 *
529 * @since Log 1.0
530 */
531 function priorityToString($priority)
532 {
533 $levels = array(
534 PEAR_LOG_EMERG => 'emergency',
535 PEAR_LOG_ALERT => 'alert',
536 PEAR_LOG_CRIT => 'critical',
537 PEAR_LOG_ERR => 'error',
538 PEAR_LOG_WARNING => 'warning',
539 PEAR_LOG_NOTICE => 'notice',
540 PEAR_LOG_INFO => 'info',
541 PEAR_LOG_DEBUG => 'debug'
542 );
543
544 return $levels[$priority];
545 }
546
547 /**
548 * Returns the the PEAR_LOG_* integer constant for the given string
549 * representation of a priority name. This function performs a
550 * case-insensitive search.
551 *
552 * @param string $name String containing a priority name.
553 *
554 * @return string The PEAR_LOG_* integer contstant corresponding
555 * the the specified priority name.
556 *
557 * @since Log 1.9.0
558 */
559 function stringToPriority($name)
560 {
561 $levels = array(
562 'emergency' => PEAR_LOG_EMERG,
563 'alert' => PEAR_LOG_ALERT,
564 'critical' => PEAR_LOG_CRIT,
565 'error' => PEAR_LOG_ERR,
566 'warning' => PEAR_LOG_WARNING,
567 'notice' => PEAR_LOG_NOTICE,
568 'info' => PEAR_LOG_INFO,
569 'debug' => PEAR_LOG_DEBUG
570 );
571
572 return $levels[strtolower($name)];
573 }
574
575 /**
576 * Calculate the log mask for the given priority.
577 *
578 * This method may be called statically.
579 *
580 * @param integer $priority The priority whose mask will be calculated.
581 *
582 * @return integer The calculated log mask.
583 *
584 * @access public
585 * @since Log 1.7.0
586 */
587 function MASK($priority)
588 {
589 return (1 << $priority);
590 }
591
592 /**
593 * Calculate the log mask for all priorities up to the given priority.
594 *
595 * This method may be called statically.
596 *
597 * @param integer $priority The maximum priority covered by this mask.
598 *
599 * @return integer The resulting log mask.
600 *
601 * @access public
602 * @since Log 1.7.0
603 *
604 * @deprecated deprecated since Log 1.9.4; use Log::MAX() instead
605 */
606 function UPTO($priority)
607 {
608 return Log::MAX($priority);
609 }
610
611 /**
612 * Calculate the log mask for all priorities greater than or equal to the
613 * given priority. In other words, $priority will be the lowest priority
614 * matched by the resulting mask.
615 *
616 * This method may be called statically.
617 *
618 * @param integer $priority The minimum priority covered by this mask.
619 *
620 * @return integer The resulting log mask.
621 *
622 * @access public
623 * @since Log 1.9.4
624 */
625 function MIN($priority)
626 {
627 return PEAR_LOG_ALL ^ ((1 << $priority) - 1);
628 }
629
630 /**
631 * Calculate the log mask for all priorities less than or equal to the
632 * given priority. In other words, $priority will be the highests priority
633 * matched by the resulting mask.
634 *
635 * This method may be called statically.
636 *
637 * @param integer $priority The maximum priority covered by this mask.
638 *
639 * @return integer The resulting log mask.
640 *
641 * @access public
642 * @since Log 1.9.4
643 */
644 function MAX($priority)
645 {
646 return ((1 << ($priority + 1)) - 1);
647 }
648
649 /**
650 * Set and return the level mask for the current Log instance.
651 *
652 * @param integer $mask A bitwise mask of log levels.
653 *
654 * @return integer The current level mask.
655 *
656 * @access public
657 * @since Log 1.7.0
658 */
659 function setMask($mask)
660 {
661 $this->_mask = $mask;
662
663 return $this->_mask;
664 }
665
666 /**
667 * Returns the current level mask.
668 *
669 * @return interger The current level mask.
670 *
671 * @access public
672 * @since Log 1.7.0
673 */
674 function getMask()
675 {
676 return $this->_mask;
677 }
678
679 /**
680 * Check if the given priority is included in the current level mask.
681 *
682 * @param integer $priority The priority to check.
683 *
684 * @return boolean True if the given priority is included in the current
685 * log mask.
686 *
687 * @access private
688 * @since Log 1.7.0
689 */
690 function _isMasked($priority)
691 {
692 return (Log::MASK($priority) & $this->_mask);
693 }
694
695 /**
696 * Returns the current default priority.
697 *
698 * @return integer The current default priority.
699 *
700 * @access public
701 * @since Log 1.8.4
702 */
703 function getPriority()
704 {
705 return $this->_priority;
706 }
707
708 /**
709 * Sets the default priority to the specified value.
710 *
711 * @param integer $priority The new default priority.
712 *
713 * @access public
714 * @since Log 1.8.4
715 */
716 function setPriority($priority)
717 {
718 $this->_priority = $priority;
719 }
720
721 /**
722 * Adds a Log_observer instance to the list of observers that are listening
723 * for messages emitted by this Log instance.
724 *
725 * @param object $observer The Log_observer instance to attach as a
726 * listener.
727 *
728 * @param boolean True if the observer is successfully attached.
729 *
730 * @access public
731 * @since Log 1.0
732 */
733 function attach(&$observer)
734 {
735 if (!is_a($observer, 'Log_observer')) {
736 return false;
737 }
738
739 $this->_listeners[$observer->_id] = &$observer;
740
741 return true;
742 }
743
744 /**
745 * Removes a Log_observer instance from the list of observers.
746 *
747 * @param object $observer The Log_observer instance to detach from
748 * the list of listeners.
749 *
750 * @param boolean True if the observer is successfully detached.
751 *
752 * @access public
753 * @since Log 1.0
754 */
755 function detach($observer)
756 {
757 if (!is_a($observer, 'Log_observer') ||
758 !isset($this->_listeners[$observer->_id])) {
759 return false;
760 }
761
762 unset($this->_listeners[$observer->_id]);
763
764 return true;
765 }
766
767 /**
768 * Informs each registered observer instance that a new message has been
769 * logged.
770 *
771 * @param array $event A hash describing the log event.
772 *
773 * @access private
774 */
775 function _announce($event)
776 {
777 foreach ($this->_listeners as $id => $listener) {
778 if ($event['priority'] <= $this->_listeners[$id]->_priority) {
779 $this->_listeners[$id]->notify($event);
780 }
781 }
782 }
783
784 /**
785 * Indicates whether this is a composite class.
786 *
787 * @return boolean True if this is a composite class.
788 *
789 * @access public
790 * @since Log 1.0
791 */
792 function isComposite()
793 {
794 return false;
795 }
796
797 /**
798 * Sets this Log instance's identification string.
799 *
800 * @param string $ident The new identification string.
801 *
802 * @access public
803 * @since Log 1.6.3
804 */
805 function setIdent($ident)
806 {
807 $this->_ident = $ident;
808 }
809
810 /**
811 * Returns the current identification string.
812 *
813 * @return string The current Log instance's identification string.
814 *
815 * @access public
816 * @since Log 1.6.3
817 */
818 function getIdent()
819 {
820 return $this->_ident;
821 }
822}
Note: See TracBrowser for help on using the repository browser.