< prev index next >

src/java.base/share/classes/jdk/internal/logger/BootstrapLogger.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 902,924 **** String fname = System.getProperty("java.util.logging.config.file"); return (cname != null || fname != null) ? LoggingBackend.JUL_WITH_CONFIG : LoggingBackend.JUL_DEFAULT; } else { ! // SimpleLogger is used return LoggingBackend.NONE; } } }); } } ! // We will use temporary SimpleConsoleLoggers if // the logging backend is JUL, there is no custom config, // and the LogManager has not been initialized yet. ! private static boolean useTemporaryLoggers() { // being paranoid: this should already have been checked if (!isBooted()) return true; return DetectBackend.detectedBackend == LoggingBackend.JUL_DEFAULT && !logManagerConfigured; } --- 902,924 ---- String fname = System.getProperty("java.util.logging.config.file"); return (cname != null || fname != null) ? LoggingBackend.JUL_WITH_CONFIG : LoggingBackend.JUL_DEFAULT; } else { ! // SimpleConsoleLogger is used return LoggingBackend.NONE; } } }); } } ! // We will use a temporary SurrogateLogger if // the logging backend is JUL, there is no custom config, // and the LogManager has not been initialized yet. ! private static boolean useSurrogateLoggers() { // being paranoid: this should already have been checked if (!isBooted()) return true; return DetectBackend.detectedBackend == LoggingBackend.JUL_DEFAULT && !logManagerConfigured; }
*** 929,957 **** // - the logging backend is JUL, there is no custom config, // and the LogManager has not been initialized yet. public static synchronized boolean useLazyLoggers() { return !BootstrapLogger.isBooted() || DetectBackend.detectedBackend == LoggingBackend.CUSTOM ! || useTemporaryLoggers(); } // Called by LazyLoggerAccessor. This method will determine whether // to create a BootstrapLogger (if the VM is not yet booted), ! // a SimpleConsoleLogger (if JUL is the default backend and there // is no custom JUL configuration and LogManager is not yet initialized), // or a logger returned by the loaded LoggerFinder (all other cases). static Logger getLogger(LazyLoggerAccessor accessor) { if (!BootstrapLogger.isBooted()) { return new BootstrapLogger(accessor); } else { ! boolean temporary = useTemporaryLoggers(); ! if (temporary) { // JUL is the default backend, there is no custom configuration, // LogManager has not been used. synchronized(BootstrapLogger.class) { ! if (useTemporaryLoggers()) { ! return makeTemporaryLogger(accessor); } } } // Already booted. Return the real logger. return accessor.createLogger(); --- 929,956 ---- // - the logging backend is JUL, there is no custom config, // and the LogManager has not been initialized yet. public static synchronized boolean useLazyLoggers() { return !BootstrapLogger.isBooted() || DetectBackend.detectedBackend == LoggingBackend.CUSTOM ! || useSurrogateLoggers(); } // Called by LazyLoggerAccessor. This method will determine whether // to create a BootstrapLogger (if the VM is not yet booted), ! // a SurrogateLogger (if JUL is the default backend and there // is no custom JUL configuration and LogManager is not yet initialized), // or a logger returned by the loaded LoggerFinder (all other cases). static Logger getLogger(LazyLoggerAccessor accessor) { if (!BootstrapLogger.isBooted()) { return new BootstrapLogger(accessor); } else { ! if (useSurrogateLoggers()) { // JUL is the default backend, there is no custom configuration, // LogManager has not been used. synchronized(BootstrapLogger.class) { ! if (useSurrogateLoggers()) { ! return createSurrogateLogger(accessor); } } } // Already booted. Return the real logger. return accessor.createLogger();
*** 959,1008 **** } // If the backend is JUL, and there is no custom configuration, and // nobody has attempted to call LogManager.getLogManager() yet, then ! // we can temporarily substitute JUL Logger with SimpleConsoleLoggers, // which avoids the cost of actually loading up the LogManager... ! // The TemporaryLoggers class has the logic to create such temporary // loggers, and to possibly replace them with real JUL loggers if // someone calls LogManager.getLogManager(). ! static final class TemporaryLoggers implements ! Function<LazyLoggerAccessor, SimpleConsoleLogger> { // all accesses must be synchronized on the outer BootstrapLogger.class ! final Map<LazyLoggerAccessor, SimpleConsoleLogger> temporaryLoggers = new HashMap<>(); // all accesses must be synchronized on the outer BootstrapLogger.class ! // The temporaryLoggers map will be cleared when LogManager is initialized. boolean cleared; @Override // all accesses must be synchronized on the outer BootstrapLogger.class ! public SimpleConsoleLogger apply(LazyLoggerAccessor t) { if (cleared) throw new IllegalStateException("LoggerFinder already initialized"); ! return SimpleConsoleLogger.makeSimpleLogger(t.getLoggerName(), true); } // all accesses must be synchronized on the outer BootstrapLogger.class ! SimpleConsoleLogger get(LazyLoggerAccessor a) { if (cleared) throw new IllegalStateException("LoggerFinder already initialized"); ! return temporaryLoggers.computeIfAbsent(a, this); } // all accesses must be synchronized on the outer BootstrapLogger.class ! Map<LazyLoggerAccessor, SimpleConsoleLogger> drainTemporaryLoggers() { ! if (temporaryLoggers.isEmpty()) return null; if (cleared) throw new IllegalStateException("LoggerFinder already initialized"); ! final Map<LazyLoggerAccessor, SimpleConsoleLogger> accessors = new HashMap<>(temporaryLoggers); ! temporaryLoggers.clear(); cleared = true; return accessors; } ! static void resetTemporaryLoggers(Map<LazyLoggerAccessor, SimpleConsoleLogger> accessors) { // When the backend is JUL we want to force the creation of // JUL loggers here: some tests are expecting that the // PlatformLogger will create JUL loggers as soon as the // LogManager is initialized. // --- 958,1007 ---- } // If the backend is JUL, and there is no custom configuration, and // nobody has attempted to call LogManager.getLogManager() yet, then ! // we can temporarily substitute JUL Logger with SurrogateLoggers, // which avoids the cost of actually loading up the LogManager... ! // The RedirectedLoggers class has the logic to create such surrogate // loggers, and to possibly replace them with real JUL loggers if // someone calls LogManager.getLogManager(). ! static final class RedirectedLoggers implements ! Function<LazyLoggerAccessor, SurrogateLogger> { // all accesses must be synchronized on the outer BootstrapLogger.class ! final Map<LazyLoggerAccessor, SurrogateLogger> redirectedLoggers = new HashMap<>(); // all accesses must be synchronized on the outer BootstrapLogger.class ! // The redirectLoggers map will be cleared when LogManager is initialized. boolean cleared; @Override // all accesses must be synchronized on the outer BootstrapLogger.class ! public SurrogateLogger apply(LazyLoggerAccessor t) { if (cleared) throw new IllegalStateException("LoggerFinder already initialized"); ! return SurrogateLogger.makeSurrogateLogger(t.getLoggerName()); } // all accesses must be synchronized on the outer BootstrapLogger.class ! SurrogateLogger get(LazyLoggerAccessor a) { if (cleared) throw new IllegalStateException("LoggerFinder already initialized"); ! return redirectedLoggers.computeIfAbsent(a, this); } // all accesses must be synchronized on the outer BootstrapLogger.class ! Map<LazyLoggerAccessor, SurrogateLogger> drainLoggersMap() { ! if (redirectedLoggers.isEmpty()) return null; if (cleared) throw new IllegalStateException("LoggerFinder already initialized"); ! final Map<LazyLoggerAccessor, SurrogateLogger> accessors = new HashMap<>(redirectedLoggers); ! redirectedLoggers.clear(); cleared = true; return accessors; } ! static void replaceSurrogateLoggers(Map<LazyLoggerAccessor, SurrogateLogger> accessors) { // When the backend is JUL we want to force the creation of // JUL loggers here: some tests are expecting that the // PlatformLogger will create JUL loggers as soon as the // LogManager is initialized. //
*** 1010,1064 **** // of the wrapped logger until they are next accessed. // final LoggingBackend detectedBackend = DetectBackend.detectedBackend; final boolean lazy = detectedBackend != LoggingBackend.JUL_DEFAULT && detectedBackend != LoggingBackend.JUL_WITH_CONFIG; ! for (Map.Entry<LazyLoggerAccessor, SimpleConsoleLogger> a : accessors.entrySet()) { a.getKey().release(a.getValue(), !lazy); } } // all accesses must be synchronized on the outer BootstrapLogger.class ! static final TemporaryLoggers INSTANCE = new TemporaryLoggers(); } ! static synchronized Logger makeTemporaryLogger(LazyLoggerAccessor a) { ! // accesses to TemporaryLoggers is synchronized on BootstrapLogger.class ! return TemporaryLoggers.INSTANCE.get(a); } private static volatile boolean logManagerConfigured; ! private static synchronized Map<LazyLoggerAccessor, SimpleConsoleLogger> ! releaseTemporaryLoggers() { // first check whether there's a chance that we have used ! // temporary loggers; Will be false if logManagerConfigured is already // true. ! final boolean clearTemporaryLoggers = useTemporaryLoggers(); // then sets the flag that tells that the log manager is configured logManagerConfigured = true; ! // finally replace all temporary loggers by real JUL loggers ! if (clearTemporaryLoggers) { ! // accesses to TemporaryLoggers is synchronized on BootstrapLogger.class ! return TemporaryLoggers.INSTANCE.drainTemporaryLoggers(); } else { return null; } } public static void redirectTemporaryLoggers() { // This call is synchronized on BootstrapLogger.class. ! final Map<LazyLoggerAccessor, SimpleConsoleLogger> accessors = ! releaseTemporaryLoggers(); // We will now reset the logger accessors, triggering the ! // (possibly lazy) replacement of any temporary logger by the // real logger returned from the loaded LoggerFinder. if (accessors != null) { ! TemporaryLoggers.resetTemporaryLoggers(accessors); } BootstrapExecutors.flush(); } --- 1009,1065 ---- // of the wrapped logger until they are next accessed. // final LoggingBackend detectedBackend = DetectBackend.detectedBackend; final boolean lazy = detectedBackend != LoggingBackend.JUL_DEFAULT && detectedBackend != LoggingBackend.JUL_WITH_CONFIG; ! for (Map.Entry<LazyLoggerAccessor, SurrogateLogger> a : accessors.entrySet()) { a.getKey().release(a.getValue(), !lazy); } } // all accesses must be synchronized on the outer BootstrapLogger.class ! static final RedirectedLoggers INSTANCE = new RedirectedLoggers(); } ! static synchronized Logger createSurrogateLogger(LazyLoggerAccessor a) { ! // accesses to RedirectedLoggers is synchronized on BootstrapLogger.class ! return RedirectedLoggers.INSTANCE.get(a); } private static volatile boolean logManagerConfigured; ! private static synchronized Map<LazyLoggerAccessor, SurrogateLogger> ! releaseSurrogateLoggers() { // first check whether there's a chance that we have used ! // surrogate loggers; Will be false if logManagerConfigured is already // true. ! final boolean releaseSurrogateLoggers = useSurrogateLoggers(); // then sets the flag that tells that the log manager is configured logManagerConfigured = true; ! // finally retrieves all surrogate loggers that should be replaced ! // by real JUL loggers, and return them in the form of a redirected ! // loggers map. ! if (releaseSurrogateLoggers) { ! // accesses to RedirectedLoggers is synchronized on BootstrapLogger.class ! return RedirectedLoggers.INSTANCE.drainLoggersMap(); } else { return null; } } public static void redirectTemporaryLoggers() { // This call is synchronized on BootstrapLogger.class. ! final Map<LazyLoggerAccessor, SurrogateLogger> accessors = ! releaseSurrogateLoggers(); // We will now reset the logger accessors, triggering the ! // (possibly lazy) replacement of any temporary surrogate logger by the // real logger returned from the loaded LoggerFinder. if (accessors != null) { ! RedirectedLoggers.replaceSurrogateLoggers(accessors); } BootstrapExecutors.flush(); }
< prev index next >