Project Report: fawkez

Packagesummary org.jcoderz.commons.logging

org.jcoderz.commons.logging.StackTraceFormat

LineHitsNoteSource
1  /*
2   * $Id: StackTraceFormat.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.logging;
34  
35  
36  import java.nio.CharBuffer;
37  import java.text.Format;
38  import java.text.ParseException;
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.logging.LogRecord;
42  
43  import org.jcoderz.commons.Loggable;
44 (1)import org.jcoderz.commons.util.StringUtil;
45  
46  /**
47   * This class is used for formatting the stack trace of a Throwable or for
48   * parsing one line of stack trace.
49   *
50   */
51 (2)public final class StackTraceFormat
52        extends ContinuationLineFormat
53  {
54     /** The number of fields added to the basic continuation line format.
55       * The cause field is added.
56       */
57     private static final int NUMBER_OF_ADDITIONAL_PARAMETERS = 1;
58  
59     private static final int TEXT_INDEX = NUMBER_OF_PARAMETERS;
60  
61     /** The fields added to the basic format. Parameter name and parameter value
62      * list are added.
63      */
64     private static final String ADDITIONAL_LOGLINE_FORMAT_PATTERN
65          = " {" + TEXT_INDEX + "}";
66  
67     private static final String CAUSED_BY_CLAUSE = "Caused by: ";
68     private static final String AT_CLAUSE = " at ";
69  
70     /**
71      * Creates a new instance of this and initializes the message format.
72      */
73     public StackTraceFormat ()
74     {
75100       super(LogLineFormat.STACKTRACE_MESSAGE, ADDITIONAL_LOGLINE_FORMAT_PATTERN,
76              NUMBER_OF_ADDITIONAL_PARAMETERS);
77100       setFormats(getFormatList(null, true));
78100    }
79  
80     /**
81      * Gets the formats as array for formatting a stack trace line.
82      *
83      * @param options The display options specifying which fields to display.
84      * Will be ignored and ,ight be null if <code>ignoreOptions == true</code>.
85      * @param ignoreOptions flag whether to ignore the supplied options and
86      * return the formats for all fields.
87      *
88      * @return array filled with formats for each selected field. Might be empty
89      * array, never null.
90      */
91     static Format[] getFormatList (
92           final DisplayOptions options,
93           final boolean ignoreOptions)
94     {
95100       final List formatList = new ArrayList();
9650       if (ignoreOptions || options.displayThreadId())
97        {
98           // thread id
99100          formatList.add(getThreadIdFormat());
100        }
10150       if (ignoreOptions || options.displayTrackingNumber())
102        {
103           // sequence of tracking id
104100          formatList.add(getTrackingNumberFormat());
105        }
106100       formatList.add(new AsItIsFormat("\r\n"));
107100       return (Format[]) formatList.toArray(EMPTY_FORMATTERS);
108     }
109  
110     /**
111      * Formats a Throwable.
112      * Append a line feed after the data has been formatted into the
113      * StringBuffer.
114      *
115      * @param sb The StringBuffer where to append the formatted data.
116      * @param record The LogRecord to format, if <code>loggable</code> is not
117      * null, this is unused and might be null.
118      * @param loggable The Loggable to format, might be null, but then
119      * <code>record</code> must not be null.
120      * @param trackingIdSequence The sequence of contributing tracking ids.
121      * @param thrown The Throwable to format.
122      * @param parameter The Throwable containing <code>thrown</code> as cause.
123      * Might be null, if no such. Must be instance of Throwable if not null.
124      */
125     public void format (
126           final StringBuffer sb,
127           final LogRecord record,
128           final Loggable loggable,
129           final List trackingIdSequence,
130           final Throwable thrown,
131           final Object parameter)
132     {
133100       if ((parameter != null) && ! (parameter instanceof Throwable))
134        {
1350          throw new IllegalArgumentException(
136                 "Parameter must be null or a Throwable, but is: " + parameter);
137        }
138100       final Throwable parentThrown = (Throwable) parameter;
139  
140100       appendThrown(sb, record, loggable, trackingIdSequence, thrown,
141              parentThrown == null);
142100       appendStackTrace(sb, record, loggable, trackingIdSequence, thrown,
143              parentThrown);
144100    }
145  
146     /**
147      * Parses one line of log data and sets the data in the supplied
148      * LogFileEntry.
149      *
150      * @param sb The StringBuffer containing the log line to parse from the
151      * current position to the end.
152      * @param entry The LogFileEntry which gets the data being parsed.
153      *
154      * @throws ParseException if an error occurs parsing the log line.
155      */
156     public void parse (StringBuffer sb, LogFileEntry entry)
157           throws ParseException
158     {
159        try
160        {
1610          parse(sb);
1620          entry.addToStackTrace(
163                 StackTraceElementParser.parse(CharBuffer.wrap(getText())));
164        }
1650       catch (ParseException pex)
166        {
167           // just rethrow
1680          throw pex;
169        }
1700       catch (Exception ex)
171        {
1720          final ParseException pex = new ParseException(
173                 "Got an error parsing " + sb, 0);
1740          pex.initCause(ex);
1750          throw pex;
1760       }
1770    }
178  
179     private void appendThrown (
180           final StringBuffer sb,
181           final LogRecord record,
182           final Loggable loggable,
183           final List trackingIdSequence,
184           final Throwable thrown,
185           final boolean topLevel)
186     {
187100       final StringBuffer text = new StringBuffer();
188100       if (! topLevel)
189        {
190100          text.append(CAUSED_BY_CLAUSE);
191        }
192100       text.append(thrown.toString());
193100       setText(text.toString());
194100       basicFormat(sb, record, loggable, trackingIdSequence);
195100    }
196  
197     private void appendStackTrace (
198           final StringBuffer sb,
199           final LogRecord record,
200           final Loggable loggable,
201           final List trackingIdSequence,
202           final Throwable thrown,
203           final Throwable outerTrace)
204     {
205        // Compute number of frames in common between this and caused
206100       final StackTraceElement[] trace = thrown.getStackTrace();
207        final int framesInCommon;
208100       int uniqueFrames = trace.length - 1;
209100       if (outerTrace != null)
210        {
211100          final StackTraceElement[] causedTrace = outerTrace.getStackTrace();
212100          int n = causedTrace.length - 1;
213           while (uniqueFrames >= 0 && n >= 0
214100                && trace[uniqueFrames].equals(causedTrace[n]))
215           {
216100             uniqueFrames--; n--;
217           }
218100          framesInCommon = trace.length - 1 - uniqueFrames;
219100       }
220        else
221        {
222100          framesInCommon = 0;
223        }
224  
225100       for (int i = 0; i <= uniqueFrames; ++i)
226        {
227  
228  //         sb.append(Constants.LINE_SEPARATOR);
229  
230100          setText(AT_CLAUSE + trace[i].toString());
231  
232100          basicFormat(sb, record, loggable, trackingIdSequence);
233        }
234100       if (framesInCommon != 0)
235        {
236  //         sb.append(Constants.LINE_SEPARATOR);
237100          setText("..." + framesInCommon + " more");
238100          basicFormat(sb, record, loggable, trackingIdSequence);
239  //         format(sb);
240        }
241100    }
242  
243     private void setText (final String text)
244     {
245100       setParameter(TEXT_INDEX, text);
246100    }
247  
248     private String getText ()
249     {
2500       return (String) getParameter(TEXT_INDEX);
251     }
252  }

Findings in this File

c (1) 44 : 8 Unused import - org.jcoderz.commons.util.StringUtil.
c (2) 51 : 0 Type Javadoc comment is missing an @author tag.