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

Revision 1011, 6.3 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.List;
39import java.util.logging.LogRecord;
40
41import org.jcoderz.commons.Loggable;
42
43/**
44 * This class formats and parses log lines for instances of type Loggable.
45 *
46 */
47public class MessageLineFormat
48      extends BasicLogLineFormat
49{
50   /** The number of fields added to the basic format.
51       The text field is added. */
52   private static final int NUMBER_OF_ADDITIONAL_PARAMETERS = 1;
53
54   private static final int TEXT_INDEX = NUMBER_OF_PARAMETERS;
55
56   /** The fields added to the basic format. The text field is added. */
57   private static final String ADDITIONAL_LOGLINE_FORMAT_PATTERN
58         = " {" + TEXT_INDEX + "}";
59
60   private static final String EMPTY_MSG = " ";
61
62   /**
63    * Creates a new instance of this and sets the default line type specifier.
64    */
65   MessageLineFormat ()
66   {
67      this(LogLineFormat.LOG_MESSAGE);
68   }
69
70   /**
71    * Creates a new instance of this and sets the supplied line type specifier.
72    * This might be used by more specialized sub classes of this.
73    *
74    * @param type The line type specifier to set.
75    */
76   protected MessageLineFormat (final LogLineType type)
77   {
78      super(type, ADDITIONAL_LOGLINE_FORMAT_PATTERN,
79            NUMBER_OF_ADDITIONAL_PARAMETERS);
80      setFormats(getFormatList(null, true));
81   }
82
83   /**
84    * Gets the formats as array for formatting a log message line.
85    *
86    * @param options The display options specifying which fields to display.
87    * Will be ignored and ,ight be null if <code>ignoreOptions == true</code>.
88    * @param ignoreOptions flag whether to ignore the supplied options and
89    * return the formats for all fields.
90    *
91    * @return array filled with formats for each selected field. Might be empty
92    * array, never null.
93    */
94   static final Format[] getFormatList (
95         final DisplayOptions options,
96         final boolean ignoreOptions)
97   {
98      final List formatList = getBasicFormatList(options, ignoreOptions);
99      formatList.add(new WhitespaceFormat(new AsItIsFormat("\r\n")));
100      return (Format[]) formatList.toArray(EMPTY_FORMATTERS);
101   }
102
103   /**
104    * Formats a Loggable into the supplied StringBuffer. Appends a line feed
105    * after the data is formatted into the StringBuffer.
106    *
107    * @param sb The StringBuffer where to append the formatted Loggable.
108    * @param record Unused by this, might be null.
109    * @param loggable The Loggable to format.
110    * @param trackingIdSequence The list containing the sequence of tracking
111    * ids contributing to this log message.
112    * @param thrown Unused by this, might be null.
113    * @param parameter Unused by this, might be null.
114    *
115    * @see LogLineFormat#format(StringBuffer, LogRecord, Loggable, List, Throwable, Object)
116    */
117   public void format (
118         final StringBuffer sb,
119         final LogRecord record,
120         final Loggable loggable,
121         final List trackingIdSequence,
122         final Throwable thrown,
123         final Object parameter)
124   {
125      setMessageText(loggable.getMessage());
126      basicFormat(sb, record, loggable, trackingIdSequence);
127   }
128
129   /**
130    * Parses a log line, which must be formatted by this, and sets the
131    * appropriate values of the supplied LogFileEntry.
132    *
133    * @param sb The StringBuffer containing the current log line.
134    * @param entry The LogFileEntry for which to parse the log line.
135    *
136    * @throws ParseException if an error occurs parsing <code>sb</code>.
137    *
138    * @see org.jcoderz.commons.logging.LogLineFormat#parse(java.lang.StringBuffer, org.jcoderz.commons.logging.LogFileEntry)
139    */
140   public void parse (StringBuffer sb, LogFileEntry entry)
141         throws ParseException
142   {
143      try
144      {
145         basicParse(sb, entry);
146         entry.setMessage(getMessageText());
147      }
148      catch (ParseException pex)
149      {
150         // just rethrow
151         throw pex;
152      }
153      catch (Exception ex)
154      {
155         final ParseException pex = new ParseException(
156               "Got an error parsing " + sb, 0);
157         pex.initCause(ex);
158         throw pex;
159      }
160   }
161
162   /**
163    * Sets the message to format. If the supplied string is null or empty, a
164    * space is set.
165    *
166    * @param text The message text.
167    */
168   protected final void setMessageText (final String text)
169   {
170      if (text == null || text.length() <= 0)
171      {
172         setParameter(TEXT_INDEX, EMPTY_MSG);
173      }
174      else
175      {
176         setParameter(TEXT_INDEX, text);
177      }
178   }
179
180   /**
181    * Gets the message text of a parsed log line.
182    *
183    * @return Message text of parsed log line.
184    */
185   protected final String getMessageText ()
186   {
187      return (String) getParameter(TEXT_INDEX);
188   }
189}
Note: See TracBrowser for help on using the browser.