Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/logging/LoggingSupport.java
38918 views
/*1* Copyright (c) 2009, 2013, 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*/242526package sun.util.logging;2728import java.lang.reflect.Field;29import java.security.AccessController;30import java.security.PrivilegedAction;31import java.util.Date;3233/**34* Internal API to support JRE implementation to detect if the java.util.logging35* support is available but with no dependency on the java.util.logging36* classes. This LoggingSupport class provides several static methods to37* access the java.util.logging functionality that requires the caller38* to ensure that the logging support is {@linkplain #isAvailable available}39* before invoking it.40*41* @see sun.util.logging.PlatformLogger if you want to log messages even42* if the logging support is not available43*/44public class LoggingSupport {45private LoggingSupport() { }4647private static final LoggingProxy proxy =48AccessController.doPrivileged(new PrivilegedAction<LoggingProxy>() {49public LoggingProxy run() {50try {51// create a LoggingProxyImpl instance when52// java.util.logging classes exist53Class<?> c = Class.forName("java.util.logging.LoggingProxyImpl", true, null);54Field f = c.getDeclaredField("INSTANCE");55f.setAccessible(true);56return (LoggingProxy) f.get(null);57} catch (ClassNotFoundException cnf) {58return null;59} catch (NoSuchFieldException e) {60throw new AssertionError(e);61} catch (IllegalAccessException e) {62throw new AssertionError(e);63}64}});6566/**67* Returns true if java.util.logging support is available.68*/69public static boolean isAvailable() {70return proxy != null;71}7273private static void ensureAvailable() {74if (proxy == null)75throw new AssertionError("Should not here");76}7778public static java.util.List<String> getLoggerNames() {79ensureAvailable();80return proxy.getLoggerNames();81}82public static String getLoggerLevel(String loggerName) {83ensureAvailable();84return proxy.getLoggerLevel(loggerName);85}8687public static void setLoggerLevel(String loggerName, String levelName) {88ensureAvailable();89proxy.setLoggerLevel(loggerName, levelName);90}9192public static String getParentLoggerName(String loggerName) {93ensureAvailable();94return proxy.getParentLoggerName(loggerName);95}9697public static Object getLogger(String name) {98ensureAvailable();99return proxy.getLogger(name);100}101102public static Object getLevel(Object logger) {103ensureAvailable();104return proxy.getLevel(logger);105}106107public static void setLevel(Object logger, Object newLevel) {108ensureAvailable();109proxy.setLevel(logger, newLevel);110}111112public static boolean isLoggable(Object logger, Object level) {113ensureAvailable();114return proxy.isLoggable(logger,level);115}116117public static void log(Object logger, Object level, String msg) {118ensureAvailable();119proxy.log(logger, level, msg);120}121122public static void log(Object logger, Object level, String msg, Throwable t) {123ensureAvailable();124proxy.log(logger, level, msg, t);125}126127public static void log(Object logger, Object level, String msg, Object... params) {128ensureAvailable();129proxy.log(logger, level, msg, params);130}131132public static Object parseLevel(String levelName) {133ensureAvailable();134return proxy.parseLevel(levelName);135}136137public static String getLevelName(Object level) {138ensureAvailable();139return proxy.getLevelName(level);140}141142public static int getLevelValue(Object level) {143ensureAvailable();144return proxy.getLevelValue(level);145}146147private static final String DEFAULT_FORMAT =148"%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n";149150private static final String FORMAT_PROP_KEY = "java.util.logging.SimpleFormatter.format";151public static String getSimpleFormat() {152return getSimpleFormat(true);153}154155// useProxy if true will cause initialization of156// java.util.logging and read its configuration157static String getSimpleFormat(boolean useProxy) {158String format =159AccessController.doPrivileged(160new PrivilegedAction<String>() {161public String run() {162return System.getProperty(FORMAT_PROP_KEY);163}164});165166if (useProxy && proxy != null && format == null) {167format = proxy.getProperty(FORMAT_PROP_KEY);168}169170if (format != null) {171try {172// validate the user-defined format string173String.format(format, new Date(), "", "", "", "", "");174} catch (IllegalArgumentException e) {175// illegal syntax; fall back to the default format176format = DEFAULT_FORMAT;177}178} else {179format = DEFAULT_FORMAT;180}181return format;182}183184}185186187