1 /*
2  * Copyright (c) 2018, 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.net.http.common;
27 
28 import java.util.function.Supplier;
29 
30 /**
31  * An internal {@code System.Logger} that is used for internal
32  * debugging purposes in the {@link java.net.http} module.
33  * <p>
34  * Though not enforced, this interface is designed for emitting
35  * debug messages with Level.DEBUG.
36  * <p>
37  * It defines {@code log} methods that default to {@code Level.DEBUG},
38  * so that they can be called in statements like:
39  * <pre>{@code debug.log("some %s with %d %s", message(), one(), params());}</pre>
40  *
41  * @implSpec
42  * This interface is implemented by loggers returned by
43  * {@link Utils#getDebugLogger(Supplier, boolean)},
44  * {@link Utils#getWebSocketLogger(Supplier, boolean)}and
45  * {@link Utils#getHpackLogger(Supplier, boolean)}.
46  * It is not designed to be implemented by any other
47  * loggers. Do not use outside of this module.
48  */
49 public interface Logger extends System.Logger {
50     /**
51      * Tells whether this logger is on.
52      * @implSpec The default implementation for this method calls
53      * {@code this.isLoggable(Level.DEBUG);}
54      */
on()55     public default boolean on() {
56         return isLoggable(Level.DEBUG);
57     }
58 
59     /**
60      * Logs a message.
61      *
62      * @implSpec The default implementation for this method calls
63      * {@code this.log(Level.DEBUG, msg);}
64      *
65      * @param msg the string message (or a key in the message catalog, if
66      * this logger is a {@link
67      * System.LoggerFinder#getLocalizedLogger(java.lang.String,
68      * java.util.ResourceBundle, java.lang.Module) localized logger});
69      * can be {@code null}.
70      */
log(String msg)71     public default void log(String msg) {
72         log(Level.DEBUG, msg);
73     }
74 
75     /**
76      * Logs a lazily supplied message.
77      *
78      * @implSpec The default implementation for this method calls
79      * {@code this.log(Level.DEBUG, msgSupplier);}
80      *
81      * @param msgSupplier a supplier function that produces a message.
82      *
83      * @throws NullPointerException if {@code msgSupplier} is {@code null}.
84      */
log(Supplier<String> msgSupplier)85     public default void log(Supplier<String> msgSupplier) {
86         log(Level.DEBUG, msgSupplier);
87     }
88 
89     /**
90      * Logs a message produced from the given object.
91      *
92      * @implSpec The default implementation for this method calls
93      * {@code this.log(Level.DEBUG, obj);}
94      *
95      * @param obj the object to log.
96      *
97      * @throws NullPointerException if {@code obj} is {@code null}.
98      */
log(Object obj)99     public default void log(Object obj) {
100         log(Level.DEBUG,  obj);
101     }
102 
103     /**
104      * Logs a message associated with a given throwable.
105      *
106      * @implSpec The default implementation for this method calls
107      * {@code this.log(Level.DEBUG, msg, thrown);}
108      *
109      * @param msg the string message (or a key in the message catalog, if
110      * this logger is a {@link
111      * System.LoggerFinder#getLocalizedLogger(java.lang.String,
112      * java.util.ResourceBundle, java.lang.Module) localized logger});
113      * can be {@code null}.
114      * @param thrown a {@code Throwable} associated with the log message;
115      *        can be {@code null}.
116      */
log(String msg, Throwable thrown)117     public default void log(String msg, Throwable thrown) {
118         this.log(Level.DEBUG, msg, thrown);
119     }
120 
121     /**
122      * Logs a lazily supplied message associated with a given throwable.
123      *
124      * @implSpec The default implementation for this method calls
125      * {@code this.log(Level.DEBUG, msgSupplier, thrown);}
126      *
127      * @param msgSupplier a supplier function that produces a message.
128      * @param thrown a {@code Throwable} associated with log message;
129      *               can be {@code null}.
130      *
131      * @throws NullPointerException if {@code msgSupplier} is {@code null}.
132      */
log(Supplier<String> msgSupplier, Throwable thrown)133     public default void log(Supplier<String> msgSupplier, Throwable thrown) {
134         log(Level.DEBUG, msgSupplier, thrown);
135     }
136 
137     /**
138      * Logs a message with an optional list of parameters.
139      *
140      * @implSpec The default implementation for this method calls
141      * {@code this.log(Level.DEBUG, format, params);}
142      *
143      * @param format the string message format in
144      * {@link String#format(String, Object...)} or {@link
145      * java.text.MessageFormat} format, (or a key in the message
146      * catalog, if this logger is a {@link
147      * System.LoggerFinder#getLocalizedLogger(java.lang.String,
148      * java.util.ResourceBundle, java.lang.Module) localized logger});
149      * can be {@code null}.
150      * @param params an optional list of parameters to the message (may be
151      * none).
152      */
log(String format, Object... params)153     public default void log(String format, Object... params) {
154         log(Level.DEBUG, format, params);
155     }
156 }
157