Path: blob/master/src/java.base/share/classes/jdk/internal/logger/AbstractLoggerWrapper.java
41159 views
/*1* Copyright (c) 2015, 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*/2425package jdk.internal.logger;2627import java.util.ResourceBundle;28import java.util.function.Supplier;29import java.lang.System.Logger;30import java.lang.System.Logger.Level;31import sun.util.logging.PlatformLogger;3233/**34* An implementation of {@link System.Logger System.Logger}35* that redirects all calls to a wrapped instance of {@link36* System.Logger System.Logger}37*38* @param <L> Type of the wrapped Logger: {@code Logger} or39* an extension of that interface.40*41*/42abstract class AbstractLoggerWrapper<L extends Logger>43implements Logger, PlatformLogger.Bridge, PlatformLogger.ConfigurableBridge {4445AbstractLoggerWrapper() { }4647abstract L wrapped();4849abstract PlatformLogger.Bridge platformProxy();5051L getWrapped() {52return wrapped();53}5455@Override56public final String getName() {57return wrapped().getName();58}5960// -----------------------------------------------------------------61// Generic methods taking a Level as parameter62// -----------------------------------------------------------------636465@Override66public boolean isLoggable(Level level) {67return wrapped().isLoggable(level);68}6970@Override71public void log(Level level, String msg) {72wrapped().log(level, msg);73}7475@Override76public void log(Level level,77Supplier<String> msgSupplier) {78wrapped().log(level, msgSupplier);79}8081@Override82public void log(Level level, Object obj) {83wrapped().log(level, obj);84}8586@Override87public void log(Level level,88String msg, Throwable thrown) {89wrapped().log(level, msg, thrown);90}9192@Override93public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {94wrapped().log(level, msgSupplier, thrown);95}9697@Override98public void log(Level level,99String format, Object... params) {100wrapped().log(level, format, params);101}102103@Override104public void log(Level level, ResourceBundle bundle,105String key, Throwable thrown) {106wrapped().log(level, bundle, key, thrown);107}108109@Override110public void log(Level level, ResourceBundle bundle,111String format, Object... params) {112wrapped().log(level, bundle, format, params);113}114115// ---------------------------------------------------------116// Methods from PlatformLogger.Bridge117// ---------------------------------------------------------118119@Override120public boolean isLoggable(PlatformLogger.Level level) {121final PlatformLogger.Bridge platformProxy = platformProxy();122if (platformProxy == null) return isLoggable(level.systemLevel());123else return platformProxy.isLoggable(level);124}125126@Override127public boolean isEnabled() {128final PlatformLogger.Bridge platformProxy = platformProxy();129return platformProxy == null || platformProxy.isEnabled();130}131132@Override133public void log(PlatformLogger.Level level, String msg) {134final PlatformLogger.Bridge platformProxy = platformProxy();135if (platformProxy == null) {136wrapped().log(level.systemLevel(), msg);137} else {138platformProxy.log(level, msg);139}140}141142@Override143public void log(PlatformLogger.Level level, String msg, Throwable thrown) {144final PlatformLogger.Bridge platformProxy = platformProxy();145if (platformProxy == null) {146wrapped().log(level.systemLevel(), msg, thrown);147} else {148platformProxy.log(level, msg, thrown);149}150}151152@Override153public void log(PlatformLogger.Level level, String msg, Object... params) {154final PlatformLogger.Bridge platformProxy = platformProxy();155if (platformProxy == null) {156wrapped().log(level.systemLevel(), msg, params);157} else {158platformProxy.log(level, msg, params);159}160}161162@Override163public void log(PlatformLogger.Level level, Supplier<String> msgSupplier) {164final PlatformLogger.Bridge platformProxy = platformProxy();165if (platformProxy == null) {166wrapped().log(level.systemLevel(),msgSupplier);167} else {168platformProxy.log(level,msgSupplier);169}170}171172@Override173public void log(PlatformLogger.Level level, Throwable thrown,174Supplier<String> msgSupplier) {175final PlatformLogger.Bridge platformProxy = platformProxy();176if (platformProxy == null) {177wrapped().log(level.systemLevel(), msgSupplier, thrown);178} else {179platformProxy.log(level, thrown, msgSupplier);180}181}182183@Override184public void logp(PlatformLogger.Level level, String sourceClass,185String sourceMethod, String msg) {186final PlatformLogger.Bridge platformProxy = platformProxy();187if (platformProxy == null) {188if (sourceClass == null && sourceMethod == null) { // best effort189wrapped().log(level.systemLevel(), msg);190} else {191Level systemLevel = level.systemLevel();192Logger wrapped = wrapped();193if (wrapped.isLoggable(systemLevel)) {194sourceClass = sourceClass == null ? "" : sourceClass;195sourceMethod = sourceMethod == null ? "" : sourceMethod;196msg = msg == null ? "" : msg;197wrapped.log(systemLevel, String.format("[%s %s] %s",198sourceClass, sourceMethod, msg));199}200}201} else {202platformProxy.logp(level, sourceClass, sourceMethod, msg);203}204}205206@Override207public void logp(PlatformLogger.Level level, String sourceClass,208String sourceMethod, Supplier<String> msgSupplier) {209final PlatformLogger.Bridge platformProxy = platformProxy();210if (platformProxy == null) { // best effort211if (sourceClass == null && sourceMethod == null) {212wrapped().log(level.systemLevel(), msgSupplier);213} else {214Level systemLevel = level.systemLevel();215Logger wrapped = wrapped();216if (wrapped.isLoggable(systemLevel)) {217final String sClass = sourceClass == null ? "" : sourceClass;218final String sMethod = sourceMethod == null ? "" : sourceMethod;219wrapped.log(systemLevel, () -> String.format("[%s %s] %s",220sClass, sMethod, msgSupplier.get()));221}222}223} else {224platformProxy.logp(level, sourceClass, sourceMethod, msgSupplier);225}226}227228@Override229public void logp(PlatformLogger.Level level, String sourceClass,230String sourceMethod, String msg, Object... params) {231final PlatformLogger.Bridge platformProxy = platformProxy();232if (platformProxy == null) { // best effort233if (sourceClass == null && sourceMethod == null) {234wrapped().log(level.systemLevel(), msg, params);235} else {236Level systemLevel = level.systemLevel();237Logger wrapped = wrapped();238if (wrapped.isLoggable(systemLevel)) {239sourceClass = sourceClass == null ? "" : sourceClass;240sourceMethod = sourceMethod == null ? "" : sourceMethod;241msg = msg == null ? "" : msg;242wrapped.log(systemLevel, String.format("[%s %s] %s",243sourceClass, sourceMethod, msg), params);244}245}246} else {247platformProxy.logp(level, sourceClass, sourceMethod, msg, params);248}249}250251@Override252public void logp(PlatformLogger.Level level, String sourceClass,253String sourceMethod, String msg, Throwable thrown) {254final PlatformLogger.Bridge platformProxy = platformProxy();255if (platformProxy == null) { // best effort256if (sourceClass == null && sourceMethod == null) {257wrapped().log(level.systemLevel(), msg, thrown);258} else {259Level systemLevel = level.systemLevel();260Logger wrapped = wrapped();261if (wrapped.isLoggable(systemLevel)) {262sourceClass = sourceClass == null ? "" : sourceClass;263sourceMethod = sourceMethod == null ? "" : sourceMethod;264msg = msg == null ? "" : msg;265wrapped.log(systemLevel, String.format("[%s %s] %s",266sourceClass, sourceMethod, msg), thrown);267}268}269} else {270platformProxy.logp(level, sourceClass, sourceMethod, msg, thrown);271}272}273274@Override275public void logp(PlatformLogger.Level level, String sourceClass,276String sourceMethod, Throwable thrown,277Supplier<String> msgSupplier) {278final PlatformLogger.Bridge platformProxy = platformProxy();279if (platformProxy == null) { // best effort280if (sourceClass == null && sourceMethod == null) {281wrapped().log(level.systemLevel(), msgSupplier, thrown);282} else {283Level systemLevel = level.systemLevel();284Logger wrapped = wrapped();285if (wrapped.isLoggable(systemLevel)) {286final String sClass = sourceClass == null ? "" : sourceClass;287final String sMethod = sourceMethod == null ? "" : sourceMethod;288wrapped.log(systemLevel, () -> String.format("[%s %s] %s",289sClass, sMethod, msgSupplier.get()), thrown);290}291}292} else {293platformProxy.logp(level, sourceClass, sourceMethod,294thrown, msgSupplier);295}296}297298@Override299public void logrb(PlatformLogger.Level level, String sourceClass,300String sourceMethod, ResourceBundle bundle,301String msg, Object... params) {302final PlatformLogger.Bridge platformProxy = platformProxy();303if (platformProxy == null) { // best effort304if (bundle != null || sourceClass == null && sourceMethod == null) {305wrapped().log(level.systemLevel(), bundle, msg, params);306} else {307Level systemLevel = level.systemLevel();308Logger wrapped = wrapped();309if (wrapped.isLoggable(systemLevel)) {310sourceClass = sourceClass == null ? "" : sourceClass;311sourceMethod = sourceMethod == null ? "" : sourceMethod;312msg = msg == null ? "" : msg;313wrapped.log(systemLevel, bundle, String.format("[%s %s] %s",314sourceClass, sourceMethod, msg), params);315}316}317} else {318platformProxy.logrb(level, sourceClass, sourceMethod,319bundle, msg, params);320}321}322323@Override324public void logrb(PlatformLogger.Level level, String sourceClass,325String sourceMethod, ResourceBundle bundle, String msg,326Throwable thrown) {327final PlatformLogger.Bridge platformProxy = platformProxy();328if (platformProxy == null) { // best effort329if (bundle != null || sourceClass == null && sourceMethod == null) {330wrapped().log(level.systemLevel(), bundle, msg, thrown);331} else {332Level systemLevel = level.systemLevel();333Logger wrapped = wrapped();334if (wrapped.isLoggable(systemLevel)) {335sourceClass = sourceClass == null ? "" : sourceClass;336sourceMethod = sourceMethod == null ? "" : sourceMethod;337msg = msg == null ? "" : msg;338wrapped.log(systemLevel, bundle, String.format("[%s %s] %s",339sourceClass, sourceMethod, msg), thrown);340}341}342} else {343platformProxy.logrb(level, sourceClass, sourceMethod, bundle,344msg, thrown);345}346}347348@Override349public void logrb(PlatformLogger.Level level, ResourceBundle bundle,350String msg, Throwable thrown) {351final PlatformLogger.Bridge platformProxy = platformProxy();352if (platformProxy == null) {353wrapped().log(level.systemLevel(), bundle, msg, thrown);354} else {355platformProxy.logrb(level, bundle, msg, thrown);356}357}358359@Override360public void logrb(PlatformLogger.Level level, ResourceBundle bundle,361String msg, Object... params) {362final PlatformLogger.Bridge platformProxy = platformProxy();363if (platformProxy == null) {364wrapped().log(level.systemLevel(), bundle, msg, params);365} else {366platformProxy.logrb(level, bundle, msg, params);367}368}369370371@Override372public LoggerConfiguration getLoggerConfiguration() {373final PlatformLogger.Bridge platformProxy = platformProxy();374return platformProxy == null ? null375: PlatformLogger.ConfigurableBridge376.getLoggerConfiguration(platformProxy);377}378379}380381382