Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move setting of initPending to false only after initializing the jdk log manager, fix #646 #647

Merged
merged 2 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
* https://github.com/danfickle/openhtmltopdf/wiki/Logging
*/
public class JDKXRLogger implements XRLogger {
private boolean initPending = true;
private volatile boolean initPending = true;

// Keep a map of Loggers so they are not garbage collected
// which makes them lose their settings we have applied.
private Map<String, Logger> loggers;
private volatile Map<String, Logger> loggers;

private final boolean useParent;
private final Level level;
Expand Down Expand Up @@ -107,10 +107,11 @@ private void init(boolean useParent, Level level, Handler handler, Formatter for
if (!initPending) {
return;
}
//now change this immediately, in case something fails
initPending = false;

initializeJDKLogManager(useParent, level, handler, formatter);
try {
initializeJDKLogManager(useParent, level, handler, formatter);
} finally {
initPending = false;
}
}

private void initializeJDKLogManager(boolean useParent, Level level, Handler handler, Formatter formatter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ private static String registerLoggerByName(final String loggerName) {
return loggerName;
}

private static boolean initPending = true;
private static XRLogger loggerImpl;
private static volatile boolean initPending = true;
private static volatile XRLogger loggerImpl;

private static volatile Boolean loggingEnabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;

public class XRLogTest {
Expand All @@ -14,5 +16,31 @@ public void testDisableLogBeforeFirstLog() {
XRLog.log(Level.INFO, LogMessageId.LogMessageId0Param.LOAD_UNABLE_TO_DISABLE_XML_EXTERNAL_ENTITIES);
Assert.assertFalse(XRLog.isLoggingEnabled());
}

/**
* See issue https://github.com/danfickle/openhtmltopdf/issues/646
*/
@Test
public void testConcurrentInitLog() throws InterruptedException {
int p = 20;
CountDownLatch latch = new CountDownLatch(p);
CountDownLatch end = new CountDownLatch(p);
AtomicInteger counter = new AtomicInteger();
for (int i = 0; i < p;i++) {
new Thread(() -> {
latch.countDown();
try {
latch.await();
XRLog.log(Level.SEVERE, LogMessageId.LogMessageId0Param.CASCADE_IS_ABSOLUTE_CSS_UNKNOWN_GIVEN);
counter.incrementAndGet();
end.countDown();
} catch (Throwable e) {
end.countDown();
}
}).start();
}
end.await();
Assert.assertEquals(p, counter.get()); //we expect 0 NPE -> counter = 20
}

}