Path: blob/master/test/jdk/sun/management/jmxremote/bootstrap/TestLogger.java
41153 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*/2223import java.util.logging.Logger;24import java.util.logging.Level;2526public class TestLogger {2728final Logger logger;29final String className;3031static String getClassName(Class clazz) {32if (clazz == null) return null;33if (clazz.isArray())34return getClassName(clazz.getComponentType()) + "[]";35final String fullname = clazz.getName();36final int lastpoint = fullname.lastIndexOf('.');37final int len = fullname.length();38if ((lastpoint < 0) || (lastpoint >= len))39return fullname;40else return fullname.substring(lastpoint+1,len);41}4243static String getLoggerName(Class clazz) {44if (clazz == null) return "sun.management.test";45Package p = clazz.getPackage();46if (p == null) return "sun.management.test";47final String pname = p.getName();48if (pname == null) return "sun.management.test";49else return pname;50}5152public TestLogger(Class clazz) {53this(getLoggerName(clazz),getClassName(clazz));54}5556public TestLogger(Class clazz, String postfix) {57this(getLoggerName(clazz)+((postfix==null)?"":"."+postfix),58getClassName(clazz));59}6061public TestLogger(String className) {62this("sun.management.test",className);63}6465public TestLogger(String loggerName, String className) {66Logger l = null;67try {68l = Logger.getLogger(loggerName);69} catch (Exception x) {70// OK. Should not happen71}72logger = l;73this.className=className;74}7576protected Logger getLogger() {77return logger;78}7980public boolean isTraceOn() {81final Logger l = getLogger();82if (l==null) return false;83return l.isLoggable(Level.FINE);84}8586public boolean isDebugOn() {87final Logger l = getLogger();88if (l==null) return false;89return l.isLoggable(Level.FINEST);90}9192public void error(String func, String msg) {93final Logger l = getLogger();94if (l!=null) l.logp(Level.SEVERE,className,95func,msg);96}9798public void trace(String func, String msg) {99final Logger l = getLogger();100if (l!=null) l.logp(Level.FINE,className,101func,msg);102}103104public void trace(String func, Throwable t) {105final Logger l = getLogger();106if (l!=null) l.logp(Level.FINE,className,107func,t.toString(),t);108}109110public void trace(String func, String msg, Throwable t) {111final Logger l = getLogger();112if (l!=null) l.logp(Level.FINE,className,113func,msg,t);114}115116public void debug(String func, String msg) {117final Logger l = getLogger();118if (l!=null) l.logp(Level.FINEST,className,119func,msg);120}121122public void debug(String func, Throwable t) {123final Logger l = getLogger();124if (l!=null) l.logp(Level.FINEST,className,125func,t.toString(),t);126}127128public void debug(String func, String msg, Throwable t) {129final Logger l = getLogger();130if (l!=null) l.logp(Level.FINEST,className,131func,msg,t);132}133}134135136