mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +01:00
Flush the log stream whenever there are records to flush.
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
package org.briarproject.briar.logging;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.logging.Formatter;
|
||||||
|
import java.util.logging.LogRecord;
|
||||||
|
import java.util.logging.StreamHandler;
|
||||||
|
|
||||||
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
|
|
||||||
|
class FlushingStreamHandler extends StreamHandler {
|
||||||
|
|
||||||
|
private static final int FLUSH_DELAY_MS = 5_000;
|
||||||
|
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
|
private final Executor ioExecutor;
|
||||||
|
private final AtomicBoolean flushScheduled = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
FlushingStreamHandler(ScheduledExecutorService scheduler,
|
||||||
|
Executor ioExecutor, OutputStream out, Formatter formatter) {
|
||||||
|
super(out, formatter);
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
this.ioExecutor = ioExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(LogRecord record) {
|
||||||
|
super.publish(record);
|
||||||
|
if (!flushScheduled.getAndSet(true)) {
|
||||||
|
scheduler.schedule(this::scheduledFlush,
|
||||||
|
FLUSH_DELAY_MS, MILLISECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduledFlush() {
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
flushScheduled.set(false);
|
||||||
|
flush();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,9 +39,8 @@ import javax.annotation.concurrent.ThreadSafe;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
import static java.util.Collections.emptyList;
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
||||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
@@ -50,11 +49,10 @@ class PersistentLogManagerImpl implements PersistentLogManager,
|
|||||||
OpenDatabaseHook {
|
OpenDatabaseHook {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(PersistentLogManagerImpl.class.getName());
|
getLogger(PersistentLogManagerImpl.class.getName());
|
||||||
|
|
||||||
private static final String LOG_FILE = "briar.log";
|
private static final String LOG_FILE = "briar.log";
|
||||||
private static final String OLD_LOG_FILE = "briar.log.old";
|
private static final String OLD_LOG_FILE = "briar.log.old";
|
||||||
private static final long FLUSH_INTERVAL_MS = MINUTES.toMillis(5);
|
|
||||||
|
|
||||||
private final ScheduledExecutorService scheduler;
|
private final ScheduledExecutorService scheduler;
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
@@ -116,15 +114,11 @@ class PersistentLogManagerImpl implements PersistentLogManager,
|
|||||||
OutputStream out = new FileOutputStream(logFile);
|
OutputStream out = new FileOutputStream(logFile);
|
||||||
StreamWriter writer =
|
StreamWriter writer =
|
||||||
streamWriterFactory.createLogStreamWriter(out, logKey);
|
streamWriterFactory.createLogStreamWriter(out, logKey);
|
||||||
StreamHandler handler =
|
StreamHandler handler = new FlushingStreamHandler(scheduler,
|
||||||
new StreamHandler(writer.getOutputStream(), formatter);
|
ioExecutor, writer.getOutputStream(), formatter);
|
||||||
// Flush the log periodically in case we're killed without getting
|
|
||||||
// the chance to run shutdown hooks
|
|
||||||
scheduler.scheduleWithFixedDelay(() ->
|
|
||||||
ioExecutor.execute(handler::flush),
|
|
||||||
FLUSH_INTERVAL_MS, FLUSH_INTERVAL_MS, MILLISECONDS);
|
|
||||||
// Flush the log and terminate the stream at shutdown
|
// Flush the log and terminate the stream at shutdown
|
||||||
shutdownManager.addShutdownHook(() -> {
|
shutdownManager.addShutdownHook(() -> {
|
||||||
|
LOG.info("Shutting down");
|
||||||
handler.flush();
|
handler.flush();
|
||||||
try {
|
try {
|
||||||
writer.sendEndOfStream();
|
writer.sendEndOfStream();
|
||||||
|
|||||||
Reference in New Issue
Block a user