Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11/XAWTFormatter.java
32288 views
/*1* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.X11;2627import java.util.logging.*;28import java.text.*;29import java.util.*;30import java.io.*;3132/**33* Formatter class providing ANSI output. Based on java.util.logging.SimpleFormatter sources.34*/3536public class XAWTFormatter extends java.util.logging.Formatter {37Date dat = new Date();38private final static String format = "{0,date} {0,time}";39private MessageFormat formatter;4041private Object args[] = new Object[1];4243// Line separator string. This is the value of the line.separator44// property at the moment that the SimpleFormatter was created.45private String lineSeparator = (String) java.security.AccessController.doPrivileged(46new sun.security.action.GetPropertyAction("line.separator"));4748boolean displayFullRecord = false;49boolean useANSI = false;50boolean showDate = true;51boolean showLevel = true;52boolean swapMethodClass = false;53public XAWTFormatter() {54displayFullRecord = "true".equals(LogManager.getLogManager().getProperty("XAWTFormatter.displayFullRecord"));55useANSI = "true".equals(LogManager.getLogManager().getProperty("XAWTFormatter.useANSI"));56showDate = !"false".equals(LogManager.getLogManager().getProperty("XAWTFormatter.showDate"));57showLevel = !"false".equals(LogManager.getLogManager().getProperty("XAWTFormatter.showLevel"));58swapMethodClass = "true".equals(LogManager.getLogManager().getProperty("XAWTFormatter.swapMethodClass"));59}6061/**62* Format the given LogRecord.63* @param record the log record to be formatted.64* @return a formatted log record65*/66public synchronized String format(LogRecord record) {67StringBuffer sb = new StringBuffer();68if (useANSI) {69Level lev = record.getLevel();70if (Level.FINEST.equals(lev)) {71sb.append("[36m");72} else if (Level.FINER.equals(lev)) {73sb.append("[32m");74} else if (Level.FINE.equals(lev)) {75sb.append("[34m");76}77}78if (displayFullRecord) {79if (showDate) {80// Minimize memory allocations here.81dat.setTime(record.getMillis());82args[0] = dat;83StringBuffer text = new StringBuffer();84if (formatter == null) {85formatter = new MessageFormat(format);86}87formatter.format(args, text, null);88sb.append(text);89sb.append(" ");90} else {91sb.append(" ");92}93if (swapMethodClass) {94if (record.getSourceMethodName() != null) {95sb.append(" [35m");96sb.append(record.getSourceMethodName());97sb.append("[30m ");98}99if (record.getSourceClassName() != null) {100sb.append(record.getSourceClassName());101} else {102sb.append(record.getLoggerName());103}104} else {105if (record.getSourceClassName() != null) {106sb.append(record.getSourceClassName());107} else {108sb.append(record.getLoggerName());109}110if (record.getSourceMethodName() != null) {111sb.append(" [35m");112sb.append(record.getSourceMethodName());113sb.append("[30m");114}115}116sb.append(lineSeparator);117}118if (useANSI) {119Level lev = record.getLevel();120if (Level.FINEST.equals(lev)) {121sb.append("[36m");122} else if (Level.FINER.equals(lev)) {123sb.append("[32m");124} else if (Level.FINE.equals(lev)) {125sb.append("[34m");126}127}128if (showLevel) {129sb.append(record.getLevel().getLocalizedName());130sb.append(": ");131}132String message = formatMessage(record);133sb.append(message);134sb.append(lineSeparator);135if (record.getThrown() != null) {136try {137StringWriter sw = new StringWriter();138PrintWriter pw = new PrintWriter(sw);139record.getThrown().printStackTrace(pw);140pw.close();141sb.append(sw.toString());142} catch (Exception ex) {143}144}145if (useANSI) {146sb.append("[30m");147}148return sb.toString();149}150}151152153