jdk/src/solaris/classes/sun/awt/X11/XAWTFormatter.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.awt.X11;
       
    27 
       
    28 import java.util.logging.*;
       
    29 import java.text.*;
       
    30 import java.util.*;
       
    31 import java.io.*;
       
    32 
       
    33 /**
       
    34  * Formatter class providing ANSI output. Based on java.util.logging.SimpleFormatter sources.
       
    35  */
       
    36 
       
    37 public class XAWTFormatter extends java.util.logging.Formatter {
       
    38     Date dat = new Date();
       
    39     private final static String format = "{0,date} {0,time}";
       
    40     private MessageFormat formatter;
       
    41 
       
    42     private Object args[] = new Object[1];
       
    43 
       
    44     // Line separator string.  This is the value of the line.separator
       
    45     // property at the moment that the SimpleFormatter was created.
       
    46     private String lineSeparator = (String) java.security.AccessController.doPrivileged(
       
    47                new sun.security.action.GetPropertyAction("line.separator"));
       
    48 
       
    49     boolean displayFullRecord = false;
       
    50     boolean useANSI = false;
       
    51     boolean showDate = true;
       
    52     boolean showLevel = true;
       
    53     boolean swapMethodClass = false;
       
    54     public XAWTFormatter() {
       
    55         displayFullRecord = "true".equals(LogManager.getLogManager().getProperty("XAWTFormatter.displayFullRecord"));
       
    56         useANSI = "true".equals(LogManager.getLogManager().getProperty("XAWTFormatter.useANSI"));
       
    57         showDate = !"false".equals(LogManager.getLogManager().getProperty("XAWTFormatter.showDate"));
       
    58         showLevel = !"false".equals(LogManager.getLogManager().getProperty("XAWTFormatter.showLevel"));
       
    59         swapMethodClass = "true".equals(LogManager.getLogManager().getProperty("XAWTFormatter.swapMethodClass"));
       
    60     }
       
    61 
       
    62     /**
       
    63      * Format the given LogRecord.
       
    64      * @param record the log record to be formatted.
       
    65      * @return a formatted log record
       
    66      */
       
    67     public synchronized String format(LogRecord record) {
       
    68         StringBuffer sb = new StringBuffer();
       
    69         if (useANSI) {
       
    70             Level lev = record.getLevel();
       
    71             if (Level.FINEST.equals(lev)) {
       
    72                 sb.append("");
       
    73             } else if (Level.FINER.equals(lev)) {
       
    74                 sb.append("");
       
    75             } else if (Level.FINE.equals(lev)) {
       
    76                 sb.append("");
       
    77             }
       
    78         }
       
    79         if (displayFullRecord) {
       
    80             if (showDate) {
       
    81                 // Minimize memory allocations here.
       
    82                 dat.setTime(record.getMillis());
       
    83                 args[0] = dat;
       
    84                 StringBuffer text = new StringBuffer();
       
    85                 if (formatter == null) {
       
    86                     formatter = new MessageFormat(format);
       
    87                 }
       
    88                 formatter.format(args, text, null);
       
    89                 sb.append(text);
       
    90                 sb.append(" ");
       
    91             } else {
       
    92                 sb.append("    ");
       
    93             }
       
    94             if (swapMethodClass) {
       
    95                 if (record.getSourceMethodName() != null) {
       
    96                     sb.append(" ");
       
    97                     sb.append(record.getSourceMethodName());
       
    98                     sb.append(" ");
       
    99                 }
       
   100                 if (record.getSourceClassName() != null) {
       
   101                     sb.append(record.getSourceClassName());
       
   102                 } else {
       
   103                     sb.append(record.getLoggerName());
       
   104                 }
       
   105             } else {
       
   106                 if (record.getSourceClassName() != null) {
       
   107                     sb.append(record.getSourceClassName());
       
   108                 } else {
       
   109                     sb.append(record.getLoggerName());
       
   110                 }
       
   111                 if (record.getSourceMethodName() != null) {
       
   112                     sb.append(" ");
       
   113                     sb.append(record.getSourceMethodName());
       
   114                     sb.append("");
       
   115                 }
       
   116             }
       
   117             sb.append(lineSeparator);
       
   118         }
       
   119         if (useANSI) {
       
   120             Level lev = record.getLevel();
       
   121             if (Level.FINEST.equals(lev)) {
       
   122                 sb.append("");
       
   123             } else if (Level.FINER.equals(lev)) {
       
   124                 sb.append("");
       
   125             } else if (Level.FINE.equals(lev)) {
       
   126                 sb.append("");
       
   127             }
       
   128         }
       
   129         if (showLevel) {
       
   130             sb.append(record.getLevel().getLocalizedName());
       
   131             sb.append(": ");
       
   132         }
       
   133         String message = formatMessage(record);
       
   134         sb.append(message);
       
   135         sb.append(lineSeparator);
       
   136         if (record.getThrown() != null) {
       
   137             try {
       
   138                 StringWriter sw = new StringWriter();
       
   139                 PrintWriter pw = new PrintWriter(sw);
       
   140                 record.getThrown().printStackTrace(pw);
       
   141                 pw.close();
       
   142                 sb.append(sw.toString());
       
   143             } catch (Exception ex) {
       
   144             }
       
   145         }
       
   146         if (useANSI) {
       
   147             sb.append("");
       
   148         }
       
   149         return sb.toString();
       
   150     }
       
   151 }