PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 1ms

/library/paypal/log.php

https://github.com/alugo/Goteo
PHP | 688 lines | 219 code | 54 blank | 415 comment | 20 complexity | b4ba508554775d1b3b01e94113cc0ac3 MD5 | raw file
  1<?php
  2/*
  3 *  Copyright (C) 2012 Platoniq y Fundacič´¸n Fuentes Abiertas (see README for details)
  4 *	This file is part of Goteo.
  5 *
  6 *  Goteo is free software: you can redistribute it and/or modify
  7 *  it under the terms of the GNU Affero General Public License as published by
  8 *  the Free Software Foundation, either version 3 of the License, or
  9 *  (at your option) any later version.
 10 *
 11 *  Goteo is distributed in the hope that it will be useful,
 12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 *  GNU Affero General Public License for more details.
 15 *
 16 *  You should have received a copy of the GNU Affero General Public License
 17 *  along with Goteo.  If not, see <http://www.gnu.org/licenses/agpl.txt>.
 18 *
 19 */
 20
 21/**
 22 * $Header: /home/ppcvs/paypal_php_sdk/Log.php,v 1.1 2006/02/19 08:22:40 dennis Exp $
 23 * $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $
 24 *
 25 * @version $Revision: 1.1 $
 26 * @package Log
 27 */
 28
 29define('PEAR_LOG_EMERG',    0);     /** System is unusable */
 30define('PEAR_LOG_ALERT',    1);     /** Immediate action required */
 31define('PEAR_LOG_CRIT',     2);     /** Critical conditions */
 32define('PEAR_LOG_ERR',      3);     /** Error conditions */
 33define('PEAR_LOG_WARNING',  4);     /** Warning conditions */
 34define('PEAR_LOG_NOTICE',   5);     /** Normal but significant */
 35define('PEAR_LOG_INFO',     6);     /** Informational */
 36define('PEAR_LOG_DEBUG',    7);     /** Debug-level messages */
 37
 38define('PEAR_LOG_ALL',      bindec('11111111'));  /** All messages */
 39define('PEAR_LOG_NONE',     bindec('00000000'));  /** No message */
 40
 41/* Log types for PHP's native error_log() function. */
 42define('PEAR_LOG_TYPE_SYSTEM',  0); /** Use PHP's system logger */
 43define('PEAR_LOG_TYPE_MAIL',    1); /** Use PHP's mail() function */
 44define('PEAR_LOG_TYPE_DEBUG',   2); /** Use PHP's debugging connection */
 45define('PEAR_LOG_TYPE_FILE',    3); /** Append to a file */
 46
 47/**
 48 * The Log:: class implements both an abstraction for various logging
 49 * mechanisms and the Subject end of a Subject-Observer pattern.
 50 *
 51 * @author  Chuck Hagenbuch <chuck@horde.org>
 52 * @author  Jon Parise <jon@php.net>
 53 * @since   Horde 1.3
 54 * @package Log
 55 */
 56class Log
 57{
 58    /**
 59     * Indicates whether or not the log can been opened / connected.
 60     *
 61     * @var boolean
 62     * @access private
 63     */
 64    public $_opened = false;
 65
 66    /**
 67     * Instance-specific unique identification number.
 68     *
 69     * @var integer
 70     * @access private
 71     */
 72    public $_id = 0;
 73
 74    /**
 75     * The label that uniquely identifies this set of log messages.
 76     *
 77     * @var string
 78     * @access private
 79     */
 80    public $_ident = '';
 81
 82    /**
 83     * The default priority to use when logging an event.
 84     *
 85     * @var integer
 86     * @access private
 87     */
 88    public $_priority = PEAR_LOG_INFO;
 89
 90    /**
 91     * The bitmask of allowed log levels.
 92     * @var integer
 93     * @access private
 94     */
 95    public $_mask = PEAR_LOG_ALL;
 96
 97    /**
 98     * Holds all Log_observer objects that wish to be notified of new messages.
 99     *
100     * @var array
101     * @access private
102     */
103    public $_listeners = array();
104
105
106    /**
107     * Attempts to return a concrete Log instance of type $handler.
108     *
109     * @param string $handler   The type of concrete Log subclass to return.
110     *                          Attempt to dynamically include the code for
111     *                          this subclass. Currently, valid values are
112     *                          'console', 'syslog', 'sql', 'file', and 'mcal'.
113     *
114     * @param string $name      The name of the actually log file, table, or
115     *                          other specific store to use. Defaults to an
116     *                          empty string, with which the subclass will
117     *                          attempt to do something intelligent.
118     *
119     * @param string $ident     The identity reported to the log system.
120     *
121     * @param array  $conf      A hash containing any additional configuration
122     *                          information that a subclass might need.
123     *
124     * @param int $level        Log messages up to and including this level.
125     *
126     * @return object Log       The newly created concrete Log instance, or
127     *                          null on an error.
128     * @access public
129     * @since Log 1.0
130     */
131    function &factory($handler, $name = '', $ident = '', $conf = array(),
132                      $level = PEAR_LOG_DEBUG)
133    {
134        $handler = strtolower($handler);
135        $class = 'Log_' . $handler;
136        $classfile = 'Log/' . $handler . '.php';
137
138        /*
139         * Attempt to include our version of the named class, but don't treat
140         * a failure as fatal.  The caller may have already included their own
141         * version of the named class.
142         */
143        if (!class_exists($class)) {
144            @include_once $classfile;
145        }
146
147        /* If the class exists, return a new instance of it. */
148        if (class_exists($class)) {
149            $obj = new $class($name, $ident, $conf, $level);
150            return $obj;
151        }
152
153        return null;
154    }
155
156    /**
157     * Attempts to return a reference to a concrete Log instance of type
158     * $handler, only creating a new instance if no log instance with the same
159     * parameters currently exists.
160     *
161     * You should use this if there are multiple places you might create a
162     * logger, you don't want to create multiple loggers, and you don't want to
163     * check for the existance of one each time. The singleton pattern does all
164     * the checking work for you.
165     *
166     * <b>You MUST call this method with the $var = &Log::singleton() syntax.
167     * Without the ampersand (&) in front of the method name, you will not get
168     * a reference, you will get a copy.</b>
169     *
170     * @param string $handler   The type of concrete Log subclass to return.
171     *                          Attempt to dynamically include the code for
172     *                          this subclass. Currently, valid values are
173     *                          'console', 'syslog', 'sql', 'file', and 'mcal'.
174     *
175     * @param string $name      The name of the actually log file, table, or
176     *                          other specific store to use.  Defaults to an
177     *                          empty string, with which the subclass will
178     *                          attempt to do something intelligent.
179     *
180     * @param string $ident     The identity reported to the log system.
181     *
182     * @param array $conf       A hash containing any additional configuration
183     *                          information that a subclass might need.
184     *
185     * @param int $level        Log messages up to and including this level.
186     *
187     * @return object Log       The newly created concrete Log instance, or
188     *                          null on an error.
189     * @access public
190     * @since Log 1.0
191     */
192    function &singleton($handler, $name = '', $ident = '', $conf = array(),
193                        $level = PEAR_LOG_DEBUG)
194    {
195        static $instances;
196        if (!isset($instances)) $instances = array();
197
198        $signature = serialize(array($handler, $name, $ident, $conf, $level));
199        if (!isset($instances[$signature])) {
200            $instances[$signature] = &Log::factory($handler, $name, $ident,
201                                                   $conf, $level);
202        }
203
204        return $instances[$signature];
205    }
206
207    /**
208     * Abstract implementation of the open() method.
209     * @since Log 1.0
210     */
211    function open()
212    {
213        return false;
214    }
215
216    /**
217     * Abstract implementation of the close() method.
218     * @since Log 1.0
219     */
220    function close()
221    {
222        return false;
223    }
224
225    /**
226     * Abstract implementation of the flush() method.
227     * @since Log 1.8.2
228     */
229    function flush()
230    {
231        return false;
232    }
233
234    /**
235     * Abstract implementation of the log() method.
236     * @since Log 1.0
237     */
238    function log($message, $priority = null)
239    {
240        return false;
241    }
242
243    /**
244     * A convenience function for logging a emergency event.  It will log a
245     * message at the PEAR_LOG_EMERG log level.
246     *
247     * @param   mixed   $message    String or object containing the message
248     *                              to log.
249     *
250     * @return  boolean True if the message was successfully logged.
251     *
252     * @access  public
253     * @since   Log 1.7.0
254     */
255    function emerg($message)
256    {
257        return $this->log($message, PEAR_LOG_EMERG);
258    }
259
260    /**
261     * A convenience function for logging an alert event.  It will log a
262     * message at the PEAR_LOG_ALERT log level.
263     *
264     * @param   mixed   $message    String or object containing the message
265     *                              to log.
266     *
267     * @return  boolean True if the message was successfully logged.
268     *
269     * @access  public
270     * @since   Log 1.7.0
271     */
272    function alert($message)
273    {
274        return $this->log($message, PEAR_LOG_ALERT);
275    }
276
277    /**
278     * A convenience function for logging a critical event.  It will log a
279     * message at the PEAR_LOG_CRIT log level.
280     *
281     * @param   mixed   $message    String or object containing the message
282     *                              to log.
283     *
284     * @return  boolean True if the message was successfully logged.
285     *
286     * @access  public
287     * @since   Log 1.7.0
288     */
289    function crit($message)
290    {
291        return $this->log($message, PEAR_LOG_CRIT);
292    }
293
294    /**
295     * A convenience function for logging a error event.  It will log a
296     * message at the PEAR_LOG_ERR log level.
297     *
298     * @param   mixed   $message    String or object containing the message
299     *                              to log.
300     *
301     * @return  boolean True if the message was successfully logged.
302     *
303     * @access  public
304     * @since   Log 1.7.0
305     */
306    function err($message)
307    {
308        return $this->log($message, PEAR_LOG_ERR);
309    }
310
311    /**
312     * A convenience function for logging a warning event.  It will log a
313     * message at the PEAR_LOG_WARNING log level.
314     *
315     * @param   mixed   $message    String or object containing the message
316     *                              to log.
317     *
318     * @return  boolean True if the message was successfully logged.
319     *
320     * @access  public
321     * @since   Log 1.7.0
322     */
323    function warning($message)
324    {
325        return $this->log($message, PEAR_LOG_WARNING);
326    }
327
328    /**
329     * A convenience function for logging a notice event.  It will log a
330     * message at the PEAR_LOG_NOTICE log level.
331     *
332     * @param   mixed   $message    String or object containing the message
333     *                              to log.
334     *
335     * @return  boolean True if the message was successfully logged.
336     *
337     * @access  public
338     * @since   Log 1.7.0
339     */
340    function notice($message)
341    {
342        return $this->log($message, PEAR_LOG_NOTICE);
343    }
344
345    /**
346     * A convenience function for logging a information event.  It will log a
347     * message at the PEAR_LOG_INFO log level.
348     *
349     * @param   mixed   $message    String or object containing the message
350     *                              to log.
351     *
352     * @return  boolean True if the message was successfully logged.
353     *
354     * @access  public
355     * @since   Log 1.7.0
356     */
357    function info($message)
358    {
359        return $this->log($message, PEAR_LOG_INFO);
360    }
361
362    /**
363     * A convenience function for logging a debug event.  It will log a
364     * message at the PEAR_LOG_DEBUG log level.
365     *
366     * @param   mixed   $message    String or object containing the message
367     *                              to log.
368     *
369     * @return  boolean True if the message was successfully logged.
370     *
371     * @access  public
372     * @since   Log 1.7.0
373     */
374    function debug($message)
375    {
376        return $this->log($message, PEAR_LOG_DEBUG);
377    }
378
379    /**
380     * Returns the string representation of the message data.
381     *
382     * If $message is an object, _extractMessage() will attempt to extract
383     * the message text using a known method (such as a PEAR_Error object's
384     * getMessage() method).  If a known method, cannot be found, the
385     * serialized representation of the object will be returned.
386     *
387     * If the message data is already a string, it will be returned unchanged.
388     *
389     * @param  mixed $message   The original message data.  This may be a
390     *                          string or any object.
391     *
392     * @return string           The string representation of the message.
393     *
394     * @access private
395     */
396    function _extractMessage($message)
397    {
398        /*
399         * If we've been given an object, attempt to extract the message using
400         * a known method.  If we can't find such a method, default to the
401         * "human-readable" version of the object.
402         *
403         * We also use the human-readable format for arrays.
404         */
405        if (is_object($message)) {
406            if (method_exists($message, 'getmessage')) {
407                $message = $message->getMessage();
408            } else if (method_exists($message, 'tostring')) {
409                $message = $message->toString();
410            } else if (method_exists($message, '__tostring')) {
411                if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
412                    $message = (string)$message;
413                } else {
414                    $message = $message->__toString();
415                }
416            } else {
417                $message = print_r($message, true);
418            }
419        } else if (is_array($message)) {
420            if (isset($message['message'])) {
421                $message = $message['message'];
422            } else {
423                $message = print_r($message, true);
424            }
425        }
426
427        /* Otherwise, we assume the message is a string. */
428        return $message;
429    }
430
431    /**
432     * Returns the string representation of a PEAR_LOG_* integer constant.
433     *
434     * @param int $priority     A PEAR_LOG_* integer constant.
435     *
436     * @return string           The string representation of $level.
437     *
438     * @since   Log 1.0
439     */
440    function priorityToString($priority)
441    {
442        $levels = array(
443            PEAR_LOG_EMERG   => 'emergency',
444            PEAR_LOG_ALERT   => 'alert',
445            PEAR_LOG_CRIT    => 'critical',
446            PEAR_LOG_ERR     => 'error',
447            PEAR_LOG_WARNING => 'warning',
448            PEAR_LOG_NOTICE  => 'notice',
449            PEAR_LOG_INFO    => 'info',
450            PEAR_LOG_DEBUG   => 'debug'
451        );
452
453        return $levels[$priority];
454    }
455
456    /**
457     * Returns the the PEAR_LOG_* integer constant for the given string
458     * representation of a priority name.  This function performs a
459     * case-insensitive search.
460     *
461     * @param string $name      String containing a priority name.
462     *
463     * @return string           The PEAR_LOG_* integer contstant corresponding
464     *                          the the specified priority name.
465     *
466     * @since   Log 1.9.0
467     */
468    function stringToPriority($name)
469    {
470        $levels = array(
471            'emergency' => PEAR_LOG_EMERG,
472            'alert'     => PEAR_LOG_ALERT,
473            'critical'  => PEAR_LOG_CRIT,
474            'error'     => PEAR_LOG_ERR,
475            'warning'   => PEAR_LOG_WARNING,
476            'notice'    => PEAR_LOG_NOTICE,
477            'info'      => PEAR_LOG_INFO,
478            'debug'     => PEAR_LOG_DEBUG
479        );
480
481        return $levels[strtolower($name)];
482    }
483
484    /**
485     * Calculate the log mask for the given priority.
486     *
487     * @param integer   $priority   The priority whose mask will be calculated.
488     *
489     * @return integer  The calculated log mask.
490     *
491     * @access  public
492     * @since   Log 1.7.0
493     */
494    function MASK($priority)
495    {
496        return (1 << $priority);
497    }
498
499    /**
500     * Calculate the log mask for all priorities up to the given priority.
501     *
502     * @param integer   $priority   The maximum priority covered by this mask.
503     *
504     * @return integer  The calculated log mask.
505     *
506     * @access  public
507     * @since   Log 1.7.0
508     */
509    function UPTO($priority)
510    {
511        return ((1 << ($priority + 1)) - 1);
512    }
513
514    /**
515     * Set and return the level mask for the current Log instance.
516     *
517     * @param integer $mask     A bitwise mask of log levels.
518     *
519     * @return integer          The current level mask.
520     *
521     * @access  public
522     * @since   Log 1.7.0
523     */
524    function setMask($mask)
525    {
526        $this->_mask = $mask;
527
528        return $this->_mask;
529    }
530
531    /**
532     * Returns the current level mask.
533     *
534     * @return interger         The current level mask.
535     *
536     * @access  public
537     * @since   Log 1.7.0
538     */
539    function getMask()
540    {
541        return $this->_mask;
542    }
543
544    /**
545     * Check if the given priority is included in the current level mask.
546     *
547     * @param integer   $priority   The priority to check.
548     *
549     * @return boolean  True if the given priority is included in the current
550     *                  log mask.
551     *
552     * @access  private
553     * @since   Log 1.7.0
554     */
555    function _isMasked($priority)
556    {
557        return (Log::MASK($priority) & $this->_mask);
558    }
559
560    /**
561     * Returns the current default priority.
562     *
563     * @return integer  The current default priority.
564     *
565     * @access  public
566     * @since   Log 1.8.4
567     */
568    function getPriority()
569    {
570        return $this->_priority;
571    }
572
573    /**
574     * Sets the default priority to the specified value.
575     *
576     * @param   integer $priority   The new default priority.
577     *
578     * @access  public
579     * @since   Log 1.8.4
580     */
581    function setPriority($priority)
582    {
583        $this->_priority = $priority;
584    }
585
586    /**
587     * Adds a Log_observer instance to the list of observers that are listening
588     * for messages emitted by this Log instance.
589     *
590     * @param object    $observer   The Log_observer instance to attach as a
591     *                              listener.
592     *
593     * @param boolean   True if the observer is successfully attached.
594     *
595     * @access  public
596     * @since   Log 1.0
597     */
598    function attach(&$observer)
599    {
600        if (!is_a($observer, 'Log_observer')) {
601            return false;
602        }
603
604        $this->_listeners[$observer->_id] = &$observer;
605
606        return true;
607    }
608
609    /**
610     * Removes a Log_observer instance from the list of observers.
611     *
612     * @param object    $observer   The Log_observer instance to detach from
613     *                              the list of listeners.
614     *
615     * @param boolean   True if the observer is successfully detached.
616     *
617     * @access  public
618     * @since   Log 1.0
619     */
620    function detach($observer)
621    {
622        if (!is_a($observer, 'Log_observer') ||
623            !isset($this->_listeners[$observer->_id])) {
624            return false;
625        }
626
627        unset($this->_listeners[$observer->_id]);
628
629        return true;
630    }
631
632    /**
633     * Informs each registered observer instance that a new message has been
634     * logged.
635     *
636     * @param array     $event      A hash describing the log event.
637     *
638     * @access private
639     */
640    function _announce($event)
641    {
642        foreach ($this->_listeners as $id => $listener) {
643            if ($event['priority'] <= $this->_listeners[$id]->_priority) {
644                $this->_listeners[$id]->notify($event);
645            }
646        }
647    }
648
649    /**
650     * Indicates whether this is a composite class.
651     *
652     * @return boolean          True if this is a composite class.
653     *
654     * @access  public
655     * @since   Log 1.0
656     */
657    function isComposite()
658    {
659        return false;
660    }
661
662    /**
663     * Sets this Log instance's identification string.
664     *
665     * @param string    $ident      The new identification string.
666     *
667     * @access  public
668     * @since   Log 1.6.3
669     */
670    function setIdent($ident)
671    {
672        $this->_ident = $ident;
673    }
674
675    /**
676     * Returns the current identification string.
677     *
678     * @return string   The current Log instance's identification string.
679     *
680     * @access  public
681     * @since   Log 1.6.3
682     */
683    function getIdent()
684    {
685        return $this->_ident;
686    }
687}
688?>