PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/Logger.php

https://bitbucket.org/dkrzos/phc
PHP | 268 lines | 177 code | 53 blank | 38 comment | 14 complexity | 317dfd9569fb1169121809b4b7bcf36e MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * $Id: Logger.class.php 10 2007-05-27 10:55:12Z spocke $
  4. *
  5. * @package MCFileManager.filesystems
  6. * @author Moxiecode
  7. * @copyright Copyright © 2005, Moxiecode Systems AB, All rights reserved.
  8. */
  9. // File type contstants
  10. define('MC_LOGGER_DEBUG', 0);
  11. define('MC_LOGGER_INFO', 10);
  12. define('MC_LOGGER_WARN', 20);
  13. define('MC_LOGGER_ERROR', 30);
  14. define('MC_LOGGER_FATAL', 40);
  15. /**
  16. * Logging utility class. This class handles basic logging with levels, log rotation and custom log formats. It's
  17. * designed to be compact but still powerful and flexible.
  18. */
  19. class Moxiecode_Logger {
  20. // Private fields
  21. var $_path;
  22. var $_filename;
  23. var $_maxSize;
  24. var $_maxFiles;
  25. var $_maxSizeBytes;
  26. var $_level;
  27. var $_format;
  28. /**
  29. * Constructs a new logger instance.
  30. */
  31. function Moxiecode_Logger() {
  32. $this->_path = "";
  33. $this->_filename = "{level}.log";
  34. $this->setMaxSize("100k");
  35. $this->_maxFiles = 10;
  36. $this->_level = MC_LOGGER_DEBUG;
  37. $this->_format = "[{time}] [{level}] {message}";
  38. }
  39. /**
  40. * Sets the current log level, use the MC_LOGGER constants.
  41. *
  42. * @param int $level Log level instance for example MC_LOGGER_DEBUG.
  43. */
  44. function setLevel($level) {
  45. if (is_string($level)) {
  46. switch (strtolower($level)) {
  47. case "debug":
  48. $level = MC_LOGGER_DEBUG;
  49. break;
  50. case "info":
  51. $level = MC_LOGGER_INFO;
  52. break;
  53. case "warn":
  54. case "warning":
  55. $level = MC_LOGGER_WARN;
  56. break;
  57. case "error":
  58. $level = MC_LOGGER_ERROR;
  59. break;
  60. case "fatal":
  61. $level = MC_LOGGER_FATAL;
  62. break;
  63. default:
  64. $level = MC_LOGGER_FATAL;
  65. }
  66. }
  67. $this->_level = $level;
  68. }
  69. /**
  70. * Returns the current log level for example MC_LOGGER_DEBUG.
  71. *
  72. * @return int Current log level for example MC_LOGGER_DEBUG.
  73. */
  74. function getLevel() {
  75. return $this->_level;
  76. }
  77. function setPath($path) {
  78. $this->_path = $path;
  79. }
  80. function getPath() {
  81. return $this->_path;
  82. }
  83. function setFileName($file_name) {
  84. $this->_filename = $file_name;
  85. }
  86. function getFileName() {
  87. return $this->_filename;
  88. }
  89. function setFormat($format) {
  90. $this->_format = $format;
  91. }
  92. function getFormat() {
  93. return $this->_format;
  94. }
  95. function setMaxSize($size) {
  96. // Fix log max size
  97. $logMaxSizeBytes = intval(preg_replace("/[^0-9]/", "", $size));
  98. // Is KB
  99. if (strpos((strtolower($size)), "k") > 0)
  100. $logMaxSizeBytes *= 1024;
  101. // Is MB
  102. if (strpos((strtolower($size)), "m") > 0)
  103. $logMaxSizeBytes *= (1024 * 1024);
  104. $this->_maxSizeBytes = $logMaxSizeBytes;
  105. $this->_maxSize = $size;
  106. }
  107. function getMaxSize() {
  108. return $this->_maxSize;
  109. }
  110. function setMaxFiles($max_files) {
  111. $this->_maxFiles = $max_files;
  112. }
  113. function getMaxFiles() {
  114. return $this->_maxFiles;
  115. }
  116. function debug($msg) {
  117. $args = func_get_args();
  118. $this->_logMsg(MC_LOGGER_DEBUG, implode(', ', $args));
  119. }
  120. function info($msg) {
  121. $args = func_get_args();
  122. $this->_logMsg(MC_LOGGER_INFO, implode(', ', $args));
  123. }
  124. function warn($msg) {
  125. $args = func_get_args();
  126. $this->_logMsg(MC_LOGGER_WARN, implode(', ', $args));
  127. }
  128. function error($msg) {
  129. $args = func_get_args();
  130. $this->_logMsg(MC_LOGGER_ERROR, implode(', ', $args));
  131. }
  132. function fatal($msg) {
  133. $args = func_get_args();
  134. $this->_logMsg(MC_LOGGER_FATAL, implode(', ', $args));
  135. }
  136. function isDebugEnabled() {
  137. return $this->_level >= MC_LOGGER_DEBUG;
  138. }
  139. function isInfoEnabled() {
  140. return $this->_level >= MC_LOGGER_INFO;
  141. }
  142. function isWarnEnabled() {
  143. return $this->_level >= MC_LOGGER_WARN;
  144. }
  145. function isErrorEnabled() {
  146. return $this->_level >= MC_LOGGER_ERROR;
  147. }
  148. function isFatalEnabled() {
  149. return $this->_level >= MC_LOGGER_FATAL;
  150. }
  151. function _logMsg($level, $message) {
  152. $roll = false;
  153. if ($level < $this->_level)
  154. return;
  155. $logFile = $this->toOSPath($this->_path . "/" . $this->_filename);
  156. switch ($level) {
  157. case MC_LOGGER_DEBUG:
  158. $levelName = "DEBUG";
  159. break;
  160. case MC_LOGGER_INFO:
  161. $levelName = "INFO";
  162. break;
  163. case MC_LOGGER_WARN:
  164. $levelName = "WARN";
  165. break;
  166. case MC_LOGGER_ERROR:
  167. $levelName = "ERROR";
  168. break;
  169. case MC_LOGGER_FATAL:
  170. $levelName = "FATAL";
  171. break;
  172. }
  173. $logFile = str_replace('{level}', strtolower($levelName), $logFile);
  174. $text = $this->_format;
  175. $text = str_replace('{time}', date("Y-m-d H:i:s"), $text);
  176. $text = str_replace('{level}', strtolower($levelName), $text);
  177. $text = str_replace('{message}', $message, $text);
  178. $message = $text . "\r\n";
  179. // Check filesize
  180. if (file_exists($logFile)) {
  181. $size = @filesize($logFile);
  182. if ($size + strlen($message) > $this->_maxSizeBytes)
  183. $roll = true;
  184. }
  185. // Roll if the size is right
  186. if ($roll) {
  187. for ($i=$this->_maxFiles-1; $i>=1; $i--) {
  188. $rfile = $this->toOSPath($logFile . "." . $i);
  189. $nfile = $this->toOSPath($logFile . "." . ($i+1));
  190. if (@file_exists($rfile))
  191. @rename($rfile, $nfile);
  192. }
  193. @rename($logFile, $this->toOSPath($logFile . ".1"));
  194. // Delete last logfile
  195. $delfile = $this->toOSPath($logFile . "." . ($this->_maxFiles + 1));
  196. if (@file_exists($delfile))
  197. @unlink($delfile);
  198. }
  199. // Append log line
  200. if (($fp = @fopen($logFile, "a")) != null) {
  201. @fputs($fp, $message);
  202. @fflush($fp);
  203. @fclose($fp);
  204. }
  205. }
  206. /**
  207. * Converts a Unix path to OS specific path.
  208. *
  209. * @param String $path Unix path to convert.
  210. */
  211. function toOSPath($path) {
  212. return str_replace("/", DIRECTORY_SEPARATOR, $path);
  213. }
  214. }
  215. ?>