1 /*
   2  * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.util.logging;
  28 
  29 import java.lang.reflect.Field;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.time.ZonedDateTime;
  33 
  34 /**
  35  * Internal API to support JRE implementation to detect if the java.util.logging
  36  * support is available but with no dependency on the java.util.logging
  37  * classes.  This LoggingSupport class provides several static methods to
  38  * access the java.util.logging functionality that requires the caller
  39  * to ensure that the logging support is {@linkplain #isAvailable available}
  40  * before invoking it.
  41  *
  42  * @see sun.util.logging.PlatformLogger if you want to log messages even
  43  * if the logging support is not available
  44  */
  45 public class LoggingSupport {
  46     private LoggingSupport() { }
  47 
  48     private static final LoggingProxy proxy =
  49         AccessController.doPrivileged(new PrivilegedAction<LoggingProxy>() {
  50             public LoggingProxy run() {
  51                 try {
  52                     // create a LoggingProxyImpl instance when
  53                     // java.util.logging classes exist
  54                     Class<?> c = Class.forName("java.util.logging.LoggingProxyImpl", true, null);
  55                     Field f = c.getDeclaredField("INSTANCE");
  56                     f.setAccessible(true);
  57                     return (LoggingProxy) f.get(null);
  58                 } catch (ClassNotFoundException cnf) {
  59                     return null;
  60                 } catch (NoSuchFieldException e) {
  61                     throw new AssertionError(e);
  62                 } catch (IllegalAccessException e) {
  63                     throw new AssertionError(e);
  64                 }
  65             }});
  66 
  67     /**
  68      * Returns true if java.util.logging support is available.
  69      */
  70     public static boolean isAvailable() {
  71         return proxy != null;
  72     }
  73 
  74     private static void ensureAvailable() {
  75         if (proxy == null)
  76             throw new AssertionError("Should not here");
  77     }
  78 
  79     public static java.util.List<String> getLoggerNames() {
  80         ensureAvailable();
  81         return proxy.getLoggerNames();
  82     }
  83     public static String getLoggerLevel(String loggerName) {
  84         ensureAvailable();
  85         return proxy.getLoggerLevel(loggerName);
  86     }
  87 
  88     public static void setLoggerLevel(String loggerName, String levelName) {
  89         ensureAvailable();
  90         proxy.setLoggerLevel(loggerName, levelName);
  91     }
  92 
  93     public static String getParentLoggerName(String loggerName) {
  94         ensureAvailable();
  95         return proxy.getParentLoggerName(loggerName);
  96     }
  97 
  98     public static Object getLogger(String name) {
  99         ensureAvailable();
 100         return proxy.getLogger(name);
 101     }
 102 
 103     public static Object getLevel(Object logger) {
 104         ensureAvailable();
 105         return proxy.getLevel(logger);
 106     }
 107 
 108     public static void setLevel(Object logger, Object newLevel) {
 109         ensureAvailable();
 110         proxy.setLevel(logger, newLevel);
 111     }
 112 
 113     public static boolean isLoggable(Object logger, Object level) {
 114         ensureAvailable();
 115         return proxy.isLoggable(logger,level);
 116     }
 117 
 118     public static void log(Object logger, Object level, String msg) {
 119         ensureAvailable();
 120         proxy.log(logger, level, msg);
 121     }
 122 
 123     public static void log(Object logger, Object level, String msg, Throwable t) {
 124         ensureAvailable();
 125         proxy.log(logger, level, msg, t);
 126     }
 127 
 128     public static void log(Object logger, Object level, String msg, Object... params) {
 129         ensureAvailable();
 130         proxy.log(logger, level, msg, params);
 131     }
 132 
 133     public static Object parseLevel(String levelName) {
 134         ensureAvailable();
 135         return proxy.parseLevel(levelName);
 136     }
 137 
 138     public static String getLevelName(Object level) {
 139         ensureAvailable();
 140         return proxy.getLevelName(level);
 141     }
 142 
 143     public static int getLevelValue(Object level) {
 144         ensureAvailable();
 145         return proxy.getLevelValue(level);
 146     }
 147 
 148     // Since JDK 9, logging uses java.time to get more precise time stamps.
 149     // It is possible to configure the simple format to print nano seconds (.%1$tN)
 150     // by specifying:
 151     // java.util.logging.SimpleFormatter.format=%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS.%1$tN %1$Tp %2$s%n%4$s: %5$s%6$s%n
 152     // in the logging configuration
 153     private static final String DEFAULT_FORMAT =
 154         "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n";
 155 
 156     private static final String FORMAT_PROP_KEY = "java.util.logging.SimpleFormatter.format";
 157     public static String getSimpleFormat() {
 158         return getSimpleFormat(true);
 159     }
 160 
 161     // useProxy if true will cause initialization of
 162     // java.util.logging and read its configuration
 163     static String getSimpleFormat(boolean useProxy) {
 164         String format =
 165             AccessController.doPrivileged(
 166                 new PrivilegedAction<String>() {
 167                     public String run() {
 168                         return System.getProperty(FORMAT_PROP_KEY);
 169                     }
 170                 });
 171 
 172         if (useProxy && proxy != null && format == null) {
 173             format = proxy.getProperty(FORMAT_PROP_KEY);
 174         }
 175 
 176         if (format != null) {
 177             try {
 178                 // validate the user-defined format string
 179                 String.format(format, ZonedDateTime.now(), "", "", "", "", "");
 180             } catch (IllegalArgumentException e) {
 181                 // illegal syntax; fall back to the default format
 182                 format = DEFAULT_FORMAT;
 183             }
 184         } else {
 185             format = DEFAULT_FORMAT;
 186         }
 187         return format;
 188     }
 189 
 190 }