PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/pacore/ext/Log/file.php

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