PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/BarBottin/lib/ezpdo.1.1.6/libs/pear/Log/file.php

https://gitlab.com/BGCX261/zigolive-svn-to-git
PHP | 267 lines | 104 code | 37 blank | 126 comment | 17 complexity | 5f6ec266ae4bc85d5a78acbc97746f1a MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. <?php
  2. // $Id: file.php 212 2005-04-18 19:38:02Z nauhygon $
  3. /**
  4. * The Log_file class is a concrete implementation of the Log abstract
  5. * class that logs messages to a text file.
  6. *
  7. * @author Jon Parise <jon@php.net>
  8. * @author Roman Neuhauser <neuhauser@bellavista.cz>
  9. * @version $Revision$
  10. * @package Log
  11. */
  12. //class Log_file extends Log
  13. class epLib_Log_file extends epLib_Log
  14. {
  15. /**
  16. * String containing the name of the log file.
  17. * @var string
  18. * @access private
  19. */
  20. var $_filename = 'php.log';
  21. /**
  22. * Handle to the log file.
  23. * @var resource
  24. * @access private
  25. */
  26. var $_fp = false;
  27. /**
  28. * Should new log entries be append to an existing log file, or should the
  29. * a new log file overwrite an existing one?
  30. * @var boolean
  31. * @access private
  32. */
  33. var $_append = true;
  34. /**
  35. * Integer (in octal) containing the log file's permissions mode.
  36. * @var integer
  37. */
  38. var $_mode = 0644;
  39. /**
  40. * String containing the format of a log line.
  41. * @var string
  42. * @access private
  43. */
  44. var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  45. /**
  46. * String containing the timestamp format. It will be passed directly to
  47. * strftime(). Note that the timestamp string will generated using the
  48. * current locale.
  49. * @var string
  50. * @access private
  51. */
  52. var $_timeFormat = '%b %d %H:%M:%S';
  53. /**
  54. * Hash that maps canonical format keys to position arguments for the
  55. * "line format" string.
  56. * @var array
  57. * @access private
  58. */
  59. var $_formatMap = array('%{timestamp}' => '%1$s',
  60. '%{ident}' => '%2$s',
  61. '%{priority}' => '%3$s',
  62. '%{message}' => '%4$s',
  63. '%\{' => '%%{');
  64. /**
  65. * String containing the end-on-line character sequence.
  66. * @var string
  67. * @access private
  68. */
  69. var $_eol = "\n";
  70. /**
  71. * Constructs a new Log_file object.
  72. *
  73. * @param string $name Ignored.
  74. * @param string $ident The identity string.
  75. * @param array $conf The configuration array.
  76. * @param array $maxLevel Maximum priority level at which to log.
  77. * @access public
  78. */
  79. function epLib_Log_file($name, $ident = '', $conf = array(),
  80. $maxLevel = PEAR_LOG_DEBUG)
  81. //function Log_file($name, $ident = '', $conf = array(),
  82. // $maxLevel = PEAR_LOG_DEBUG)
  83. {
  84. $this->_id = md5(microtime());
  85. $this->_filename = $name;
  86. $this->_ident = $ident;
  87. //$this->_mask = Log::UPTO($maxLevel);
  88. $this->_mask = epLib_Log::UPTO($maxLevel);
  89. if (isset($conf['append'])) {
  90. $this->_append = $conf['append'];
  91. }
  92. if (!empty($conf['mode'])) {
  93. $this->_mode = $conf['mode'];
  94. }
  95. if (!empty($conf['lineFormat'])) {
  96. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  97. array_values($this->_formatMap),
  98. $conf['lineFormat']);
  99. }
  100. if (!empty($conf['timeFormat'])) {
  101. $this->_timeFormat = $conf['timeFormat'];
  102. }
  103. if (!empty($conf['eol'])) {
  104. $this->_eol = $conf['eol'];
  105. } else {
  106. $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
  107. }
  108. register_shutdown_function(array(&$this, '_Log_file'));
  109. }
  110. /**
  111. * Destructor
  112. */
  113. function _Log_file()
  114. {
  115. if ($this->_opened) {
  116. $this->close();
  117. }
  118. }
  119. /**
  120. * Creates the given directory path. If the parent directories don't
  121. * already exist, they will be created, too.
  122. *
  123. * @param string $path The full directory path to create.
  124. * @param integer $mode The permissions mode with which the
  125. * directories will be created.
  126. *
  127. * @return True if the full path is successfully created or already
  128. * exists.
  129. *
  130. * @access private
  131. */
  132. function _mkpath($path, $mode = 0700)
  133. {
  134. static $depth = 0;
  135. /* Guard against potentially infinite recursion. */
  136. if ($depth++ > 25) {
  137. trigger_error("_mkpath(): Maximum recursion depth (25) exceeded",
  138. E_USER_WARNING);
  139. return false;
  140. }
  141. /* We're only interested in the directory component of the path. */
  142. $path = dirname($path);
  143. /* If the directory already exists, return success immediately. */
  144. if (is_dir($path)) {
  145. $depth = 0;
  146. return true;
  147. }
  148. /*
  149. * In order to understand recursion, you must first understand
  150. * recursion ...
  151. */
  152. if ($this->_mkpath($path, $mode) === false) {
  153. return false;
  154. }
  155. return @mkdir($path, $mode);
  156. }
  157. /**
  158. * Opens the log file for output. If the specified log file does not
  159. * already exist, it will be created. By default, new log entries are
  160. * appended to the end of the log file.
  161. *
  162. * This is implicitly called by log(), if necessary.
  163. *
  164. * @access public
  165. */
  166. function open()
  167. {
  168. if (!$this->_opened) {
  169. /* If the log file's directory doesn't exist, create it. */
  170. if (!is_dir(dirname($this->_filename))) {
  171. $this->_mkpath($this->_filename);
  172. }
  173. /* Obtain a handle to the log file. */
  174. $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w');
  175. $this->_opened = ($this->_fp !== false);
  176. /* Attempt to set the log file's mode. */
  177. @chmod($this->_filename, $this->_mode);
  178. }
  179. return $this->_opened;
  180. }
  181. /**
  182. * Closes the log file if it is open.
  183. *
  184. * @access public
  185. */
  186. function close()
  187. {
  188. /* If the log file is open, close it. */
  189. if ($this->_opened && fclose($this->_fp)) {
  190. $this->_opened = false;
  191. }
  192. return ($this->_opened === false);
  193. }
  194. /**
  195. * Logs $message to the output window. The message is also passed along
  196. * to any Log_observer instances that are observing this Log.
  197. *
  198. * @param mixed $message String or object containing the message to log.
  199. * @param string $priority The priority of the message. Valid
  200. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  201. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  202. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  203. * The default is PEAR_LOG_INFO.
  204. * @return boolean True on success or false on failure.
  205. * @access public
  206. */
  207. function log($message, $priority = PEAR_LOG_INFO)
  208. {
  209. /* Abort early if the priority is above the maximum logging level. */
  210. if (!$this->_isMasked($priority)) {
  211. return false;
  212. }
  213. /* If the log file isn't already open, open it now. */
  214. if (!$this->_opened && !$this->open()) {
  215. return false;
  216. }
  217. /* Extract the string representation of the message. */
  218. $message = $this->_extractMessage($message);
  219. /* Build the string containing the complete log line. */
  220. $line = sprintf($this->_lineFormat, strftime($this->_timeFormat),
  221. $this->_ident, $this->priorityToString($priority),
  222. $message) . $this->_eol;
  223. /* Write the log line to the log file. */
  224. $success = (fwrite($this->_fp, $line) !== false);
  225. /* Notify observers about this log message. */
  226. $this->_announce(array('priority' => $priority, 'message' => $message));
  227. return $success;
  228. }
  229. }
  230. ?>