loggerpatternparser.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 409 行 · 第 1/2 页

PHP
409
字号
<?php
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * @package log4php
 * @subpackage helpers
 */

/**
 * @ignore 
 */
if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');

if (!defined('LOG4PHP_LINE_SEP')) {
    if (substr(php_uname(), 0, 7) == "Windows") {
        /**
         * @ignore
         */
        define('LOG4PHP_LINE_SEP', "\r\n");
    } else {
        /**
         * @ignore
         */
        define('LOG4PHP_LINE_SEP', "\n");
    }
}
 
/**
 */
require_once(LOG4PHP_DIR . '/helpers/LoggerFormattingInfo.php');
require_once(LOG4PHP_DIR . '/helpers/LoggerPatternConverter.php');
require_once(LOG4PHP_DIR . '/LoggerLog.php');

define('LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR',         '%');

define('LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE',       0);
define('LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE',     1);
define('LOG4PHP_LOGGER_PATTERN_PARSER_MINUS_STATE',         2);
define('LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE',           3);
define('LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE',           4);
define('LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE',           5);

define('LOG4PHP_LOGGER_PATTERN_PARSER_FULL_LOCATION_CONVERTER',         1000);
define('LOG4PHP_LOGGER_PATTERN_PARSER_METHOD_LOCATION_CONVERTER',       1001);
define('LOG4PHP_LOGGER_PATTERN_PARSER_CLASS_LOCATION_CONVERTER',        1002);
define('LOG4PHP_LOGGER_PATTERN_PARSER_FILE_LOCATION_CONVERTER',         1003);
define('LOG4PHP_LOGGER_PATTERN_PARSER_LINE_LOCATION_CONVERTER',         1004);

define('LOG4PHP_LOGGER_PATTERN_PARSER_RELATIVE_TIME_CONVERTER',         2000);
define('LOG4PHP_LOGGER_PATTERN_PARSER_THREAD_CONVERTER',                2001);
define('LOG4PHP_LOGGER_PATTERN_PARSER_LEVEL_CONVERTER',                 2002);
define('LOG4PHP_LOGGER_PATTERN_PARSER_NDC_CONVERTER',                   2003);
define('LOG4PHP_LOGGER_PATTERN_PARSER_MESSAGE_CONVERTER',               2004);

define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601',    'Y-m-d H:i:s,u'); 
define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ABSOLUTE',   'H:i:s');
define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_DATE',       'd M Y H:i:s,u');

/**
 * Most of the work of the {@link LoggerPatternLayout} class 
 * is delegated to the {@link LoggerPatternParser} class.
 * 
 * <p>It is this class that parses conversion patterns and creates
 * a chained list of {@link LoggerPatternConverter} converters.</p>
 * 
 * @author  Marco Vassura
 * @version $Revision: 635069 $ 
 * @package log4php
 * @subpackage helpers
 *
 * @since 0.3
 */
class LoggerPatternParser {

    var $state;
    var $currentLiteral;
    var $patternLength;
    var $i;
    
    /**
     * @var LoggerPatternConverter
     */
    var $head = null;
     
    /**
     * @var LoggerPatternConverter
     */
    var $tail = null;
    
    /**
     * @var LoggerFormattingInfo
     */
    var $formattingInfo;
    
    /**
     * @var string pattern to parse
     */
    var $pattern;

    /**
     * Constructor 
     *
     * @param string $pattern
     */
    function LoggerPatternParser($pattern)
    {
        LoggerLog::debug("LoggerPatternParser::LoggerPatternParser() pattern='$pattern'");
    
        $this->pattern = $pattern;
        $this->patternLength =  strlen($pattern);
        $this->formattingInfo = new LoggerFormattingInfo();
        $this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
    }

    /**
     * @param LoggerPatternConverter $pc
     */
    function addToList($pc)
    {
        // LoggerLog::debug("LoggerPatternParser::addToList()");
    
        if($this->head == null) {
            $this->head = $pc;
            $this->tail =& $this->head;
        } else {
            $this->tail->next = $pc;
            $this->tail =& $this->tail->next;
        }
    }

    /**
     * @return string
     */
    function extractOption()
    {
        if(($this->i < $this->patternLength) and ($this->pattern{$this->i} == '{')) {
            $end = strpos($this->pattern, '}' , $this->i);
            if ($end !== false) {
                $r = substr($this->pattern, ($this->i + 1), ($end - $this->i - 1));
                    $this->i= $end + 1;
                    return $r;
            }
        }
        return null;
    }

    /**
     * The option is expected to be in decimal and positive. In case of
     * error, zero is returned.  
     */
    function extractPrecisionOption()
    {
        $opt = $this->extractOption();
        $r = 0;
        if ($opt !== null) {
            if (is_numeric($opt)) {
                $r = (int)$opt;
                if($r <= 0) {
                    LoggerLog::warn("Precision option ({$opt}) isn't a positive integer.");
                    $r = 0;
                }
            } else {
                LoggerLog::warn("Category option \"{$opt}\" not a decimal integer.");
            }
        }
        return $r;
    }

    function parse()
    {
        LoggerLog::debug("LoggerPatternParser::parse()");
    
        $c = '';
        $this->i = 0;
        $this->currentLiteral = '';
        while ($this->i < $this->patternLength) {
            $c = $this->pattern{$this->i++};
//            LoggerLog::debug("LoggerPatternParser::parse() char is now '$c' and currentLiteral is '{$this->currentLiteral}'");            
            switch($this->state) {
                case LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE:
                    // LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE'");
                    // In literal state, the last char is always a literal.
                    if($this->i == $this->patternLength) {
                        $this->currentLiteral .= $c;
                        continue;
                    }
                    if($c == LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR) {
                        // LoggerLog::debug("LoggerPatternParser::parse() char is an escape char");                    
                        // peek at the next char.
                        switch($this->pattern{$this->i}) {
                            case LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?