001: /*
002: * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.util.logging.*;
029: import java.text.*;
030: import java.util.*;
031: import java.io.*;
032:
033: /**
034: * Formatter class providing ANSI output. Based on java.util.logging.SimpleFormatter sources.
035: */
036:
037: public class XAWTFormatter extends java.util.logging.Formatter {
038: Date dat = new Date();
039: private final static String format = "{0,date} {0,time}";
040: private MessageFormat formatter;
041:
042: private Object args[] = new Object[1];
043:
044: // Line separator string. This is the value of the line.separator
045: // property at the moment that the SimpleFormatter was created.
046: private String lineSeparator = (String) java.security.AccessController
047: .doPrivileged(new sun.security.action.GetPropertyAction(
048: "line.separator"));
049:
050: boolean displayFullRecord = false;
051: boolean useANSI = false;
052: boolean showDate = true;
053: boolean showLevel = true;
054: boolean swapMethodClass = false;
055:
056: public XAWTFormatter() {
057: displayFullRecord = "true".equals(LogManager.getLogManager()
058: .getProperty("XAWTFormatter.displayFullRecord"));
059: useANSI = "true".equals(LogManager.getLogManager().getProperty(
060: "XAWTFormatter.useANSI"));
061: showDate = !"false".equals(LogManager.getLogManager()
062: .getProperty("XAWTFormatter.showDate"));
063: showLevel = !"false".equals(LogManager.getLogManager()
064: .getProperty("XAWTFormatter.showLevel"));
065: swapMethodClass = "true".equals(LogManager.getLogManager()
066: .getProperty("XAWTFormatter.swapMethodClass"));
067: }
068:
069: /**
070: * Format the given LogRecord.
071: * @param record the log record to be formatted.
072: * @return a formatted log record
073: */
074: public synchronized String format(LogRecord record) {
075: StringBuffer sb = new StringBuffer();
076: if (useANSI) {
077: Level lev = record.getLevel();
078: if (Level.FINEST.equals(lev)) {
079: sb.append("[36m");
080: } else if (Level.FINER.equals(lev)) {
081: sb.append("[32m");
082: } else if (Level.FINE.equals(lev)) {
083: sb.append("[34m");
084: }
085: }
086: if (displayFullRecord) {
087: if (showDate) {
088: // Minimize memory allocations here.
089: dat.setTime(record.getMillis());
090: args[0] = dat;
091: StringBuffer text = new StringBuffer();
092: if (formatter == null) {
093: formatter = new MessageFormat(format);
094: }
095: formatter.format(args, text, null);
096: sb.append(text);
097: sb.append(" ");
098: } else {
099: sb.append(" ");
100: }
101: if (swapMethodClass) {
102: if (record.getSourceMethodName() != null) {
103: sb.append(" [35m");
104: sb.append(record.getSourceMethodName());
105: sb.append("[30m ");
106: }
107: if (record.getSourceClassName() != null) {
108: sb.append(record.getSourceClassName());
109: } else {
110: sb.append(record.getLoggerName());
111: }
112: } else {
113: if (record.getSourceClassName() != null) {
114: sb.append(record.getSourceClassName());
115: } else {
116: sb.append(record.getLoggerName());
117: }
118: if (record.getSourceMethodName() != null) {
119: sb.append(" [35m");
120: sb.append(record.getSourceMethodName());
121: sb.append("[30m");
122: }
123: }
124: sb.append(lineSeparator);
125: }
126: if (useANSI) {
127: Level lev = record.getLevel();
128: if (Level.FINEST.equals(lev)) {
129: sb.append("[36m");
130: } else if (Level.FINER.equals(lev)) {
131: sb.append("[32m");
132: } else if (Level.FINE.equals(lev)) {
133: sb.append("[34m");
134: }
135: }
136: if (showLevel) {
137: sb.append(record.getLevel().getLocalizedName());
138: sb.append(": ");
139: }
140: String message = formatMessage(record);
141: sb.append(message);
142: sb.append(lineSeparator);
143: if (record.getThrown() != null) {
144: try {
145: StringWriter sw = new StringWriter();
146: PrintWriter pw = new PrintWriter(sw);
147: record.getThrown().printStackTrace(pw);
148: pw.close();
149: sb.append(sw.toString());
150: } catch (Exception ex) {
151: }
152: }
153: if (useANSI) {
154: sb.append("[30m");
155: }
156: return sb.toString();
157: }
158: }
|