Skip to content

Commit

Permalink
Merge pull request AdoptOpenJDK#659 from AdoptOpenJDK/fixTeeStreamDea…
Browse files Browse the repository at this point in the history
…dlock

remember original system out in SystemOutLogger
  • Loading branch information
sclassen authored Jun 10, 2020
2 parents 831c349 + ae243cc commit 3f620eb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@
public class LoggerFactory {

private static final String FACTORY_CLASS = "net.sourceforge.jnlp.util.logging.OutputControllerLoggerFactory";
private static final LoggerFactoryImpl factory = initFactory();
private static final LoggerFactoryImpl factory;

private static LoggerFactoryImpl initFactory() {
static {
Exception ex = null;
LoggerFactoryImpl loggerFactory;
try {
final Class<?> factoryClass = LoggerFactory.class.getClassLoader().loadClass(FACTORY_CLASS);
return (LoggerFactoryImpl) factoryClass.newInstance();
loggerFactory = (LoggerFactoryImpl) factoryClass.newInstance();
} catch (Exception e) {
return new SystemOutLoggerFactory();
ex = e;
loggerFactory = new SystemOutLoggerFactory();
}
factory = loggerFactory;

// one can only get a logger after the factory has been set.
// therefore we postpone all the logging to after this comment

final Logger LOG = factory.getLogger(LoggerFactory.class);
if (ex != null) {
LOG.error("Falling back to SystemOutLogger", ex);
} else {
LOG.debug("init logger factory to {}", factory);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.adoptopenjdk.icedteaweb.logging.LoggerFactory.LoggerFactoryImpl;

import java.io.PrintStream;
import java.util.Date;

import static net.adoptopenjdk.icedteaweb.OutputUtils.exceptionToString;
Expand All @@ -11,6 +12,16 @@
*/
class SystemOutLoggerFactory implements LoggerFactoryImpl {

private static final PrintStream out;

static {
final PrintStream tmp = System.out;
if (tmp.getClass().getSimpleName().contains("TeeOutputStream")) {
throw new RuntimeException("System.out is already a tee stream");
}
out = tmp;
}

@Override
public Logger getLogger(final Class<?> forClass) {
return new SystemOutLogger(forClass);
Expand Down Expand Up @@ -96,7 +107,7 @@ private void log(final String level, final String msg, final Throwable t) {
final String header = new Date().toString() + " " + level + " " + forClass + ":";
final String line = header + separator + msg + (t != null ? separator + exceptionToString(t) : "");

System.out.println(line);
out.println(line);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* Behaves like the 'tee' command, sends output to both actual std stream and a
* log
*/
public final class TeeOutputStream extends PrintStream implements SingleStreamLogger{
public final class TeeOutputStream extends PrintStream implements SingleStreamLogger {

private static final String LINE_SEPARATOR = PlainTextFormatter.getLineSeparator();

Expand Down Expand Up @@ -85,18 +85,33 @@ public synchronized void write(byte[] b, int off, int len) {
if (len == 0) {
return;
}
appendByteArray(b, off, len);

byteArrayOutputStream.write(b, off, len);
flushLogAtEndOfLine();

super.write(b, off, len);
}

@Override
public synchronized void write(int b) {
appendByte(b);
byteArrayOutputStream.write(b);
flushLogAtEndOfLine();

super.write(b);
}

private void flushLog() {
String s = byteArrayOutputStream.toString();
flushLog(byteArrayOutputStream.toString());
}

private void flushLogAtEndOfLine() {
final String s = byteArrayOutputStream.toString();
if (s.endsWith(LINE_SEPARATOR)) {
flushLog(s);
}
}

private void flushLog(String s) {
if (s.length() > 0) {
log(s);
byteArrayOutputStream.reset();
Expand All @@ -105,30 +120,14 @@ private void flushLog() {

@Override
public void log(String s) {
JavaMessage jm = new JavaMessage(new Header(getLevel(), true), s);
JavaMessage jm = new JavaMessage(new Header(getLevel(), true), s);
outputController.log(jm);
}

private boolean isError() {
return isError;
}

private void appendByte(int b) {
byteArrayOutputStream.write(b);
String s = byteArrayOutputStream.toString();
if (s.endsWith(LINE_SEPARATOR)) {
flushLog();
}
}

private void appendByteArray(byte[] b, int off, int len) {
byteArrayOutputStream.write(b, off, len);
String s = new String(b, off, len);
if (s.endsWith(LINE_SEPARATOR)) {
flushLog();
}
}

private OutputControllerLevel getLevel() {
if (isError()) {
return OutputControllerLevel.ERROR_ALL;
Expand Down

0 comments on commit 3f620eb

Please sign in to comment.