PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/log4php.debug/helpers/LoggerPatternParser.php

https://bitbucket.org/thomashii/vtigercrm-6-for-postgresql
PHP | 419 lines | 329 code | 20 blank | 70 comment | 14 complexity | af6da90a41e4af2ec65a2baa924aa69e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, LGPL-2.1, GPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage helpers
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. if (!defined('LOG4PHP_LINE_SEP')) {
  24. if (substr(php_uname(), 0, 7) == "Windows") {
  25. /**
  26. * @ignore
  27. */
  28. define('LOG4PHP_LINE_SEP', "\r\n");
  29. } else {
  30. /**
  31. * @ignore
  32. */
  33. define('LOG4PHP_LINE_SEP', "\n");
  34. }
  35. }
  36. /**
  37. */
  38. require_once(LOG4PHP_DIR . '/helpers/LoggerFormattingInfo.php');
  39. require_once(LOG4PHP_DIR . '/helpers/LoggerPatternConverter.php');
  40. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  41. define('LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR', '%');
  42. define('LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE', 0);
  43. define('LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE', 1);
  44. define('LOG4PHP_LOGGER_PATTERN_PARSER_MINUS_STATE', 2);
  45. define('LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE', 3);
  46. define('LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE', 4);
  47. define('LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE', 5);
  48. define('LOG4PHP_LOGGER_PATTERN_PARSER_FULL_LOCATION_CONVERTER', 1000);
  49. define('LOG4PHP_LOGGER_PATTERN_PARSER_METHOD_LOCATION_CONVERTER', 1001);
  50. define('LOG4PHP_LOGGER_PATTERN_PARSER_CLASS_LOCATION_CONVERTER', 1002);
  51. define('LOG4PHP_LOGGER_PATTERN_PARSER_FILE_LOCATION_CONVERTER', 1003);
  52. define('LOG4PHP_LOGGER_PATTERN_PARSER_LINE_LOCATION_CONVERTER', 1004);
  53. define('LOG4PHP_LOGGER_PATTERN_PARSER_RELATIVE_TIME_CONVERTER', 2000);
  54. define('LOG4PHP_LOGGER_PATTERN_PARSER_THREAD_CONVERTER', 2001);
  55. define('LOG4PHP_LOGGER_PATTERN_PARSER_LEVEL_CONVERTER', 2002);
  56. define('LOG4PHP_LOGGER_PATTERN_PARSER_NDC_CONVERTER', 2003);
  57. define('LOG4PHP_LOGGER_PATTERN_PARSER_MESSAGE_CONVERTER', 2004);
  58. define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601', 'Y-m-d H:i:s,u');
  59. define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ABSOLUTE', 'H:i:s');
  60. define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_DATE', 'd M Y H:i:s,u');
  61. /**
  62. * Most of the work of the {@link LoggerPatternLayout} class
  63. * is delegated to the {@link LoggerPatternParser} class.
  64. *
  65. * <p>It is this class that parses conversion patterns and creates
  66. * a chained list of {@link LoggerPatternConverter} converters.</p>
  67. *
  68. * @author VxR <vxr@vxr.it>
  69. * @version $Revision: 1.10 $
  70. * @package log4php
  71. * @subpackage helpers
  72. *
  73. * @since 0.3
  74. */
  75. class LoggerPatternParser {
  76. var $state;
  77. var $currentLiteral;
  78. var $patternLength;
  79. var $i;
  80. /**
  81. * @var LoggerPatternConverter
  82. */
  83. var $head = null;
  84. /**
  85. * @var LoggerPatternConverter
  86. */
  87. var $tail = null;
  88. /**
  89. * @var LoggerFormattingInfo
  90. */
  91. var $formattingInfo;
  92. /**
  93. * @var string pattern to parse
  94. */
  95. var $pattern;
  96. /**
  97. * Constructor
  98. *
  99. * @param string $pattern
  100. */
  101. function LoggerPatternParser($pattern)
  102. {
  103. LoggerLog::debug("LoggerPatternParser::LoggerPatternParser() pattern='$pattern'");
  104. $this->pattern = $pattern;
  105. $this->patternLength = strlen($pattern);
  106. $this->formattingInfo = new LoggerFormattingInfo();
  107. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
  108. }
  109. /**
  110. * @param LoggerPatternConverter $pc
  111. */
  112. function addToList($pc)
  113. {
  114. // LoggerLog::debug("LoggerPatternParser::addToList()");
  115. if($this->head == null) {
  116. $this->head = $pc;
  117. $this->tail =& $this->head;
  118. } else {
  119. $this->tail->next = $pc;
  120. $this->tail =& $this->tail->next;
  121. }
  122. }
  123. /**
  124. * @return string
  125. */
  126. function extractOption()
  127. {
  128. if(($this->i < $this->patternLength) and ($this->pattern{$this->i} == '{')) {
  129. $end = strpos($this->pattern, '}' , $this->i);
  130. if ($end !== false) {
  131. $r = substr($this->pattern, ($this->i + 1), ($end - $this->i - 1));
  132. $this->i= $end + 1;
  133. return $r;
  134. }
  135. }
  136. return null;
  137. }
  138. /**
  139. * The option is expected to be in decimal and positive. In case of
  140. * error, zero is returned.
  141. */
  142. function extractPrecisionOption()
  143. {
  144. $opt = $this->extractOption();
  145. $r = 0;
  146. if ($opt !== null) {
  147. if (is_numeric($opt)) {
  148. $r = (int)$opt;
  149. if($r <= 0) {
  150. LoggerLog::warn("Precision option ({$opt}) isn't a positive integer.");
  151. $r = 0;
  152. }
  153. } else {
  154. LoggerLog::warn("Category option \"{$opt}\" not a decimal integer.");
  155. }
  156. }
  157. return $r;
  158. }
  159. function parse()
  160. {
  161. LoggerLog::debug("LoggerPatternParser::parse()");
  162. $c = '';
  163. $this->i = 0;
  164. $this->currentLiteral = '';
  165. while ($this->i < $this->patternLength) {
  166. $c = $this->pattern{$this->i++};
  167. // LoggerLog::debug("LoggerPatternParser::parse() char is now '$c' and currentLiteral is '{$this->currentLiteral}'");
  168. switch($this->state) {
  169. case LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE:
  170. // LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE'");
  171. // In literal state, the last char is always a literal.
  172. if($this->i == $this->patternLength) {
  173. $this->currentLiteral .= $c;
  174. continue;
  175. }
  176. if($c == LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR) {
  177. // LoggerLog::debug("LoggerPatternParser::parse() char is an escape char");
  178. // peek at the next char.
  179. switch($this->pattern{$this->i}) {
  180. case LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR:
  181. // LoggerLog::debug("LoggerPatternParser::parse() next char is an escape char");
  182. $this->currentLiteral .= $c;
  183. $this->i++; // move pointer
  184. break;
  185. case 'n':
  186. // LoggerLog::debug("LoggerPatternParser::parse() next char is 'n'");
  187. $this->currentLiteral .= LOG4PHP_LINE_SEP;
  188. $this->i++; // move pointer
  189. break;
  190. default:
  191. if(strlen($this->currentLiteral) != 0) {
  192. $this->addToList(new LoggerLiteralPatternConverter($this->currentLiteral));
  193. LoggerLog::debug("LoggerPatternParser::parse() Parsed LITERAL converter: \"{$this->currentLiteral}\".");
  194. }
  195. $this->currentLiteral = $c;
  196. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE;
  197. $this->formattingInfo->reset();
  198. }
  199. } else {
  200. $this->currentLiteral .= $c;
  201. }
  202. break;
  203. case LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE:
  204. // LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE'");
  205. $this->currentLiteral .= $c;
  206. switch($c) {
  207. case '-':
  208. $this->formattingInfo->leftAlign = true;
  209. break;
  210. case '.':
  211. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE;
  212. break;
  213. default:
  214. if(ord($c) >= ord('0') and ord($c) <= ord('9')) {
  215. $this->formattingInfo->min = ord($c) - ord('0');
  216. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE;
  217. } else {
  218. $this->finalizeConverter($c);
  219. }
  220. } // switch
  221. break;
  222. case LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE:
  223. // LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE'");
  224. $this->currentLiteral .= $c;
  225. if(ord($c) >= ord('0') and ord($c) <= ord('9')) {
  226. $this->formattingInfo->min = ($this->formattingInfo->min * 10) + (ord(c) - ord('0'));
  227. } elseif ($c == '.') {
  228. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE;
  229. } else {
  230. $this->finalizeConverter($c);
  231. }
  232. break;
  233. case LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE:
  234. // LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE'");
  235. $this->currentLiteral .= $c;
  236. if(ord($c) >= ord('0') and ord($c) <= ord('9')) {
  237. $this->formattingInfo->max = ord($c) - ord('0');
  238. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE;
  239. } else {
  240. LoggerLog::warn("LoggerPatternParser::parse() Error occured in position {$this->i}. Was expecting digit, instead got char \"{$c}\".");
  241. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
  242. }
  243. break;
  244. case LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE:
  245. // LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE'");
  246. $this->currentLiteral .= $c;
  247. if(ord($c) >= ord('0') and ord($c) <= ord('9')) {
  248. $this->formattingInfo->max = ($this->formattingInfo->max * 10) + (ord($c) - ord('0'));
  249. } else {
  250. $this->finalizeConverter($c);
  251. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
  252. }
  253. break;
  254. } // switch
  255. } // while
  256. if(strlen($this->currentLiteral) != 0) {
  257. $this->addToList(new LoggerLiteralPatternConverter($this->currentLiteral));
  258. // LoggerLog::debug("LoggerPatternParser::parse() Parsed LITERAL converter: \"{$this->currentLiteral}\".");
  259. }
  260. return $this->head;
  261. }
  262. function finalizeConverter($c)
  263. {
  264. LoggerLog::debug("LoggerPatternParser::finalizeConverter() with char '$c'");
  265. $pc = null;
  266. switch($c) {
  267. case 'c':
  268. $pc = new LoggerCategoryPatternConverter($this->formattingInfo, $this->extractPrecisionOption());
  269. LoggerLog::debug("LoggerPatternParser::finalizeConverter() CATEGORY converter.");
  270. // $this->formattingInfo->dump();
  271. $this->currentLiteral = '';
  272. break;
  273. case 'C':
  274. $pc = new LoggerClassNamePatternConverter($this->formattingInfo, $this->extractPrecisionOption());
  275. LoggerLog::debug("LoggerPatternParser::finalizeConverter() CLASSNAME converter.");
  276. //$this->formattingInfo->dump();
  277. $this->currentLiteral = '';
  278. break;
  279. case 'd':
  280. $dateFormatStr = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601; // ISO8601_DATE_FORMAT;
  281. $dOpt = $this->extractOption();
  282. if($dOpt !== null)
  283. $dateFormatStr = $dOpt;
  284. if ($dateFormatStr == 'ISO8601') {
  285. $df = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601;
  286. } elseif($dateFormatStr == 'ABSOLUTE') {
  287. $df = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ABSOLUTE;
  288. } elseif($dateFormatStr == 'DATE') {
  289. $df = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_DATE;
  290. } else {
  291. $df = $dateFormatStr;
  292. if ($df == null) {
  293. $df = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601;
  294. }
  295. }
  296. $pc = new LoggerDatePatternConverter($this->formattingInfo, $df);
  297. $this->currentLiteral = '';
  298. break;
  299. case 'F':
  300. $pc = new LoggerLocationPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_FILE_LOCATION_CONVERTER);
  301. LoggerLog::debug("LoggerPatternParser::finalizeConverter() File name converter.");
  302. //formattingInfo.dump();
  303. $this->currentLiteral = '';
  304. break;
  305. case 'l':
  306. $pc = new LoggerLocationPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_FULL_LOCATION_CONVERTER);
  307. LoggerLog::debug("LoggerPatternParser::finalizeConverter() Location converter.");
  308. //formattingInfo.dump();
  309. $this->currentLiteral = '';
  310. break;
  311. case 'L':
  312. $pc = new LoggerLocationPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_LINE_LOCATION_CONVERTER);
  313. LoggerLog::debug("LoggerPatternParser::finalizeConverter() LINE NUMBER converter.");
  314. //formattingInfo.dump();
  315. $this->currentLiteral = '';
  316. break;
  317. case 'm':
  318. $pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_MESSAGE_CONVERTER);
  319. LoggerLog::debug("LoggerPatternParser::finalizeConverter() MESSAGE converter.");
  320. //formattingInfo.dump();
  321. $this->currentLiteral = '';
  322. break;
  323. case 'M':
  324. $pc = new LoggerLocationPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_METHOD_LOCATION_CONVERTER);
  325. //LogLog.debug("METHOD converter.");
  326. //formattingInfo.dump();
  327. $this->currentLiteral = '';
  328. break;
  329. case 'p':
  330. $pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_LEVEL_CONVERTER);
  331. //LogLog.debug("LEVEL converter.");
  332. //formattingInfo.dump();
  333. $this->currentLiteral = '';
  334. break;
  335. case 'r':
  336. $pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_RELATIVE_TIME_CONVERTER);
  337. LoggerLog::debug("LoggerPatternParser::finalizeConverter() RELATIVE TIME converter.");
  338. //formattingInfo.dump();
  339. $this->currentLiteral = '';
  340. break;
  341. case 't':
  342. $pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_THREAD_CONVERTER);
  343. LoggerLog::debug("LoggerPatternParser::finalizeConverter() THREAD converter.");
  344. //formattingInfo.dump();
  345. $this->currentLiteral = '';
  346. break;
  347. case 'u':
  348. if($this->i < $this->patternLength) {
  349. $cNext = $this->pattern{$this->i};
  350. if(ord($cNext) >= ord('0') and ord($cNext) <= ord('9')) {
  351. $pc = new LoggerUserFieldPatternConverter($this->formattingInfo, (string)(ord($cNext) - ord('0')));
  352. LoggerLog::debug("LoggerPatternParser::finalizeConverter() USER converter [{$cNext}].");
  353. // formattingInfo.dump();
  354. $this->currentLiteral = '';
  355. $this->i++;
  356. } else {
  357. LoggerLog::warn("LoggerPatternParser::finalizeConverter() Unexpected char '{$cNext}' at position {$this->i}.");
  358. }
  359. }
  360. break;
  361. case 'x':
  362. $pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_NDC_CONVERTER);
  363. LoggerLog::debug("LoggerPatternParser::finalizeConverter() NDC converter.");
  364. $this->currentLiteral = '';
  365. break;
  366. case 'X':
  367. $xOpt = $this->extractOption();
  368. $pc = new LoggerMDCPatternConverter($this->formattingInfo, $xOpt);
  369. LoggerLog::debug("LoggerPatternParser::finalizeConverter() MDC converter.");
  370. $this->currentLiteral = '';
  371. break;
  372. default:
  373. LoggerLog::warn("LoggerPatternParser::finalizeConverter() Unexpected char [$c] at position {$this->i} in conversion pattern.");
  374. $pc = new LoggerLiteralPatternConverter($this->currentLiteral);
  375. $this->currentLiteral = '';
  376. }
  377. $this->addConverter($pc);
  378. }
  379. function addConverter($pc)
  380. {
  381. $this->currentLiteral = '';
  382. // Add the pattern converter to the list.
  383. $this->addToList($pc);
  384. // Next pattern is assumed to be a literal.
  385. $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
  386. // Reset formatting info
  387. $this->formattingInfo->reset();
  388. }
  389. }
  390. ?>