Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jmxremote/bootstrap/TestLogger.java
38867 views
/*1* Copyright (c) 2003, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package util;2324import java.util.logging.Logger;25import java.util.logging.Level;2627public class TestLogger {2829final Logger logger;30final String className;3132static String getClassName(Class clazz) {33if (clazz == null) return null;34if (clazz.isArray())35return getClassName(clazz.getComponentType()) + "[]";36final String fullname = clazz.getName();37final int lastpoint = fullname.lastIndexOf('.');38final int len = fullname.length();39if ((lastpoint < 0) || (lastpoint >= len))40return fullname;41else return fullname.substring(lastpoint+1,len);42}4344static String getLoggerName(Class clazz) {45if (clazz == null) return "sun.management.test";46Package p = clazz.getPackage();47if (p == null) return "sun.management.test";48final String pname = p.getName();49if (pname == null) return "sun.management.test";50else return pname;51}5253public TestLogger(Class clazz) {54this(getLoggerName(clazz),getClassName(clazz));55}5657public TestLogger(Class clazz, String postfix) {58this(getLoggerName(clazz)+((postfix==null)?"":"."+postfix),59getClassName(clazz));60}6162public TestLogger(String className) {63this("sun.management.test",className);64}6566public TestLogger(String loggerName, String className) {67Logger l = null;68try {69l = Logger.getLogger(loggerName);70} catch (Exception x) {71// OK. Should not happen72}73logger = l;74this.className=className;75}7677protected Logger getLogger() {78return logger;79}8081public boolean isTraceOn() {82final Logger l = getLogger();83if (l==null) return false;84return l.isLoggable(Level.FINE);85}8687public boolean isDebugOn() {88final Logger l = getLogger();89if (l==null) return false;90return l.isLoggable(Level.FINEST);91}9293public void error(String func, String msg) {94final Logger l = getLogger();95if (l!=null) l.logp(Level.SEVERE,className,96func,msg);97}9899public void trace(String func, String msg) {100final Logger l = getLogger();101if (l!=null) l.logp(Level.FINE,className,102func,msg);103}104105public void trace(String func, Throwable t) {106final Logger l = getLogger();107if (l!=null) l.logp(Level.FINE,className,108func,t.toString(),t);109}110111public void trace(String func, String msg, Throwable t) {112final Logger l = getLogger();113if (l!=null) l.logp(Level.FINE,className,114func,msg,t);115}116117public void debug(String func, String msg) {118final Logger l = getLogger();119if (l!=null) l.logp(Level.FINEST,className,120func,msg);121}122123public void debug(String func, Throwable t) {124final Logger l = getLogger();125if (l!=null) l.logp(Level.FINEST,className,126func,t.toString(),t);127}128129public void debug(String func, String msg, Throwable t) {130final Logger l = getLogger();131if (l!=null) l.logp(Level.FINEST,className,132func,msg,t);133}134}135136137