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