1
1
/*
2
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
32
32
import java .lang .System .Logger ;
33
33
import java .lang .ref .WeakReference ;
34
34
import java .util .Objects ;
35
+ import java .util .function .BooleanSupplier ;
36
+
37
+ import jdk .internal .logger .LoggerFinderLoader .TemporaryLoggerFinder ;
35
38
import jdk .internal .misc .VM ;
36
39
import sun .util .logging .PlatformLogger ;
37
40
@@ -110,6 +113,9 @@ static final class LazyLoggerAccessor implements LoggerAccessor {
110
113
// We need to pass the actual caller module when creating the logger.
111
114
private final WeakReference <Module > moduleRef ;
112
115
116
+ // whether this is the loading thread, can be null
117
+ private final BooleanSupplier isLoadingThread ;
118
+
113
119
// The name of the logger that will be created lazyly
114
120
final String name ;
115
121
// The plain logger SPI object - null until it is accessed for the
@@ -122,16 +128,24 @@ static final class LazyLoggerAccessor implements LoggerAccessor {
122
128
private LazyLoggerAccessor (String name ,
123
129
LazyLoggerFactories <? extends Logger > factories ,
124
130
Module module ) {
131
+ this (name , factories , module , null );
132
+ }
133
+
134
+ private LazyLoggerAccessor (String name ,
135
+ LazyLoggerFactories <? extends Logger > factories ,
136
+ Module module , BooleanSupplier isLoading ) {
137
+
125
138
this (Objects .requireNonNull (name ), Objects .requireNonNull (factories ),
126
- Objects .requireNonNull (module ), null );
139
+ Objects .requireNonNull (module ), isLoading , null );
127
140
}
128
141
129
142
private LazyLoggerAccessor (String name ,
130
143
LazyLoggerFactories <? extends Logger > factories ,
131
- Module module , Void unused ) {
144
+ Module module , BooleanSupplier isLoading , Void unused ) {
132
145
this .name = name ;
133
146
this .factories = factories ;
134
147
this .moduleRef = new WeakReference <>(module );
148
+ this .isLoadingThread = isLoading ;
135
149
}
136
150
137
151
/**
@@ -162,7 +176,7 @@ public Logger wrapped() {
162
176
// BootstrapLogger has the logic to decide whether to invoke the
163
177
// SPI or use a temporary (BootstrapLogger or SimpleConsoleLogger)
164
178
// logger.
165
- wrapped = BootstrapLogger .getLogger (this );
179
+ wrapped = BootstrapLogger .getLogger (this , isLoadingThread );
166
180
synchronized (this ) {
167
181
// if w has already been in between, simply drop 'wrapped'.
168
182
setWrappedIfNotSet (wrapped );
@@ -194,7 +208,7 @@ public PlatformLogger.Bridge platform() {
194
208
// BootstrapLogger has the logic to decide whether to invoke the
195
209
// SPI or use a temporary (BootstrapLogger or SimpleConsoleLogger)
196
210
// logger.
197
- final Logger wrapped = BootstrapLogger .getLogger (this );
211
+ final Logger wrapped = BootstrapLogger .getLogger (this , isLoadingThread );
198
212
synchronized (this ) {
199
213
// if w has already been set, simply drop 'wrapped'.
200
214
setWrappedIfNotSet (wrapped );
@@ -282,10 +296,10 @@ Logger createLogger() {
282
296
* Creates a new lazy logger accessor for the named logger. The given
283
297
* factories will be use when it becomes necessary to actually create
284
298
* the logger.
285
- * @param <T> An interface that extends {@link Logger}.
286
299
* @param name The logger name.
287
300
* @param factories The factories that should be used to create the
288
301
* wrapped logger.
302
+ * @param module The module for which the logger is being created
289
303
* @return A new LazyLoggerAccessor.
290
304
*/
291
305
public static LazyLoggerAccessor makeAccessor (String name ,
@@ -339,6 +353,7 @@ private static LoggerFinder accessLoggerFinder() {
339
353
prov = sm == null ? LoggerFinder .getLoggerFinder () :
340
354
AccessController .doPrivileged (
341
355
(PrivilegedAction <LoggerFinder >)LoggerFinder ::getLoggerFinder );
356
+ if (prov instanceof TemporaryLoggerFinder ) return prov ;
342
357
provider = prov ;
343
358
}
344
359
return prov ;
@@ -358,7 +373,6 @@ public Logger apply(String name, Module module) {
358
373
new LazyLoggerFactories <>(loggerSupplier );
359
374
360
375
361
-
362
376
// A concrete implementation of Logger that delegates to a System.Logger,
363
377
// but only creates the System.Logger instance lazily when it's used for
364
378
// the first time.
@@ -376,6 +390,11 @@ private JdkLazyLogger(LazyLoggerAccessor holder, Void unused) {
376
390
}
377
391
}
378
392
393
+ static Logger makeLazyLogger (String name , Module module , BooleanSupplier isLoading ) {
394
+ final LazyLoggerAccessor holder = new LazyLoggerAccessor (name , factories , module , isLoading );
395
+ return new JdkLazyLogger (holder , null );
396
+ }
397
+
379
398
/**
380
399
* Gets a logger from the LoggerFinder. Creates the actual concrete
381
400
* logger.
1 commit comments
openjdk-notifier[bot] commentedon Nov 9, 2023
Review
Issues