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

Fix not showing early logged messages in the UI #1712

Merged
merged 2 commits into from
Sep 9, 2024
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
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);
leMaik marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
}
Loading