Source for gnu.classpath.debug.Simple1LineFormatter

   1: /* Simple1LineFormatter.java -- A simple 1-line logging formatter
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.classpath.debug;
  40: 
  41: import gnu.java.security.action.GetPropertyAction;
  42: 
  43: import java.io.PrintWriter;
  44: import java.io.StringWriter;
  45: import java.security.AccessController;
  46: import java.text.DateFormat;
  47: import java.text.DecimalFormat;
  48: import java.text.NumberFormat;
  49: import java.text.SimpleDateFormat;
  50: import java.util.Date;
  51: import java.util.logging.Formatter;
  52: import java.util.logging.LogRecord;
  53: 
  54: /**
  55:  * A simple 1-line formatter to use instead of the 2-line SimpleFormatter used
  56:  * by default in the JDK logging handlers.
  57:  * <p>
  58:  * The fixed format of this formatter is as follows:
  59:  * <p>
  60:  * <ol>
  61:  *   <li>Date: As a yyyy-MM-dd string.</li>
  62:  *   <li>Time: As a HH:mm:ss.SSSS Z string.</li>
  63:  *   <li>Thread identifier, right-justified, and framed in an 11-digit field.</li>
  64:  *   <li>Class name, without its package name, left-justified, and framed in a
  65:  *   32-character field.</li>
  66:  *   <li>Method name, left-justified, and framed in a 32-character field.</li>
  67:  *   <li>Level name, left-justified, and framed in a 6-character field.</li>
  68:  *   <li>User message and arguments.</li>
  69:  *   <li>Platform-dependent line-separator.</li>
  70:  *   <li>Optionally, if the log-record contains a thrown exception, that
  71:  *   exception's stack trace is appended to the output.</li>
  72:  * </ol>
  73:  * <p>
  74:  * Here is an example of the output generated by this formatter:
  75:  * <p>
  76:  * <pre>
  77:  * 2006-02-27 21:59:12.0881 +1100 -1343151280 EncodedKeyFactory                engineGeneratePublic()           FINER - ENTRY java.security.spec.X509EncodedKeySpec@b00d7fc0
  78:  * 2006-02-27 21:59:12.0887 +1100 -1343151280 EncodedKeyFactory                engineGeneratePublic()           FINE  - Exception in DSSPublicKey.valueOf(). Ignore
  79:  * java.security.InvalidParameterException: Unexpected OID: 1.2.840.113549.1.1.1
  80:  *    at gnu.java.security.key.dss.DSSKeyPairX509Codec.decodePublicKey (DSSKeyPairX509Codec.java:205)
  81:  *    at gnu.java.security.key.dss.DSSPublicKey.valueOf (DSSPublicKey.java:136)
  82:  *    at gnu.java.security.jce.sig.EncodedKeyFactory.engineGeneratePublic (EncodedKeyFactory.java:218)
  83:  *    at java.security.KeyFactory.generatePublic (KeyFactory.java:219)
  84:  *    at gnu.java.security.x509.X509Certificate.parse (X509Certificate.java:657)
  85:  *    at gnu.java.security.x509.X509Certificate.<init> (X509Certificate.java:163)
  86:  *    ...
  87:  * 2006-02-27 21:59:12.0895 +1100 -1343151280 RSAKeyPairX509Codec              decodePublicKey()                FINER - ENTRY [B@b00d7fd0
  88:  * 2006-02-27 21:59:12.0897 +1100 -1343151280 RSAKeyPairX509Codec              decodePublicKey()                FINER - RETURN gnu.java.security.key.rsa.GnuRSAPublicKey@b00fb940
  89:  * </pre>
  90:  */
  91: public class Simple1LineFormatter
  92:     extends Formatter
  93: {
  94:   private static final String DAT_PATTERN = "yyyy-MM-dd HH:mm:ss.SSSS Z ";
  95:   private static final String THREAD_PATTERN = " #########0;-#########0";
  96:   private static final String SPACES_32 = "                                ";
  97:   private static final String SPACES_6 = "      ";
  98:   private static final String LS = (String) AccessController.doPrivileged
  99:     (new GetPropertyAction("line.separator"));
 100:   private DateFormat dateFormat;
 101:   private NumberFormat threadFormat;
 102: 
 103:   // default 0-arguments constructor
 104: 
 105:   public String format(LogRecord record)
 106:   {
 107:     if (dateFormat == null)
 108:       dateFormat = new SimpleDateFormat(DAT_PATTERN);
 109: 
 110:     if (threadFormat == null)
 111:       threadFormat = new DecimalFormat(THREAD_PATTERN);
 112: 
 113:     StringBuilder sb = new StringBuilder(180)
 114:         .append(dateFormat.format(new Date(record.getMillis())))
 115:         .append(threadFormat.format(record.getThreadID()))
 116:         .append(" ");
 117:     String s = record.getSourceClassName();
 118:     if (s == null)
 119:       sb.append(SPACES_32);
 120:     else
 121:       {
 122:         s = s.trim();
 123:         int i = s.lastIndexOf(".");
 124:         if (i != - 1)
 125:           s = s.substring(i + 1);
 126: 
 127:         s = (s + SPACES_32).substring(0, 32);
 128:       }
 129: 
 130:     sb.append(s).append(" ");
 131:     s = record.getSourceMethodName();
 132:     if (s == null)
 133:       sb.append(SPACES_32);
 134:     else
 135:       {
 136:         s = s.trim();
 137:         if (s.endsWith("()"))
 138:           s = (s.trim() + SPACES_32).substring(0, 32);
 139:         else
 140:           s = (s.trim() + "()" + SPACES_32).substring(0, 32);
 141:       }
 142: 
 143:     sb.append(s).append(" ");
 144:     s = String.valueOf(record.getLevel());
 145:     if (s == null)
 146:       sb.append(SPACES_6);
 147:     else
 148:       s = (s.trim() + SPACES_6).substring(0, 6);
 149: 
 150:     sb.append(s).append(" - ").append(formatMessage(record)).append(LS);
 151:     Throwable cause = record.getThrown();
 152:     if (cause != null)
 153:       {
 154:         StringWriter sw = new StringWriter();
 155:         cause.printStackTrace(new PrintWriter(sw, true));
 156:         sb.append(sw.toString());
 157:       }
 158: 
 159:     return sb.toString();
 160:   }
 161: }