Skip to content

Commit

Permalink
Fix not showing early logged messages in the UI (#1712)
Browse files Browse the repository at this point in the history
* Buffer log events before a receiver is set, fixes #1702

* Always set a receiver if an unchecked exception was thrown when running chunky
  • Loading branch information
NotStirred authored Sep 9, 2024
1 parent 704cab0 commit 2e2e884
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
3 changes: 3 additions & 0 deletions chunky/src/java/se/llbit/chunky/main/Chunky.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import se.llbit.chunky.world.MaterialStore;
import se.llbit.json.JsonArray;
import se.llbit.json.JsonValue;
import se.llbit.log.ConsoleReceiver;
import se.llbit.log.Level;
import se.llbit.log.Log;
import se.llbit.log.Receiver;
Expand Down Expand Up @@ -237,6 +238,8 @@ public static void main(final String[] args) {
break;
}
} catch (Throwable t) {
// set receiver in case an exception was thrown before it was set in one of the start modes.
Log.setReceiver(ConsoleReceiver.INSTANCE, Level.INFO, Level.WARNING, Level.ERROR);
Log.error("Unchecked exception caused Chunky to close.", t);
exitCode = 2;
}
Expand Down
28 changes: 28 additions & 0 deletions lib/src/se/llbit/log/BufferingConsoleReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package se.llbit.log;

import java.util.ArrayList;
import java.util.List;

public class BufferingConsoleReceiver extends Receiver {
public static final BufferingConsoleReceiver INSTANCE = new BufferingConsoleReceiver();

private final List<Event> bufferedEvents = new ArrayList<>();

protected BufferingConsoleReceiver() {
}

@Override
public void logEvent(Level level, String message) {
this.bufferedEvents.add(new Event(level, message));
}

@Override
public boolean isBuffered() {
return true;
}

@Override
public List<Event> getBufferedEvents() {
return this.bufferedEvents;
}
}
10 changes: 9 additions & 1 deletion lib/src/se/llbit/log/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Log {
private static final int WARNING = Level.WARNING.ordinal();
private static final int ERROR = Level.ERROR.ordinal();
private static final Receiver[] receiver =
{ConsoleReceiver.INSTANCE, ConsoleReceiver.INSTANCE, ConsoleReceiver.INSTANCE};
{BufferingConsoleReceiver.INSTANCE, BufferingConsoleReceiver.INSTANCE, BufferingConsoleReceiver.INSTANCE};

static {
try {
Expand All @@ -51,7 +51,15 @@ public static void setReceiver(Receiver receiver, Level... levels) {
throw new IllegalArgumentException("No log level specified for receiver.");
}
for (Level level : levels) {
Receiver previousReceiver = Log.receiver[level.ordinal()];
Log.receiver[level.ordinal()] = receiver;

// flush the buffered receiver, if required.
if (previousReceiver.isBuffered()) {
previousReceiver.getBufferedEvents().stream()
.filter(event -> event.level == level)
.forEach(event -> receiver.logEvent(event.level, event.message));
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions lib/src/se/llbit/log/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.List;

abstract public class Receiver {
abstract public void logEvent(Level level, String message);
Expand All @@ -37,4 +39,21 @@ public void logEvent(Level level, Throwable thrown) {
logEvent(level, writer.toString());
}

public boolean isBuffered() {
return false;
}

public List<Event> getBufferedEvents() {
return Collections.emptyList();
}

public static class Event {
public final Level level;
public final String message;

public Event(Level level, String message) {
this.level = level;
this.message = message;
}
}
}

0 comments on commit 2e2e884

Please sign in to comment.