includes/clientside/tinymce/plugins/spellchecker/classes/utils/Logger.php
changeset 1344 dc96d6c5cd1e
parent 1343 2a31905a567d
child 1345 1de01205143b
equal deleted inserted replaced
1343:2a31905a567d 1344:dc96d6c5cd1e
     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 
       
    10 // File type contstants
       
    11 define('MC_LOGGER_DEBUG', 0);
       
    12 define('MC_LOGGER_INFO', 10);
       
    13 define('MC_LOGGER_WARN', 20);
       
    14 define('MC_LOGGER_ERROR', 30);
       
    15 define('MC_LOGGER_FATAL', 40);
       
    16 
       
    17 /**
       
    18  * Logging utility class. This class handles basic logging with levels, log rotation and custom log formats. It's
       
    19  * designed to be compact but still powerful and flexible.
       
    20  */
       
    21 class Moxiecode_Logger {
       
    22 	// Private fields
       
    23 	var $_path;
       
    24 	var $_filename;
       
    25 	var $_maxSize;
       
    26 	var $_maxFiles;
       
    27 	var $_maxSizeBytes;
       
    28 	var $_level;
       
    29 	var $_format;
       
    30 
       
    31 	/**
       
    32 	 * Constructs a new logger instance.
       
    33 	 */
       
    34 	function Moxiecode_Logger() {
       
    35 		$this->_path = "";
       
    36 		$this->_filename = "{level}.log";
       
    37 		$this->setMaxSize("100k");
       
    38 		$this->_maxFiles = 10;
       
    39 		$this->_level = MC_LOGGER_DEBUG;
       
    40 		$this->_format = "[{time}] [{level}] {message}";
       
    41 	}
       
    42 
       
    43 	/**
       
    44 	 * Sets the current log level, use the MC_LOGGER constants.
       
    45 	 *
       
    46 	 * @param int $level Log level instance for example MC_LOGGER_DEBUG.
       
    47 	 */
       
    48 	function setLevel($level) {
       
    49 		if (is_string($level)) {
       
    50 			switch (strtolower($level)) {
       
    51 				case "debug":
       
    52 					$level = MC_LOGGER_DEBUG;
       
    53 					break;
       
    54 
       
    55 				case "info":
       
    56 					$level = MC_LOGGER_INFO;
       
    57 					break;
       
    58 
       
    59 				case "warn":
       
    60 				case "warning":
       
    61 					$level = MC_LOGGER_WARN;
       
    62 					break;
       
    63 
       
    64 				case "error":
       
    65 					$level = MC_LOGGER_ERROR;
       
    66 					break;
       
    67 
       
    68 				case "fatal":
       
    69 					$level = MC_LOGGER_FATAL;
       
    70 					break;
       
    71 
       
    72 				default:
       
    73 					$level = MC_LOGGER_FATAL;
       
    74 			}
       
    75 		}
       
    76 
       
    77 		$this->_level = $level;
       
    78 	}
       
    79 
       
    80 	/**
       
    81 	 * Returns the current log level for example MC_LOGGER_DEBUG.
       
    82 	 *
       
    83 	 * @return int Current log level for example MC_LOGGER_DEBUG.
       
    84 	 */
       
    85 	function getLevel() {
       
    86 		return $this->_level;
       
    87 	}
       
    88 
       
    89 	function setPath($path) {
       
    90 		$this->_path = $path;
       
    91 	}
       
    92 
       
    93 	function getPath() {
       
    94 		return $this->_path;
       
    95 	}
       
    96 
       
    97 	function setFileName($file_name) {
       
    98 		$this->_filename = $file_name;
       
    99 	}
       
   100 
       
   101 	function getFileName() {
       
   102 		return $this->_filename;
       
   103 	}
       
   104 
       
   105 	function setFormat($format) {
       
   106 		$this->_format = $format;
       
   107 	}
       
   108 
       
   109 	function getFormat() {
       
   110 		return $this->_format;
       
   111 	}
       
   112 
       
   113 	function setMaxSize($size) {
       
   114 		// Fix log max size
       
   115 		$logMaxSizeBytes = intval(preg_replace("/[^0-9]/", "", $size));
       
   116 
       
   117 		// Is KB
       
   118 		if (strpos((strtolower($size)), "k") > 0)
       
   119 			$logMaxSizeBytes *= 1024;
       
   120 
       
   121 		// Is MB
       
   122 		if (strpos((strtolower($size)), "m") > 0)
       
   123 			$logMaxSizeBytes *= (1024 * 1024);
       
   124 
       
   125 		$this->_maxSizeBytes = $logMaxSizeBytes;
       
   126 		$this->_maxSize = $size;
       
   127 	}
       
   128 
       
   129 	function getMaxSize() {
       
   130 		return $this->_maxSize;
       
   131 	}
       
   132 
       
   133 	function setMaxFiles($max_files) {
       
   134 		$this->_maxFiles = $max_files;
       
   135 	}
       
   136 
       
   137 	function getMaxFiles() {
       
   138 		return $this->_maxFiles;
       
   139 	}
       
   140 
       
   141 	function debug($msg) {
       
   142 		$args = func_get_args();
       
   143 		$this->_logMsg(MC_LOGGER_DEBUG, implode(', ', $args));
       
   144 	}
       
   145 
       
   146 	function info($msg) {
       
   147 		$args = func_get_args();
       
   148 		$this->_logMsg(MC_LOGGER_INFO, implode(', ', $args));
       
   149 	}
       
   150 
       
   151 	function warn($msg) {
       
   152 		$args = func_get_args();
       
   153 		$this->_logMsg(MC_LOGGER_WARN, implode(', ', $args));
       
   154 	}
       
   155 
       
   156 	function error($msg) {
       
   157 		$args = func_get_args();
       
   158 		$this->_logMsg(MC_LOGGER_ERROR, implode(', ', $args));
       
   159 	}
       
   160 
       
   161 	function fatal($msg) {
       
   162 		$args = func_get_args();
       
   163 		$this->_logMsg(MC_LOGGER_FATAL, implode(', ', $args));
       
   164 	}
       
   165 
       
   166 	function isDebugEnabled() {
       
   167 		return $this->_level >= MC_LOGGER_DEBUG;
       
   168 	}
       
   169 
       
   170 	function isInfoEnabled() {
       
   171 		return $this->_level >= MC_LOGGER_INFO;
       
   172 	}
       
   173 
       
   174 	function isWarnEnabled() {
       
   175 		return $this->_level >= MC_LOGGER_WARN;
       
   176 	}
       
   177 
       
   178 	function isErrorEnabled() {
       
   179 		return $this->_level >= MC_LOGGER_ERROR;
       
   180 	}
       
   181 
       
   182 	function isFatalEnabled() {
       
   183 		return $this->_level >= MC_LOGGER_FATAL;
       
   184 	}
       
   185 
       
   186 	function _logMsg($level, $message) {
       
   187 		$roll = false;
       
   188 
       
   189 		if ($level < $this->_level)
       
   190 			return;
       
   191 
       
   192 		$logFile = $this->toOSPath($this->_path . "/" . $this->_filename);
       
   193 
       
   194 		switch ($level) {
       
   195 			case MC_LOGGER_DEBUG:
       
   196 				$levelName = "DEBUG";
       
   197 				break;
       
   198 
       
   199 			case MC_LOGGER_INFO:
       
   200 				$levelName = "INFO";
       
   201 				break;
       
   202 
       
   203 			case MC_LOGGER_WARN:
       
   204 				$levelName = "WARN";
       
   205 				break;
       
   206 
       
   207 			case MC_LOGGER_ERROR:
       
   208 				$levelName = "ERROR";
       
   209 				break;
       
   210 
       
   211 			case MC_LOGGER_FATAL:
       
   212 				$levelName = "FATAL";
       
   213 				break;
       
   214 		}
       
   215 
       
   216 		$logFile = str_replace('{level}', strtolower($levelName), $logFile);
       
   217 
       
   218 		$text = $this->_format;
       
   219 		$text = str_replace('{time}', date("Y-m-d H:i:s"), $text);
       
   220 		$text = str_replace('{level}', strtolower($levelName), $text);
       
   221 		$text = str_replace('{message}', $message, $text);
       
   222 		$message = $text . "\r\n";
       
   223 
       
   224 		// Check filesize
       
   225 		if (file_exists($logFile)) {
       
   226 			$size = @filesize($logFile);
       
   227 
       
   228 			if ($size + strlen($message) > $this->_maxSizeBytes)
       
   229 				$roll = true;
       
   230 		}
       
   231 
       
   232 		// Roll if the size is right
       
   233 		if ($roll) {
       
   234 			for ($i=$this->_maxFiles-1; $i>=1; $i--) {
       
   235 				$rfile = $this->toOSPath($logFile . "." . $i);
       
   236 				$nfile = $this->toOSPath($logFile . "." . ($i+1));
       
   237 
       
   238 				if (@file_exists($rfile))
       
   239 					@rename($rfile, $nfile);
       
   240 			}
       
   241 
       
   242 			@rename($logFile, $this->toOSPath($logFile . ".1"));
       
   243 
       
   244 			// Delete last logfile
       
   245 			$delfile = $this->toOSPath($logFile . "." . ($this->_maxFiles + 1));
       
   246 			if (@file_exists($delfile))
       
   247 				@unlink($delfile);
       
   248 		}
       
   249 
       
   250 		// Append log line
       
   251 		if (($fp = @fopen($logFile, "a")) != null) {
       
   252 			@fputs($fp, $message);
       
   253 			@fflush($fp);
       
   254 			@fclose($fp);
       
   255 		}
       
   256 	}
       
   257 
       
   258 	/**
       
   259 	 * Converts a Unix path to OS specific path.
       
   260 	 *
       
   261 	 * @param String $path Unix path to convert.
       
   262 	 */
       
   263 	function toOSPath($path) {
       
   264 		return str_replace("/", DIRECTORY_SEPARATOR, $path);
       
   265 	}
       
   266 }
       
   267 
       
   268 ?>