1 /*
2  * Copyright (c) 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 package jdk.internal.logger;
27 
28 import java.util.ResourceBundle;
29 import java.util.function.Supplier;
30 import java.lang.System.Logger;
31 import java.lang.System.Logger.Level;
32 import sun.util.logging.PlatformLogger;
33 
34 /**
35  * An implementation of {@link System.Logger System.Logger}
36  * that redirects all calls to a wrapped instance of {@link
37  * System.Logger System.Logger}
38  *
39  * @param <L> Type of the wrapped Logger: {@code Logger} or
40  *        an extension of that interface.
41  *
42  */
43 abstract class AbstractLoggerWrapper<L extends Logger>
44     implements Logger, PlatformLogger.Bridge, PlatformLogger.ConfigurableBridge {
45 
AbstractLoggerWrapper()46     AbstractLoggerWrapper() { }
47 
wrapped()48     abstract L wrapped();
49 
platformProxy()50     abstract PlatformLogger.Bridge platformProxy();
51 
getWrapped()52     L getWrapped() {
53         return wrapped();
54     }
55 
56     @Override
getName()57     public final String getName() {
58         return wrapped().getName();
59     }
60 
61     // -----------------------------------------------------------------
62     // Generic methods taking a Level as parameter
63     // -----------------------------------------------------------------
64 
65 
66     @Override
isLoggable(Level level)67     public boolean isLoggable(Level level) {
68         return wrapped().isLoggable(level);
69     }
70 
71     @Override
log(Level level, String msg)72     public void log(Level level, String msg) {
73         wrapped().log(level, msg);
74     }
75 
76     @Override
log(Level level, Supplier<String> msgSupplier)77     public void log(Level level,
78                     Supplier<String> msgSupplier) {
79         wrapped().log(level, msgSupplier);
80     }
81 
82     @Override
log(Level level, Object obj)83     public void log(Level level, Object obj) {
84         wrapped().log(level, obj);
85     }
86 
87     @Override
log(Level level, String msg, Throwable thrown)88     public void log(Level level,
89                    String msg, Throwable thrown) {
90         wrapped().log(level, msg, thrown);
91     }
92 
93     @Override
log(Level level, Supplier<String> msgSupplier, Throwable thrown)94     public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {
95         wrapped().log(level, msgSupplier, thrown);
96     }
97 
98     @Override
log(Level level, String format, Object... params)99     public void log(Level level,
100                     String format, Object... params) {
101         wrapped().log(level, format, params);
102     }
103 
104     @Override
log(Level level, ResourceBundle bundle, String key, Throwable thrown)105     public void log(Level level, ResourceBundle bundle,
106                     String key, Throwable thrown) {
107         wrapped().log(level, bundle, key, thrown);
108     }
109 
110     @Override
log(Level level, ResourceBundle bundle, String format, Object... params)111     public void log(Level level, ResourceBundle bundle,
112                     String format, Object... params) {
113         wrapped().log(level, bundle, format, params);
114     }
115 
116     // ---------------------------------------------------------
117     // Methods from PlatformLogger.Bridge
118     // ---------------------------------------------------------
119 
120     @Override
isLoggable(PlatformLogger.Level level)121     public boolean isLoggable(PlatformLogger.Level level) {
122         final PlatformLogger.Bridge platformProxy = platformProxy();
123         if (platformProxy == null) return isLoggable(level.systemLevel());
124         else return platformProxy.isLoggable(level);
125     }
126 
127     @Override
isEnabled()128     public boolean isEnabled() {
129         final PlatformLogger.Bridge platformProxy = platformProxy();
130         return platformProxy == null || platformProxy.isEnabled();
131     }
132 
133     @Override
log(PlatformLogger.Level level, String msg)134     public void log(PlatformLogger.Level level, String msg) {
135         final PlatformLogger.Bridge platformProxy = platformProxy();
136         if (platformProxy == null)  {
137             wrapped().log(level.systemLevel(), msg);
138         } else {
139             platformProxy.log(level, msg);
140         }
141     }
142 
143     @Override
log(PlatformLogger.Level level, String msg, Throwable thrown)144     public void log(PlatformLogger.Level level, String msg, Throwable thrown) {
145         final PlatformLogger.Bridge platformProxy = platformProxy();
146         if (platformProxy == null)  {
147             wrapped().log(level.systemLevel(), msg, thrown);
148         } else {
149             platformProxy.log(level, msg, thrown);
150         }
151     }
152 
153     @Override
log(PlatformLogger.Level level, String msg, Object... params)154     public void log(PlatformLogger.Level level, String msg, Object... params) {
155         final PlatformLogger.Bridge platformProxy = platformProxy();
156         if (platformProxy == null)  {
157             wrapped().log(level.systemLevel(), msg, params);
158         } else {
159             platformProxy.log(level, msg, params);
160         }
161     }
162 
163     @Override
log(PlatformLogger.Level level, Supplier<String> msgSupplier)164     public void log(PlatformLogger.Level level, Supplier<String> msgSupplier) {
165         final PlatformLogger.Bridge platformProxy = platformProxy();
166         if (platformProxy == null)  {
167             wrapped().log(level.systemLevel(),msgSupplier);
168         } else {
169             platformProxy.log(level,msgSupplier);
170         }
171     }
172 
173     @Override
log(PlatformLogger.Level level, Throwable thrown, Supplier<String> msgSupplier)174     public void log(PlatformLogger.Level level, Throwable thrown,
175                     Supplier<String> msgSupplier) {
176         final PlatformLogger.Bridge platformProxy = platformProxy();
177         if (platformProxy == null)  {
178             wrapped().log(level.systemLevel(), msgSupplier, thrown);
179         } else {
180             platformProxy.log(level, thrown, msgSupplier);
181         }
182     }
183 
184     @Override
logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg)185     public void logp(PlatformLogger.Level level, String sourceClass,
186                      String sourceMethod, String msg) {
187         final PlatformLogger.Bridge platformProxy = platformProxy();
188         if (platformProxy == null)  {
189             if (sourceClass == null && sourceMethod == null) { // best effort
190                 wrapped().log(level.systemLevel(), msg);
191             } else {
192                 Level systemLevel = level.systemLevel();
193                 Logger wrapped = wrapped();
194                 if (wrapped.isLoggable(systemLevel)) {
195                     sourceClass  = sourceClass  == null ? "" : sourceClass;
196                     sourceMethod = sourceMethod == null ? "" : sourceMethod;
197                     msg = msg == null ? "" : msg;
198                     wrapped.log(systemLevel, String.format("[%s %s] %s",
199                             sourceClass, sourceMethod, msg));
200                 }
201             }
202         } else {
203             platformProxy.logp(level, sourceClass, sourceMethod, msg);
204         }
205     }
206 
207     @Override
logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, Supplier<String> msgSupplier)208     public void logp(PlatformLogger.Level level, String sourceClass,
209                      String sourceMethod, Supplier<String> msgSupplier) {
210         final PlatformLogger.Bridge platformProxy = platformProxy();
211         if (platformProxy == null) { // best effort
212             if (sourceClass == null && sourceMethod == null) {
213                 wrapped().log(level.systemLevel(), msgSupplier);
214             } else {
215                 Level systemLevel = level.systemLevel();
216                 Logger wrapped = wrapped();
217                 if (wrapped.isLoggable(systemLevel)) {
218                     final String sClass  = sourceClass  == null ? "" : sourceClass;
219                     final String sMethod = sourceMethod == null ? "" : sourceMethod;
220                     wrapped.log(systemLevel, () -> String.format("[%s %s] %s",
221                             sClass, sMethod, msgSupplier.get()));
222                 }
223             }
224         } else {
225             platformProxy.logp(level, sourceClass, sourceMethod, msgSupplier);
226         }
227     }
228 
229     @Override
logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg, Object... params)230     public void logp(PlatformLogger.Level level, String sourceClass,
231                      String sourceMethod, String msg, Object... params) {
232         final PlatformLogger.Bridge platformProxy = platformProxy();
233         if (platformProxy == null) { // best effort
234             if (sourceClass == null && sourceMethod == null) {
235                 wrapped().log(level.systemLevel(), msg, params);
236             } else {
237                 Level systemLevel = level.systemLevel();
238                 Logger wrapped = wrapped();
239                 if (wrapped.isLoggable(systemLevel)) {
240                     sourceClass  = sourceClass  == null ? "" : sourceClass;
241                     sourceMethod = sourceMethod == null ? "" : sourceMethod;
242                     msg = msg == null ? "" : msg;
243                     wrapped.log(systemLevel, String.format("[%s %s] %s",
244                             sourceClass, sourceMethod, msg), params);
245                 }
246             }
247         } else {
248             platformProxy.logp(level, sourceClass, sourceMethod, msg, params);
249         }
250     }
251 
252     @Override
logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown)253     public void logp(PlatformLogger.Level level, String sourceClass,
254                      String sourceMethod, String msg, Throwable thrown) {
255         final PlatformLogger.Bridge platformProxy = platformProxy();
256         if (platformProxy == null) { // best effort
257             if (sourceClass == null && sourceMethod == null) {
258                 wrapped().log(level.systemLevel(), msg, thrown);
259             } else {
260                 Level systemLevel = level.systemLevel();
261                 Logger wrapped = wrapped();
262                 if (wrapped.isLoggable(systemLevel)) {
263                     sourceClass  = sourceClass  == null ? "" : sourceClass;
264                     sourceMethod = sourceMethod == null ? "" : sourceMethod;
265                     msg = msg == null ? "" : msg;
266                     wrapped.log(systemLevel, String.format("[%s %s] %s",
267                             sourceClass, sourceMethod, msg), thrown);
268                 }
269             }
270         } else {
271             platformProxy.logp(level, sourceClass, sourceMethod, msg, thrown);
272         }
273     }
274 
275     @Override
logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, Throwable thrown, Supplier<String> msgSupplier)276     public void logp(PlatformLogger.Level level, String sourceClass,
277                      String sourceMethod, Throwable thrown,
278                      Supplier<String> msgSupplier) {
279         final PlatformLogger.Bridge platformProxy = platformProxy();
280         if (platformProxy == null)  { // best effort
281             if (sourceClass == null && sourceMethod == null) {
282                 wrapped().log(level.systemLevel(), msgSupplier, thrown);
283             } else {
284                 Level systemLevel = level.systemLevel();
285                 Logger wrapped = wrapped();
286                 if (wrapped.isLoggable(systemLevel)) {
287                     final String sClass  = sourceClass  == null ? "" : sourceClass;
288                     final String sMethod = sourceMethod == null ? "" : sourceMethod;
289                     wrapped.log(systemLevel,  () -> String.format("[%s %s] %s",
290                             sClass, sMethod, msgSupplier.get()), thrown);
291                 }
292             }
293         } else {
294             platformProxy.logp(level, sourceClass, sourceMethod,
295                                thrown, msgSupplier);
296         }
297     }
298 
299     @Override
logrb(PlatformLogger.Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String msg, Object... params)300     public void logrb(PlatformLogger.Level level, String sourceClass,
301                       String sourceMethod, ResourceBundle bundle,
302                       String msg, Object... params) {
303         final PlatformLogger.Bridge platformProxy = platformProxy();
304         if (platformProxy == null)  { // best effort
305             if (bundle != null || sourceClass == null && sourceMethod == null) {
306                 wrapped().log(level.systemLevel(), bundle, msg, params);
307             } else {
308                 Level systemLevel = level.systemLevel();
309                 Logger wrapped = wrapped();
310                 if (wrapped.isLoggable(systemLevel)) {
311                     sourceClass  = sourceClass  == null ? "" : sourceClass;
312                     sourceMethod = sourceMethod == null ? "" : sourceMethod;
313                     msg = msg == null ? "" : msg;
314                     wrapped.log(systemLevel, bundle, String.format("[%s %s] %s",
315                             sourceClass, sourceMethod, msg), params);
316                 }
317             }
318         } else {
319             platformProxy.logrb(level, sourceClass, sourceMethod,
320                     bundle, msg, params);
321         }
322     }
323 
324     @Override
logrb(PlatformLogger.Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String msg, Throwable thrown)325     public void logrb(PlatformLogger.Level level, String sourceClass,
326                       String sourceMethod, ResourceBundle bundle, String msg,
327                       Throwable thrown) {
328         final PlatformLogger.Bridge platformProxy = platformProxy();
329         if (platformProxy == null)  { // best effort
330             if (bundle != null || sourceClass == null && sourceMethod == null) {
331                 wrapped().log(level.systemLevel(), bundle, msg, thrown);
332             } else {
333                 Level systemLevel = level.systemLevel();
334                 Logger wrapped = wrapped();
335                 if (wrapped.isLoggable(systemLevel)) {
336                     sourceClass  = sourceClass  == null ? "" : sourceClass;
337                     sourceMethod = sourceMethod == null ? "" : sourceMethod;
338                     msg = msg == null ? "" : msg;
339                     wrapped.log(systemLevel, bundle, String.format("[%s %s] %s",
340                             sourceClass, sourceMethod, msg), thrown);
341                 }
342             }
343         } else {
344             platformProxy.logrb(level, sourceClass, sourceMethod, bundle,
345                                 msg, thrown);
346         }
347     }
348 
349     @Override
logrb(PlatformLogger.Level level, ResourceBundle bundle, String msg, Throwable thrown)350     public void logrb(PlatformLogger.Level level, ResourceBundle bundle,
351                       String msg, Throwable thrown) {
352         final PlatformLogger.Bridge platformProxy = platformProxy();
353         if (platformProxy == null)  {
354             wrapped().log(level.systemLevel(), bundle, msg, thrown);
355         } else {
356             platformProxy.logrb(level, bundle, msg, thrown);
357         }
358     }
359 
360     @Override
logrb(PlatformLogger.Level level, ResourceBundle bundle, String msg, Object... params)361     public void logrb(PlatformLogger.Level level, ResourceBundle bundle,
362                       String msg, Object... params) {
363         final PlatformLogger.Bridge platformProxy = platformProxy();
364         if (platformProxy == null)  {
365             wrapped().log(level.systemLevel(), bundle, msg, params);
366         } else {
367             platformProxy.logrb(level, bundle, msg, params);
368         }
369     }
370 
371 
372     @Override
getLoggerConfiguration()373     public LoggerConfiguration getLoggerConfiguration() {
374         final PlatformLogger.Bridge platformProxy = platformProxy();
375         return platformProxy == null ? null
376                : PlatformLogger.ConfigurableBridge
377                        .getLoggerConfiguration(platformProxy);
378     }
379 
380 }
381