PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/paypal/log.php

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