Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

file.php 9.3 KiB

18 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * $Header: /home/ppcvs/paypal_php_sdk/Log/file.php,v 1.1 2006/02/19 08:22:29 dennis Exp $
  4. *
  5. * @version $Revision: 1.1 $
  6. * @package Log
  7. */
  8. /**
  9. * The Log_file class is a concrete implementation of the Log abstract
  10. * class that logs messages to a text file.
  11. *
  12. * @author Jon Parise <jon@php.net>
  13. * @author Roman Neuhauser <neuhauser@bellavista.cz>
  14. * @since Log 1.0
  15. * @package Log
  16. *
  17. * @example file.php Using the file handler.
  18. */
  19. class Log_file extends Log
  20. {
  21. /**
  22. * String containing the name of the log file.
  23. * @var string
  24. * @access private
  25. */
  26. var $_filename = 'php.log';
  27. /**
  28. * Handle to the log file.
  29. * @var resource
  30. * @access private
  31. */
  32. var $_fp = false;
  33. /**
  34. * Should new log entries be append to an existing log file, or should the
  35. * a new log file overwrite an existing one?
  36. * @var boolean
  37. * @access private
  38. */
  39. var $_append = true;
  40. /**
  41. * Should advisory file locking (i.e., flock()) be used?
  42. * @var boolean
  43. * @access private
  44. */
  45. var $_locking = false;
  46. /**
  47. * Integer (in octal) containing the log file's permissions mode.
  48. * @var integer
  49. * @access private
  50. */
  51. var $_mode = 0644;
  52. /**
  53. * Integer (in octal) specifying the file permission mode that will be
  54. * used when creating directories that do not already exist.
  55. * @var integer
  56. * @access private
  57. */
  58. var $_dirmode = 0755;
  59. /**
  60. * String containing the format of a log line.
  61. * @var string
  62. * @access private
  63. */
  64. var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  65. /**
  66. * String containing the timestamp format. It will be passed directly to
  67. * strftime(). Note that the timestamp string will generated using the
  68. * current locale.
  69. * @var string
  70. * @access private
  71. */
  72. var $_timeFormat = '%b %d %H:%M:%S';
  73. /**
  74. * Hash that maps canonical format keys to position arguments for the
  75. * "line format" string.
  76. * @var array
  77. * @access private
  78. */
  79. var $_formatMap = array('%{timestamp}' => '%1$s',
  80. '%{ident}' => '%2$s',
  81. '%{priority}' => '%3$s',
  82. '%{message}' => '%4$s',
  83. '%\{' => '%%{');
  84. /**
  85. * String containing the end-on-line character sequence.
  86. * @var string
  87. * @access private
  88. */
  89. var $_eol = "\n";
  90. /**
  91. * Constructs a new Log_file object.
  92. *
  93. * @param string $name Ignored.
  94. * @param string $ident The identity string.
  95. * @param array $conf The configuration array.
  96. * @param int $level Log messages up to and including this level.
  97. * @access public
  98. */
  99. function Log_file($name, $ident = '', $conf = array(),
  100. $level = PEAR_LOG_DEBUG)
  101. {
  102. $this->_id = md5(microtime());
  103. $this->_filename = $name;
  104. $this->_ident = $ident;
  105. $this->_mask = Log::UPTO($level);
  106. if (isset($conf['append'])) {
  107. $this->_append = $conf['append'];
  108. }
  109. if (isset($conf['locking'])) {
  110. $this->_locking = $conf['locking'];
  111. }
  112. if (!empty($conf['mode'])) {
  113. if (is_string($conf['mode'])) {
  114. $this->_mode = octdec($conf['mode']);
  115. } else {
  116. $this->_mode = $conf['mode'];
  117. }
  118. }
  119. if (!empty($conf['dirmode'])) {
  120. if (is_string($conf['dirmode'])) {
  121. $this->_dirmode = octdec($conf['dirmode']);
  122. } else {
  123. $this->_dirmode = $conf['dirmode'];
  124. }
  125. }
  126. if (!empty($conf['lineFormat'])) {
  127. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  128. array_values($this->_formatMap),
  129. $conf['lineFormat']);
  130. }
  131. if (!empty($conf['timeFormat'])) {
  132. $this->_timeFormat = $conf['timeFormat'];
  133. }
  134. if (!empty($conf['eol'])) {
  135. $this->_eol = $conf['eol'];
  136. } else {
  137. $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
  138. }
  139. register_shutdown_function(array(&$this, '_Log_file'));
  140. }
  141. /**
  142. * Destructor
  143. */
  144. function _Log_file()
  145. {
  146. if ($this->_opened) {
  147. $this->close();
  148. }
  149. }
  150. /**
  151. * Creates the given directory path. If the parent directories don't
  152. * already exist, they will be created, too.
  153. *
  154. * This implementation is inspired by Python's os.makedirs function.
  155. *
  156. * @param string $path The full directory path to create.
  157. * @param integer $mode The permissions mode with which the
  158. * directories will be created.
  159. *
  160. * @return True if the full path is successfully created or already
  161. * exists.
  162. *
  163. * @access private
  164. */
  165. function _mkpath($path, $mode = 0700)
  166. {
  167. /* Separate the last pathname component from the rest of the path. */
  168. $head = dirname($path);
  169. $tail = basename($path);
  170. /* Make sure we've split the path into two complete components. */
  171. if (empty($tail)) {
  172. $head = dirname($path);
  173. $tail = basename($path);
  174. }
  175. /* Recurse up the path if our current segment does not exist. */
  176. if (!empty($head) && !empty($tail) && !is_dir($head)) {
  177. $this->_mkpath($head, $mode);
  178. }
  179. /* Create this segment of the path. */
  180. return @mkdir($head, $mode);
  181. }
  182. /**
  183. * Opens the log file for output. If the specified log file does not
  184. * already exist, it will be created. By default, new log entries are
  185. * appended to the end of the log file.
  186. *
  187. * This is implicitly called by log(), if necessary.
  188. *
  189. * @access public
  190. */
  191. function open()
  192. {
  193. if (!$this->_opened) {
  194. /* If the log file's directory doesn't exist, create it. */
  195. if (!is_dir(dirname($this->_filename))) {
  196. $this->_mkpath($this->_filename, $this->_dirmode);
  197. }
  198. /* Determine whether the log file needs to be created. */
  199. $creating = !file_exists($this->_filename);
  200. /* Obtain a handle to the log file. */
  201. $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w');
  202. /* We consider the file "opened" if we have a valid file pointer. */
  203. $this->_opened = ($this->_fp !== false);
  204. /* Attempt to set the file's permissions if we just created it. */
  205. if ($creating && $this->_opened) {
  206. chmod($this->_filename, $this->_mode);
  207. }
  208. }
  209. return $this->_opened;
  210. }
  211. /**
  212. * Closes the log file if it is open.
  213. *
  214. * @access public
  215. */
  216. function close()
  217. {
  218. /* If the log file is open, close it. */
  219. if ($this->_opened && fclose($this->_fp)) {
  220. $this->_opened = false;
  221. }
  222. return ($this->_opened === false);
  223. }
  224. /**
  225. * Flushes all pending data to the file handle.
  226. *
  227. * @access public
  228. * @since Log 1.8.2
  229. */
  230. function flush()
  231. {
  232. return fflush($this->_fp);
  233. }
  234. /**
  235. * Logs $message to the output window. The message is also passed along
  236. * to any Log_observer instances that are observing this Log.
  237. *
  238. * @param mixed $message String or object containing the message to log.
  239. * @param string $priority The priority of the message. Valid
  240. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  241. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  242. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  243. * @return boolean True on success or false on failure.
  244. * @access public
  245. */
  246. function log($message, $priority = null)
  247. {
  248. /* If a priority hasn't been specified, use the default value. */
  249. if ($priority === null) {
  250. $priority = $this->_priority;
  251. }
  252. /* Abort early if the priority is above the maximum logging level. */
  253. if (!$this->_isMasked($priority)) {
  254. return false;
  255. }
  256. /* If the log file isn't already open, open it now. */
  257. if (!$this->_opened && !$this->open()) {
  258. return false;
  259. }
  260. /* Extract the string representation of the message. */
  261. $message = $this->_extractMessage($message);
  262. /* Build the string containing the complete log line. */
  263. $line = sprintf($this->_lineFormat, strftime($this->_timeFormat),
  264. $this->_ident, $this->priorityToString($priority),
  265. $message) . $this->_eol;
  266. /* If locking is enabled, acquire an exclusive lock on the file. */
  267. if ($this->_locking) {
  268. flock($this->_fp, LOCK_EX);
  269. }
  270. /* Write the log line to the log file. */
  271. $success = (fwrite($this->_fp, $line) !== false);
  272. /* Unlock the file now that we're finished writing to it. */
  273. if ($this->_locking) {
  274. flock($this->_fp, LOCK_UN);
  275. }
  276. /* Notify observers about this log message. */
  277. $this->_announce(array('priority' => $priority, 'message' => $message));
  278. return $success;
  279. }
  280. }