Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: de/hunsicker/jalopy/plugin/SwingLayout.java


1   /*
2    * Copyright (c) 2001-2002, Marco Hunsicker. All rights reserved.
3    *
4    * This software is distributable under the BSD license. See the terms of the
5    * BSD license in the documentation provided with this software.
6    */
7   package de.hunsicker.jalopy.plugin;
8   
9   import de.hunsicker.jalopy.storage.Convention;
10  import de.hunsicker.jalopy.storage.ConventionDefaults;
11  import de.hunsicker.jalopy.storage.ConventionKeys;
12  import de.hunsicker.util.StringHelper;
13  
14  import org.apache.log4j.Layout;
15  import org.apache.log4j.spi.LoggingEvent;
16  
17  
18  /**
19   * A custom Log4J layout which reformats muliple line messages and takes care of
20   * throwable information.
21   *
22   * @author <a href="http://jalopy.sf.net/contact.html">Marco Hunsicker</a>
23   * @version $Revision: 1.1 $
24   */
25  final class SwingLayout
26      extends Layout
27  {
28      //~ Static variables/initializers ----------------------------------------------------
29  
30      private static final String[] EMPTY_STRING_ARRAY = new String[0];
31      private static final int MAX_LINE_LENGTH = 100;
32  
33      //~ Instance variables ---------------------------------------------------------------
34  
35      private Convention _settings = Convention.getInstance();
36  
37      //~ Constructors ---------------------------------------------------------------------
38  
39      /**
40       * Creates a new SwingLayout object.
41       */
42      public SwingLayout()
43      {
44      }
45  
46      //~ Methods --------------------------------------------------------------------------
47  
48      /**
49       * Activate the options that were previously set with calls to option setters.
50       * Actaally does nothing as the option setters are deprecated and no longer used.
51       */
52      public void activateOptions()
53      {
54      }
55  
56  
57      /**
58       * Returns the log statement. Adds throwable information if available and enabled in
59       * the code convention. Multiple line messages will be reformatted to fit into the
60       * given maximal line length.
61       *
62       * @param event the logging event.
63       *
64       * @return the formatted message.
65       */
66      public String format(LoggingEvent event)
67      {
68          if (ignoresThrowable())
69          {
70              String message = event.getRenderedMessage();
71  
72              // max. line length exeeded, perform reformatting
73              if (message.length() > MAX_LINE_LENGTH)
74              {
75                  return StringHelper.wrapString(message, MAX_LINE_LENGTH, true);
76              }
77              else
78              {
79                  return message;
80              }
81          }
82          else
83          {
84              StringBuffer buf = new StringBuffer(100);
85  
86              if (event.getThrowableStrRep() != null)
87              {
88                  String[] lines = event.getThrowableStrRep();
89                  String message = event.getRenderedMessage();
90  
91                  // first the message
92                  buf.append(message);
93                  buf.append('\n');
94  
95                  // append the stacktrace (if available)
96                  for (int i = 0; i < lines.length; i++)
97                  {
98                      buf.append(lines[i]);
99                      buf.append('\n');
100                 }
101 
102                 // remove the last separator
103                 buf.setLength(buf.length() - 1);
104             }
105             else
106             {
107                 buf.append(event.getRenderedMessage());
108             }
109 
110             return buf.toString();
111         }
112     }
113 
114 
115     /**
116      * Indicates whether throwable information should be included in the output.
117      *
118      * @return <code>true</code> if throwable information should be included in the
119      *         output.
120      */
121     public boolean ignoresThrowable()
122     {
123         return !_settings.getBoolean(
124             ConventionKeys.MSG_SHOW_ERROR_STACKTRACE,
125             ConventionDefaults.MSG_SHOW_ERROR_STACKTRACE);
126     }
127 }