0N/A/*
2216N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
1879N/A
1879N/A
1879N/Apackage sun.util.logging;
1879N/A
1879N/Aimport java.lang.reflect.Field;
1879N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
453N/Aimport java.util.Date;
0N/A
0N/A/**
0N/A * Internal API to support JRE implementation to detect if the java.util.logging
0N/A * support is available but with no dependency on the java.util.logging
0N/A * classes. This LoggingSupport class provides several static methods to
0N/A * access the java.util.logging functionality that requires the caller
0N/A * to ensure that the logging support is {@linkplain #isAvailable available}
0N/A * before invoking it.
0N/A *
0N/A * @see sun.util.logging.PlatformLogger if you want to log messages even
0N/A * if the logging support is not available
0N/A */
0N/Apublic class LoggingSupport {
0N/A private LoggingSupport() { }
0N/A
0N/A private static final LoggingProxy proxy =
0N/A AccessController.doPrivileged(new PrivilegedAction<LoggingProxy>() {
0N/A public LoggingProxy run() {
0N/A try {
0N/A // create a LoggingProxyImpl instance when
0N/A // java.util.logging classes exist
0N/A Class<?> c = Class.forName("java.util.logging.LoggingProxyImpl", true, null);
2751N/A Field f = c.getDeclaredField("INSTANCE");
2751N/A f.setAccessible(true);
0N/A return (LoggingProxy) f.get(null);
0N/A } catch (ClassNotFoundException cnf) {
0N/A return null;
0N/A } catch (NoSuchFieldException e) {
0N/A throw new AssertionError(e);
0N/A } catch (IllegalAccessException e) {
342N/A throw new AssertionError(e);
342N/A }
342N/A }});
342N/A
342N/A /**
342N/A * Returns true if java.util.logging support is available.
342N/A */
342N/A public static boolean isAvailable() {
0N/A return proxy != null;
0N/A }
0N/A
2216N/A private static void ensureAvailable() {
0N/A if (proxy == null)
0N/A throw new AssertionError("Should not here");
0N/A }
0N/A
0N/A public static java.util.List<String> getLoggerNames() {
0N/A ensureAvailable();
0N/A return proxy.getLoggerNames();
0N/A }
0N/A public static String getLoggerLevel(String loggerName) {
453N/A ensureAvailable();
453N/A return proxy.getLoggerLevel(loggerName);
453N/A }
453N/A
453N/A public static void setLoggerLevel(String loggerName, String levelName) {
453N/A ensureAvailable();
453N/A proxy.setLoggerLevel(loggerName, levelName);
453N/A }
0N/A
113N/A public static String getParentLoggerName(String loggerName) {
1753N/A ensureAvailable();
113N/A return proxy.getParentLoggerName(loggerName);
1753N/A }
1753N/A
113N/A public static Object getLogger(String name) {
113N/A ensureAvailable();
0N/A return proxy.getLogger(name);
0N/A }
0N/A
0N/A public static Object getLevel(Object logger) {
0N/A ensureAvailable();
113N/A return proxy.getLevel(logger);
2216N/A }
2216N/A
0N/A public static void setLevel(Object logger, Object newLevel) {
457N/A ensureAvailable();
453N/A proxy.setLevel(logger, newLevel);
453N/A }
457N/A
453N/A public static boolean isLoggable(Object logger, Object level) {
453N/A ensureAvailable();
0N/A return proxy.isLoggable(logger,level);
0N/A }
0N/A
0N/A public static void log(Object logger, Object level, String msg) {
0N/A ensureAvailable();
0N/A proxy.log(logger, level, msg);
0N/A }
0N/A
0N/A public static void log(Object logger, Object level, String msg, Throwable t) {
0N/A ensureAvailable();
0N/A proxy.log(logger, level, msg, t);
0N/A }
0N/A
0N/A public static void log(Object logger, Object level, String msg, Object... params) {
0N/A ensureAvailable();
0N/A proxy.log(logger, level, msg, params);
0N/A }
113N/A
0N/A public static Object parseLevel(String levelName) {
0N/A ensureAvailable();
0N/A return proxy.parseLevel(levelName);
0N/A }
0N/A
0N/A public static String getLevelName(Object level) {
113N/A ensureAvailable();
0N/A return proxy.getLevelName(level);
0N/A }
0N/A
0N/A public static int getLevelValue(Object level) {
0N/A ensureAvailable();
113N/A return proxy.getLevelValue(level);
0N/A }
0N/A
113N/A private static final String DEFAULT_FORMAT =
0N/A "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n";
0N/A
0N/A private static final String FORMAT_PROP_KEY = "java.util.logging.SimpleFormatter.format";
0N/A public static String getSimpleFormat() {
113N/A return getSimpleFormat(true);
0N/A }
0N/A
0N/A // useProxy if true will cause initialization of
113N/A // java.util.logging and read its configuration
0N/A static String getSimpleFormat(boolean useProxy) {
0N/A String format =
0N/A AccessController.doPrivileged(
0N/A new PrivilegedAction<String>() {
0N/A public String run() {
113N/A return System.getProperty(FORMAT_PROP_KEY);
0N/A }
0N/A });
0N/A
0N/A if (useProxy && proxy != null && format == null) {
0N/A format = proxy.getProperty(FORMAT_PROP_KEY);
0N/A }
113N/A
0N/A if (format != null) {
0N/A try {
0N/A // validate the user-defined format string
0N/A String.format(format, new Date(), "", "", "", "", "");
0N/A } catch (IllegalArgumentException e) {
0N/A // illegal syntax; fall back to the default format
0N/A format = DEFAULT_FORMAT;
0N/A }
0N/A } else {
1190N/A format = DEFAULT_FORMAT;
1190N/A }
0N/A return format;
0N/A }
0N/A
1190N/A}
1190N/A