Skip to content

Commit

Permalink
some convenience methods for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Aug 12, 2024
1 parent 768be0e commit fb945a6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/main/java/com/cleanroommc/groovyscript/api/GroovyLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.function.Supplier;

/**
* A interface for the GroovyScript logger. The log is separate to Minecraft's normal and debug log.
* An interface for the GroovyScript logger. The log is separate to Minecraft's normal and debug log.
* The generated log file can be found at "[Minecraft instance]/groovy.log".
* All logging methods format its content similarly to how C does it.
* Curly braces in the msg parameter get replaced with the given arguments.
Expand Down Expand Up @@ -269,6 +269,19 @@ interface Msg {
*/
Msg add(boolean condition, String msg, Object... args);

/**
* Adds a sub message to this message with exactly one parameter, but only if the given condition is true. The arg {@link Supplier}
* is invoked if the condition is true.
*
* @param condition sub message will only be added if this is true
* @param msg sub message
* @param arg sub message argument
* @return this
*/
default Msg add(boolean condition, String msg, Supplier<Object> arg) {
return add(condition, msg, (Object) arg);
}

/**
* Adds a sub message to this message, but only if the given condition is true.
* For convenience.
Expand All @@ -290,8 +303,8 @@ interface Msg {
Msg add(boolean condition, Consumer<Msg> msgBuilder);

/**
* Adds an exception to the message. The exception will always be logged at last.
* The exception counts as a sub message. This message can only have one message at a time.
* Adds an exception to the message. The exception will always be logged at last. The exception counts as a sub message. This
* message can only have one message at a time.
*
* @param throwable exception.
* @return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public abstract class MetaClassImplMixin {
@Shadow
protected abstract Object doInvokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass);

@Shadow
protected MetaClassRegistry registry;

@Shadow
protected abstract Object invokeMissingMethod(Object instance, String methodName, Object[] arguments, RuntimeException original, boolean isCallToSuper);

Expand All @@ -47,6 +44,8 @@ public abstract class MetaClassImplMixin {
@Final
private MetaMethod[] additionalMetaMethods;

@Shadow protected MetaClassRegistry registry;

@Inject(method = "<init>(Ljava/lang/Class;[Lgroovy/lang/MetaMethod;)V", at = @At("TAIL"))
public void removeBlacklistedAdditional(Class<?> theClass, MetaMethod[] add, CallbackInfo ci) {
if (additionalMetaMethods.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public Path getPath() {
}

/**
* Logs a info msg to the groovy log AND Minecraft's log
* Logs an info msg to the groovy log AND Minecraft's log
*
* @param msg message
* @param args arguments
Expand Down Expand Up @@ -347,6 +347,13 @@ public Msg add(String msg, Object... data) {
@Override
public Msg add(boolean condition, String msg, Object... args) {
if (condition) {
if (args != null && args.length > 0) {
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof Supplier<?> s) {
args[i] = s.get();
}
}
}
return add(msg, args);
}
return this;
Expand Down

0 comments on commit fb945a6

Please sign in to comment.