提交 294fb235 编写于 作者: M mchung

8011380: FX dependency on PlatformLogger broken by 8010309

Reviewed-by: alanb
上级 1cdda8ec
...@@ -105,6 +105,11 @@ class LoggingProxyImpl implements LoggingProxy { ...@@ -105,6 +105,11 @@ class LoggingProxyImpl implements LoggingProxy {
return ((Level) level).getLevelName(); return ((Level) level).getLevelName();
} }
@Override
public int getLevelValue(Object level) {
return ((Level) level).intValue();
}
@Override @Override
public String getProperty(String key) { public String getProperty(String key) {
return LogManager.getLogManager().getProperty(key); return LogManager.getLogManager().getProperty(key);
......
...@@ -61,6 +61,8 @@ public interface LoggingProxy { ...@@ -61,6 +61,8 @@ public interface LoggingProxy {
public String getLevelName(Object level); public String getLevelName(Object level);
public int getLevelValue(Object level);
// return the logging property // return the logging property
public String getProperty(String key); public String getProperty(String key);
} }
...@@ -140,6 +140,11 @@ public class LoggingSupport { ...@@ -140,6 +140,11 @@ public class LoggingSupport {
return proxy.getLevelName(level); return proxy.getLevelName(level);
} }
public static int getLevelValue(Object level) {
ensureAvailable();
return proxy.getLevelValue(level);
}
private static final String DEFAULT_FORMAT = private static final String DEFAULT_FORMAT =
"%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n"; "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n";
......
...@@ -32,6 +32,7 @@ import java.io.PrintWriter; ...@@ -32,6 +32,7 @@ import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -84,31 +85,38 @@ import sun.misc.SharedSecrets; ...@@ -84,31 +85,38 @@ import sun.misc.SharedSecrets;
* @since 1.7 * @since 1.7
*/ */
public class PlatformLogger { public class PlatformLogger {
// shortcut to PlatformLogger.Level enums /*
public static final Level OFF = Level.OFF; * These constants should be shortcuts to Level enum constants that
public static final Level SEVERE = Level.SEVERE; * the clients of sun.util.logging.PlatformLogger require no source
public static final Level WARNING = Level.WARNING; * modification and avoid the conversion from int to Level enum.
public static final Level INFO = Level.INFO; *
public static final Level CONFIG = Level.CONFIG; * This can be done when JavaFX is converted to use the new PlatformLogger.Level API.
public static final Level FINE = Level.FINE; */
public static final Level FINER = Level.FINER; public static final int OFF = Integer.MAX_VALUE;
public static final Level FINEST = Level.FINEST; public static final int SEVERE = 1000;
public static final Level ALL = Level.ALL; public static final int WARNING = 900;
public static final int INFO = 800;
public static final int CONFIG = 700;
public static final int FINE = 500;
public static final int FINER = 400;
public static final int FINEST = 300;
public static final int ALL = Integer.MIN_VALUE;
/** /**
* PlatformLogger logging levels. * PlatformLogger logging levels.
*/ */
public static enum Level { public static enum Level {
// The name and value must match that of {@code java.util.logging.Level} objects. // The name and value must match that of {@code java.util.logging.Level}s.
ALL(Integer.MIN_VALUE), // Declare in ascending order of the given value for binary search.
FINEST(300), ALL,
FINER(400), FINEST,
FINE(500), FINER,
CONFIG(700), FINE,
INFO(800), CONFIG,
WARNING(900), INFO,
SEVERE(1000), WARNING,
OFF(Integer.MAX_VALUE); SEVERE,
OFF;
/** /**
* Associated java.util.logging.Level lazily initialized in * Associated java.util.logging.Level lazily initialized in
...@@ -118,17 +126,39 @@ public class PlatformLogger { ...@@ -118,17 +126,39 @@ public class PlatformLogger {
*/ */
/* java.util.logging.Level */ Object javaLevel; /* java.util.logging.Level */ Object javaLevel;
private final int value; // ascending order for binary search matching the list of enum constants
private static final int[] levelValues = new int[] {
PlatformLogger.ALL, PlatformLogger.FINEST, PlatformLogger.FINER,
PlatformLogger.FINE, PlatformLogger.CONFIG, PlatformLogger.INFO,
PlatformLogger.WARNING, PlatformLogger.SEVERE, PlatformLogger.OFF
};
public int intValue() { public int intValue() {
return value; return levelValues[this.ordinal()];
} }
Level(int value) { static Level valueOf(int level) {
this.value = value; switch (level) {
// ordering per the highest occurences in the jdk source
// finest, fine, finer, info first
case PlatformLogger.FINEST : return Level.FINEST;
case PlatformLogger.FINE : return Level.FINE;
case PlatformLogger.FINER : return Level.FINER;
case PlatformLogger.INFO : return Level.INFO;
case PlatformLogger.WARNING : return Level.WARNING;
case PlatformLogger.CONFIG : return Level.CONFIG;
case PlatformLogger.SEVERE : return Level.SEVERE;
case PlatformLogger.OFF : return Level.OFF;
case PlatformLogger.ALL : return Level.ALL;
}
// return the nearest Level value >= the given level,
// for level > SEVERE, return SEVERE and exclude OFF
int i = Arrays.binarySearch(levelValues, 0, levelValues.length-2, level);
return values()[i >= 0 ? i : (-i-1)];
} }
} }
private static final Level DEFAULT_LEVEL = INFO; private static final Level DEFAULT_LEVEL = Level.INFO;
private static boolean loggingEnabled; private static boolean loggingEnabled;
static { static {
loggingEnabled = AccessController.doPrivileged( loggingEnabled = AccessController.doPrivileged(
...@@ -231,11 +261,47 @@ public class PlatformLogger { ...@@ -231,11 +261,47 @@ public class PlatformLogger {
return loggerProxy.name; return loggerProxy.name;
} }
/**
* Returns true if a message of the given level would actually
* be logged by this logger.
*
* @deprecated Use isLoggable(Level) instead.
*/
@Deprecated
public boolean isLoggable(int levelValue) {
return isLoggable(Level.valueOf(levelValue));
}
/**
* Gets the current log level. Returns 0 if the current effective level is
* not set (equivalent to Logger.getLevel() returns null).
*
* @deprecated Use level() instead
*/
@Deprecated
public int getLevel() {
Level level = loggerProxy.getLevel();
return level != null ? level.intValue() : 0;
}
/**
* Sets the log level.
*
* @deprecated Use setLevel(Level) instead
*/
@Deprecated
public void setLevel(int newLevel) {
loggerProxy.setLevel(newLevel == 0 ? null : Level.valueOf(newLevel));
}
/** /**
* Returns true if a message of the given level would actually * Returns true if a message of the given level would actually
* be logged by this logger. * be logged by this logger.
*/ */
public boolean isLoggable(Level level) { public boolean isLoggable(Level level) {
if (level == null) {
throw new NullPointerException();
}
// performance-sensitive method: use two monomorphic call-sites // performance-sensitive method: use two monomorphic call-sites
JavaLoggerProxy jlp = javaLoggerProxy; JavaLoggerProxy jlp = javaLoggerProxy;
return jlp != null ? jlp.isLoggable(level) : loggerProxy.isLoggable(level); return jlp != null ? jlp.isLoggable(level) : loggerProxy.isLoggable(level);
...@@ -246,15 +312,9 @@ public class PlatformLogger { ...@@ -246,15 +312,9 @@ public class PlatformLogger {
* The result may be null, which means that this logger's * The result may be null, which means that this logger's
* effective level will be inherited from its parent. * effective level will be inherited from its parent.
* *
* This method is primarily for testing purpose and not recommended
* to be used at runtime since it does not support custom j.u.l.Level.
*
* @return this PlatformLogger's level * @return this PlatformLogger's level
*
* @throw IllegalArgumentException if j.u.l.Logger is set to
* a custom j.u.l.Level when java.util.logging facility is enabled
*/ */
public Level getLevel() { public Level level() {
return loggerProxy.getLevel(); return loggerProxy.getLevel();
} }
...@@ -278,105 +338,105 @@ public class PlatformLogger { ...@@ -278,105 +338,105 @@ public class PlatformLogger {
* Logs a SEVERE message. * Logs a SEVERE message.
*/ */
public void severe(String msg) { public void severe(String msg) {
loggerProxy.doLog(SEVERE, msg); loggerProxy.doLog(Level.SEVERE, msg);
} }
public void severe(String msg, Throwable t) { public void severe(String msg, Throwable t) {
loggerProxy.doLog(SEVERE, msg, t); loggerProxy.doLog(Level.SEVERE, msg, t);
} }
public void severe(String msg, Object... params) { public void severe(String msg, Object... params) {
loggerProxy.doLog(SEVERE, msg, params); loggerProxy.doLog(Level.SEVERE, msg, params);
} }
/** /**
* Logs a WARNING message. * Logs a WARNING message.
*/ */
public void warning(String msg) { public void warning(String msg) {
loggerProxy.doLog(WARNING, msg); loggerProxy.doLog(Level.WARNING, msg);
} }
public void warning(String msg, Throwable t) { public void warning(String msg, Throwable t) {
loggerProxy.doLog(WARNING, msg, t); loggerProxy.doLog(Level.WARNING, msg, t);
} }
public void warning(String msg, Object... params) { public void warning(String msg, Object... params) {
loggerProxy.doLog(WARNING, msg, params); loggerProxy.doLog(Level.WARNING, msg, params);
} }
/** /**
* Logs an INFO message. * Logs an INFO message.
*/ */
public void info(String msg) { public void info(String msg) {
loggerProxy.doLog(INFO, msg); loggerProxy.doLog(Level.INFO, msg);
} }
public void info(String msg, Throwable t) { public void info(String msg, Throwable t) {
loggerProxy.doLog(INFO, msg, t); loggerProxy.doLog(Level.INFO, msg, t);
} }
public void info(String msg, Object... params) { public void info(String msg, Object... params) {
loggerProxy.doLog(INFO, msg, params); loggerProxy.doLog(Level.INFO, msg, params);
} }
/** /**
* Logs a CONFIG message. * Logs a CONFIG message.
*/ */
public void config(String msg) { public void config(String msg) {
loggerProxy.doLog(CONFIG, msg); loggerProxy.doLog(Level.CONFIG, msg);
} }
public void config(String msg, Throwable t) { public void config(String msg, Throwable t) {
loggerProxy.doLog(CONFIG, msg, t); loggerProxy.doLog(Level.CONFIG, msg, t);
} }
public void config(String msg, Object... params) { public void config(String msg, Object... params) {
loggerProxy.doLog(CONFIG, msg, params); loggerProxy.doLog(Level.CONFIG, msg, params);
} }
/** /**
* Logs a FINE message. * Logs a FINE message.
*/ */
public void fine(String msg) { public void fine(String msg) {
loggerProxy.doLog(FINE, msg); loggerProxy.doLog(Level.FINE, msg);
} }
public void fine(String msg, Throwable t) { public void fine(String msg, Throwable t) {
loggerProxy.doLog(FINE, msg, t); loggerProxy.doLog(Level.FINE, msg, t);
} }
public void fine(String msg, Object... params) { public void fine(String msg, Object... params) {
loggerProxy.doLog(FINE, msg, params); loggerProxy.doLog(Level.FINE, msg, params);
} }
/** /**
* Logs a FINER message. * Logs a FINER message.
*/ */
public void finer(String msg) { public void finer(String msg) {
loggerProxy.doLog(FINER, msg); loggerProxy.doLog(Level.FINER, msg);
} }
public void finer(String msg, Throwable t) { public void finer(String msg, Throwable t) {
loggerProxy.doLog(FINER, msg, t); loggerProxy.doLog(Level.FINER, msg, t);
} }
public void finer(String msg, Object... params) { public void finer(String msg, Object... params) {
loggerProxy.doLog(FINER, msg, params); loggerProxy.doLog(Level.FINER, msg, params);
} }
/** /**
* Logs a FINEST message. * Logs a FINEST message.
*/ */
public void finest(String msg) { public void finest(String msg) {
loggerProxy.doLog(FINEST, msg); loggerProxy.doLog(Level.FINEST, msg);
} }
public void finest(String msg, Throwable t) { public void finest(String msg, Throwable t) {
loggerProxy.doLog(FINEST, msg, t); loggerProxy.doLog(Level.FINEST, msg, t);
} }
public void finest(String msg, Object... params) { public void finest(String msg, Object... params) {
loggerProxy.doLog(FINEST, msg, params); loggerProxy.doLog(Level.FINEST, msg, params);
} }
/** /**
...@@ -421,7 +481,7 @@ public class PlatformLogger { ...@@ -421,7 +481,7 @@ public class PlatformLogger {
} }
boolean isEnabled() { boolean isEnabled() {
return effectiveLevel != OFF; return effectiveLevel != Level.OFF;
} }
Level getLevel() { Level getLevel() {
...@@ -457,7 +517,7 @@ public class PlatformLogger { ...@@ -457,7 +517,7 @@ public class PlatformLogger {
boolean isLoggable(Level level) { boolean isLoggable(Level level) {
Level effectiveLevel = this.effectiveLevel; Level effectiveLevel = this.effectiveLevel;
return level.intValue() >= effectiveLevel.intValue() && effectiveLevel != OFF; return level.intValue() >= effectiveLevel.intValue() && effectiveLevel != Level.OFF;
} }
// derive effective level (could do inheritance search like j.u.l.Logger) // derive effective level (could do inheritance search like j.u.l.Logger)
...@@ -611,15 +671,18 @@ public class PlatformLogger { ...@@ -611,15 +671,18 @@ public class PlatformLogger {
/** /**
* Returns the PlatformLogger.Level mapped from j.u.l.Level * Returns the PlatformLogger.Level mapped from j.u.l.Level
* set in the logger. * set in the logger. If the j.u.l.Logger is set to a custom Level,
* @throw IllegalArgumentException if j.u.l.Logger is set to * this method will return the nearest Level.
* a custom j.u.l.Level
*/ */
Level getLevel() { Level getLevel() {
Object javaLevel = LoggingSupport.getLevel(javaLogger); Object javaLevel = LoggingSupport.getLevel(javaLogger);
return javaLevel == null if (javaLevel == null) return null;
? null
: Level.valueOf(LoggingSupport.getLevelName(javaLevel)); try {
return Level.valueOf(LoggingSupport.getLevelName(javaLevel));
} catch (IllegalArgumentException e) {
return Level.valueOf(LoggingSupport.getLevelValue(javaLevel));
}
} }
void setLevel(Level level) { void setLevel(Level level) {
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.logging.*; import java.util.logging.*;
import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger;
import static sun.util.logging.PlatformLogger.Level.*;
public class PlatformLoggerTest { public class PlatformLoggerTest {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
...@@ -69,40 +70,63 @@ public class PlatformLoggerTest { ...@@ -69,40 +70,63 @@ public class PlatformLoggerTest {
checkLogger(GOO_PLATFORM_LOGGER, null); checkLogger(GOO_PLATFORM_LOGGER, null);
checkLogger(BAR_LOGGER, Level.WARNING); checkLogger(BAR_LOGGER, Level.WARNING);
foo.setLevel(PlatformLogger.SEVERE); foo.setLevel(PlatformLogger.Level.SEVERE);
checkLogger(FOO_PLATFORM_LOGGER, Level.SEVERE); checkLogger(FOO_PLATFORM_LOGGER, Level.SEVERE);
checkPlatformLoggerLevels(foo, bar); checkPlatformLoggerLevels(foo, bar);
} }
// don't use java.util.logging here to prevent it from initialized
private static void checkPlatformLogger(PlatformLogger logger, String name) { private static void checkPlatformLogger(PlatformLogger logger, String name) {
if (!logger.getName().equals(name)) { if (!logger.getName().equals(name)) {
throw new RuntimeException("Invalid logger's name " + throw new RuntimeException("Invalid logger's name " +
logger.getName() + " but expected " + name); logger.getName() + " but expected " + name);
} }
if (logger.getLevel() != null) { if (logger.level() != null) {
throw new RuntimeException("Invalid default level for logger " + throw new RuntimeException("Invalid default level for logger " +
logger.getName() + ": " + logger.getLevel()); logger.getName() + ": " + logger.level());
} }
if (logger.isLoggable(PlatformLogger.FINE) != false) { checkLoggable(logger, FINE, false);
throw new RuntimeException("isLoggerable(FINE) returns true for logger " +
logger.getName() + " but expected false"); logger.setLevel(FINER);
checkLevel(logger, FINER);
checkLoggable(logger, FINER, true);
checkLoggable(logger, FINE, true);
checkLoggable(logger, FINEST, false);
logger.info("OK: Testing log message");
} }
logger.setLevel(PlatformLogger.FINER); private static void checkLoggable(PlatformLogger logger, PlatformLogger.Level level, boolean expected) {
if (logger.getLevel() != PlatformLogger.FINER) { if (logger.isLoggable(level) != expected) {
throw new RuntimeException("Invalid level for logger " + throw new RuntimeException("logger " + logger.getName() + ": " + level +
logger.getName() + " " + logger.getLevel()); (expected ? " not loggable" : " loggable"));
} }
if (logger.isLoggable(PlatformLogger.FINE) != true) { if (logger.isLoggable(level.intValue()) != expected) {
throw new RuntimeException("isLoggerable(FINE) returns false for logger " + throw new RuntimeException("logger " + logger.getName() + ": " + level.intValue() +
logger.getName() + " but expected true"); (expected ? " not loggable" : " loggable"));
} }
logger.info("OK: Testing log message"); int value = level.intValue() + 5; // custom level value
if (expected && !logger.isLoggable(value)) {
throw new RuntimeException("logger " + logger.getName() + ": " + value +
" not loggable");
}
}
private static void checkLevel(PlatformLogger logger, PlatformLogger.Level level) {
if (logger.level() != level) {
throw new RuntimeException("Invalid level for logger " +
logger.getName() + ": " + logger.level() + " != " + level);
}
if (logger.getLevel() != level.intValue()) {
throw new RuntimeException("Invalid level for logger " +
logger.getName() + ": " + logger.getLevel() + " != " + level.intValue());
}
} }
private static void checkLogger(String name, Level level) { private static void checkLogger(String name, Level level) {
...@@ -146,23 +170,29 @@ public class PlatformLoggerTest { ...@@ -146,23 +170,29 @@ public class PlatformLoggerTest {
for (Level level : levels) { for (Level level : levels) {
PlatformLogger.Level platformLevel = PlatformLogger.Level.valueOf(level.getName()); PlatformLogger.Level platformLevel = PlatformLogger.Level.valueOf(level.getName());
for (PlatformLogger logger : loggers) { for (PlatformLogger logger : loggers) {
// verify PlatformLogger.setLevel to a given level logger.setLevel(platformLevel); // setLevel(PlatformLogger.Level)
logger.setLevel(platformLevel); checkLoggerLevel(logger, level);
PlatformLogger.Level retrievedPlatformLevel = logger.getLevel();
if (platformLevel != retrievedPlatformLevel) { logger.setLevel(ALL); // setLevel(int)
throw new RuntimeException("Retrieved PlatformLogger level " + checkLoggerLevel(logger, Level.ALL);
retrievedPlatformLevel + }
" is not the same as set level " + platformLevel); }
}
private static void checkLoggerLevel(PlatformLogger logger, Level level) {
PlatformLogger.Level plevel = PlatformLogger.Level.valueOf(level.getName());
if (plevel != logger.level()) {
throw new RuntimeException("Retrieved PlatformLogger level "
+ logger.level()
+ " is not the same as set level " + plevel);
} }
// check the level set in java.util.logging.Logger // check the level set in java.util.logging.Logger
Logger javaLogger = LogManager.getLogManager().getLogger(logger.getName()); Logger javaLogger = LogManager.getLogManager().getLogger(logger.getName());
Level javaLevel = javaLogger.getLevel(); Level javaLevel = javaLogger.getLevel();
if (javaLogger.getLevel() != level) { if (javaLogger.getLevel() != level) {
throw new RuntimeException("Retrieved backing java.util.logging.Logger level " + throw new RuntimeException("Retrieved backing java.util.logging.Logger level "
javaLevel + " is not the expected " + level); + javaLevel + " is not the expected " + level);
}
}
} }
} }
...@@ -174,21 +204,23 @@ public class PlatformLoggerTest { ...@@ -174,21 +204,23 @@ public class PlatformLoggerTest {
+ " PlatformLogger.Level" + platformLevel); + " PlatformLogger.Level" + platformLevel);
} }
PlatformLogger.Level plevel;
try { try {
// validate if there is a public static final field in PlatformLogger // validate if there is a public static final field in PlatformLogger
// matching the level name Field constantField = PlatformLogger.class.getField(level.getName());
Field platformLevelField = PlatformLogger.class.getField(level.getName()); int l = (int) constantField.get(null);
plevel = (PlatformLogger.Level) platformLevelField.get(null); if (l != platformLevel.intValue()) {
throw new RuntimeException("static final " + level.getName() + " (" +
l + ") != " + platformLevel.intValue());
}
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("No public static PlatformLogger." + level.getName() + throw new RuntimeException("No public static PlatformLogger." + level.getName() +
" field", e); " field", e);
} }
if (!plevel.name().equals(level.getName())) if (!platformLevel.name().equals(level.getName()))
throw new RuntimeException("The value of PlatformLogger." + level.getName() + ".name() is " throw new RuntimeException("The value of PlatformLogger." + level.getName() + ".name() is "
+ platformLevel.name() + " but expected " + level.getName()); + platformLevel.name() + " but expected " + level.getName());
if (plevel.intValue() != level.intValue()) if (platformLevel.intValue() != level.intValue())
throw new RuntimeException("The value of PlatformLogger." + level.intValue() + ".intValue() is " throw new RuntimeException("The value of PlatformLogger." + level.intValue() + ".intValue() is "
+ platformLevel.intValue() + " but expected " + level.intValue()); + platformLevel.intValue() + " but expected " + level.intValue());
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册