Skip to content

Commit

Permalink
+ no longer closing stdout/err fds
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed May 16, 2023
1 parent 36e6d9d commit e9c405d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.github.elf4j</groupId>
<artifactId>elf4j-engine</artifactId>
<version>10.0.1</version>
<version>10.0.2</version>
<packaging>jar</packaging>
<name>elf4j-engine</name>
<description>A stand-alone Java log engine implementing the ELF4J (Easy Logging Facade for Java) API</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

package elf4j.engine.service.writer;

import elf4j.engine.service.LogServiceManager;
import elf4j.engine.service.Stoppable;
import elf4j.engine.service.configuration.LogServiceConfiguration;
import lombok.NonNull;
import lombok.ToString;
Expand All @@ -38,19 +36,17 @@
*
*/
@ToString
public class BufferedStandardOutput implements StandardOutput, Stoppable {
public class BufferedStandardOutput implements StandardOutput {
private static final OutStreamType DEFAULT_OUT_STREAM_TYPE = OutStreamType.STDOUT;
@NonNull private final OutStreamType outStreamType;
private final BufferedOutStream bufferedOutStream = new BufferedOutStream();
private boolean closed;
private final BufferedStandardOutStreams bufferedStandardOutStreams = new BufferedStandardOutStreams();

/**
* @param outStreamType
* standard out stream type, stdout or stderr, default to stdout
*/
private BufferedStandardOutput(@NonNull OutStreamType outStreamType) {
this.outStreamType = outStreamType;
LogServiceManager.INSTANCE.registerStop(this);
}

/**
Expand All @@ -68,60 +64,43 @@ private BufferedStandardOutput(@NonNull OutStreamType outStreamType) {
@Override
public void write(byte[] bytes) {
if (this.outStreamType == OutStreamType.STDERR) {
bufferedOutStream.err(bytes);
bufferedStandardOutStreams.err(bytes);
} else {
bufferedOutStream.out(bytes);
bufferedStandardOutStreams.out(bytes);
}
}

@Override
public void stop() {
this.closed = true;
this.bufferedOutStream.close();
}

@Override
public boolean isStopped() {
return this.closed;
}

enum OutStreamType {
STDOUT,
STDERR
}

static class BufferedOutStream implements Closeable {
static class BufferedStandardOutStreams {
final OutputStream bufferedStdOut = new BufferedOutputStream(new FileOutputStream(FileDescriptor.out), 2048);
final OutputStream bufferedStdErr = new BufferedOutputStream(new FileOutputStream(FileDescriptor.err), 2048);

BufferedOutStream() {
}

@Override
public void close() {
try (OutputStream out = bufferedStdOut; OutputStream err = bufferedStdErr) {
out.flush();
err.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
BufferedStandardOutStreams() {
}

synchronized void err(byte[] bytes) {
try {
bufferedStdErr.write(bytes);
bufferedStdErr.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
void err(byte[] bytes) {
synchronized (System.err) {
try {
bufferedStdErr.write(bytes);
bufferedStdErr.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

synchronized void out(byte[] bytes) {
try {
bufferedStdOut.write(bytes);
bufferedStdOut.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
void out(byte[] bytes) {
synchronized (System.out) {
try {
bufferedStdOut.write(bytes);
bufferedStdOut.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}
Expand Down

0 comments on commit e9c405d

Please sign in to comment.