View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.helpers;
    19
    20  import org.apache.log4j.spi.LoggingEvent;
    21
    22  /***
    23
    24     <p>PatternConverter is an abtract class that provides the
    25     formatting functionality that derived classes need.
    26
    27     <p>Conversion specifiers in a conversion patterns are parsed to
    28     individual PatternConverters. Each of which is responsible for
    29     converting a logging event in a converter specific manner.
    30
    31     @author <a href="mailto:cakalijp@Maritz.com">James P. Cakalic</a>
    32     @author Ceki G&uuml;lc&uuml;
    33
    34     @since 0.8.2
    35   */
    36  public abstract class PatternConverter {
    37    public PatternConverter next;
    38    int min = -1;
    39    int max = 0x7FFFFFFF;
    40    boolean leftAlign = false;
    41
    42    protected
    43    PatternConverter() {  }
    44
    45    protected
    46    PatternConverter(FormattingInfo fi) {
    47      min = fi.min;
    48      max = fi.max;
    49      leftAlign = fi.leftAlign;
    50    }
    51
    52    /***
    53       Derived pattern converters must override this method in order to
    54       convert conversion specifiers in the correct way.
    55    */
    56    abstract
    57    protected
    58    String convert(LoggingEvent event);
    59
    60    /***
    61       A template method for formatting in a converter specific way.
    62     */
    63    public
    64    void format(StringBuffer sbuf, LoggingEvent e) {
    65      String s = convert(e);
    66
    67      if(s == null) {
    68        if(0 < min)
    69  	spacePad(sbuf, min);
    70        return;
    71      }
    72
    73      int len = s.length();
    74
    75      if(len > max)
    76        sbuf.append(s.substring(len-max));
    77      else if(len < min) {
    78        if(leftAlign) {
    79  	sbuf.append(s);
    80  	spacePad(sbuf, min-len);
    81        }
    82        else {
    83  	spacePad(sbuf, min-len);
    84  	sbuf.append(s);
    85        }
    86      }
    87      else
    88        sbuf.append(s);
    89    }
    90
    91    static String[] SPACES = {" ", "  ", "    ", "        ", //1,2,4,8 spaces
    92  			    "                ", // 16 spaces
    93  			    "                                " }; // 32 spaces
    94
    95    /***
    96       Fast space padding method.
    97    */
    98    public
    99    void spacePad(StringBuffer sbuf, int length) {
    100     while(length >= 32) {
    101       sbuf.append(SPACES[5]);
    102       length -= 32;
    103     }
    104
    105     for(int i = 4; i >= 0; i--) {
    106       if((length & (1<<i)) != 0) {
    107 	sbuf.append(SPACES[i]);
    108       }
    109     }
    110   }
    111 }