root/trunk/src/java/org/jcoderz/commons/logging/NestedLineFormat.java

Revision 1011, 5.2 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
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 */
33package org.jcoderz.commons.logging;
34
35
36import java.text.Format;
37import java.text.ParseException;
38import java.util.ArrayList;
39import java.util.List;
40import java.util.logging.LogRecord;
41
42import org.jcoderz.commons.Loggable;
43
44/**
45 * Formats and parses log lines for nested messages.
46 *
47 */
48public final class NestedLineFormat
49      extends ContinuationLineFormat
50{
51   /** The number of fields added to the basic continuation line format.
52     * The cause field is added.
53     */
54   private static final int NUMBER_OF_ADDITIONAL_PARAMETERS = 1;
55
56   private static final int CAUSE_INDEX = NUMBER_OF_PARAMETERS;
57
58   /** The fields added to the basic format. No fields are added. */
59   private static final String ADDITIONAL_LOGLINE_FORMAT_PATTERN
60        = " Caused by: {" + CAUSE_INDEX + "}";
61
62  /**
63    * Creates a new instance of this and initializes the message format.
64    */
65   public NestedLineFormat ()
66   {
67      super(LogLineFormat.NESTED_MESSAGE, ADDITIONAL_LOGLINE_FORMAT_PATTERN,
68            NUMBER_OF_ADDITIONAL_PARAMETERS);
69      setFormats(getFormatList(null, true));
70   }
71
72   /**
73    * Gets the formats as array for formatting a nested line.
74    *
75    * @param options The display options specifying which fields to display.
76    * Will be ignored and ,ight be null if <code>ignoreOptions == true</code>.
77    * @param ignoreOptions flag whether to ignore the supplied options and
78    * return the formats for all fields.
79    *
80    * @return array filled with formats for each selected field. Might be empty
81    * array, never null.
82    */
83   static Format[] getFormatList (
84         final DisplayOptions options,
85         final boolean ignoreOptions)
86   {
87      final List formatList = new ArrayList();
88      formatList.addAll(getBasicFormatList(options, ignoreOptions));
89      formatList.add(new WhitespaceFormat(new AsItIsFormat("\r\n")));
90      return (Format[]) formatList.toArray(EMPTY_FORMATTERS);
91   }
92
93   /**
94    * Formats either a LogRecord or a Loggable as nested message.
95    * Append a line feed after the data has been formatted into the
96    * StringBuffer.
97    *
98    * @param sb The StringBuffer where to append the formatted data.
99    * @param record The LogRecord to format, if <code>loggable</code> is not
100    * null, this is unused and might be null.
101    * @param loggable The Loggable to format, might be null, but then
102    * <code>record</code> must not be null.
103    * @param trackingIdSequence The sequence of contributing tracking ids.
104    * @param thrown Unused, might be null.
105    * @param parameter Gives the cause of the message on top of this and is
106    * the message text to be formatted by this.
107    */
108   public void format (
109         final StringBuffer sb,
110         final LogRecord record,
111         final Loggable loggable,
112         final List trackingIdSequence,
113         final Throwable thrown,
114         final Object parameter)
115   {
116      setCause(parameter.toString());
117      basicFormat(sb, record, loggable, trackingIdSequence);
118   }
119
120   /** {@inheritDoc} */
121   public void parse (StringBuffer sb, LogFileEntry entry)
122         throws ParseException
123   {
124      try
125      {
126         basicParse(sb, entry);
127         entry.setMessage(getCause());
128      }
129      catch (ParseException pex)
130      {
131         // just rethrow
132         throw pex;
133      }
134      catch (Exception ex)
135      {
136         final ParseException pex = new ParseException(
137               "Got an error parsing " + sb, 0);
138         pex.initCause(ex);
139         throw pex;
140      }
141   }
142
143   private void setCause (final String cause)
144   {
145      setParameter(CAUSE_INDEX, cause);
146   }
147
148   private String getCause ()
149   {
150      return (String) getParameter(CAUSE_INDEX);
151   }
152}
Note: See TracBrowser for help on using the browser.