Skip to content

Commit

Permalink
add redirect_logging as a core feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 22, 2015
1 parent 9c1a924 commit 3f90ebb
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/aufdemrand/denizencore/DenizenCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.aufdemrand.denizencore.scripts.ScriptHelper;
import net.aufdemrand.denizencore.scripts.commands.CommandRegistry;
import net.aufdemrand.denizencore.scripts.queues.ScriptEngine;
import net.aufdemrand.denizencore.utilities.debugging.LogInterceptor;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.scheduling.Schedulable;

Expand Down Expand Up @@ -36,6 +37,8 @@ public static ScriptEngine getScriptEngine() {
return scriptEngine;
}

public static LogInterceptor logInterceptor = new LogInterceptor();

static {
String version = "UNKNOWN";
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ public interface DenizenImplementation {

public abstract String getLastEntryFromFlag(String flag);

public TagContext getTagContext(ScriptEntry entry);
public abstract TagContext getTagContext(ScriptEntry entry);

public int getTagTimeout();
public abstract int getTagTimeout();

public abstract boolean allowConsoleRedirection();

public abstract String cleanseLogString(String str);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;

public abstract class CommandRegistry implements dRegistry {

public CommandRegistry() {
}

Expand All @@ -19,7 +20,7 @@ public CommandRegistry() {
@Override
public boolean register(String commandName, RegistrationableInstance commandInstance) {
this.instances.put(CoreUtilities.toLowerCase(commandName), (AbstractCommand) commandInstance);
this.classes.put(((AbstractCommand) commandInstance).getClass(), commandName.toUpperCase());
this.classes.put(((AbstractCommand) commandInstance).getClass(), CoreUtilities.toLowerCase(commandName));
return true;
}

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/net/aufdemrand/denizencore/tags/core/UtilTags.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.aufdemrand.denizencore.tags.core;

import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.queues.ScriptQueue;
import net.aufdemrand.denizencore.tags.Attribute;
Expand Down Expand Up @@ -301,4 +303,33 @@ else if (attribute.startsWith("format")
}
}
}


public static void adjustSystem(Mechanism mechanism) {
Element value = mechanism.getValue();

// <--[mechanism]
// @object server
// @name redirect_logging
// @input Element(Boolean)
// @description
// Tells the server to redirect logging to a world event or not.
// Note that this redirects *all console output* not just Denizen output.
// Note: don't enable /denizen debug -e while this is active.
// @tags
// None
// -->
if (mechanism.matches("redirect_logging") && mechanism.hasValue()) {
if (!DenizenCore.getImplementation().allowConsoleRedirection()) {
dB.echoError("Console redirection disabled by administrator.");
return;
}
if (mechanism.getValue().asBoolean()) {
DenizenCore.logInterceptor.redirectOutput();
}
else {
DenizenCore.logInterceptor.standardOutput();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.aufdemrand.denizencore.utilities.debugging;

import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.dObject;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
* Intercepts system.out operations for the sake of blocking messages at request.
* Disabled by default in config.yml
*/
public class LogInterceptor extends PrintStream {
boolean redirected = false;
public PrintStream standardOut;

public LogInterceptor() {
super(System.out, true);
}

// <--[event]
// @Events
// console output
//
// @Warning Disable debug on this event or you'll get an infinite loop!
//
// @Triggers when any message is printed to console. (Requires <@link mechanism system.redirect_logging> be set true.)
// @Context
// <context.message> returns the messsage that is being printed to console.
//
// @Determine
// "CANCELLED" to disable the output.
//
// -->
@Override
public void print(String s) {
HashMap<String, dObject> context = new HashMap<String, dObject>();
context.put("message", new Element(DenizenCore.getImplementation().cleanseLogString(s)));
List<String> Determinations = OldEventManager.doEvents(Arrays.asList("console output"), // TODO: ScriptEvent
DenizenCore.getImplementation().getEmptyScriptEntryData(), context);
for (String str : Determinations) {
if (str.equalsIgnoreCase("cancelled")) {
return;
}
}
super.print(s);
}

public void redirectOutput() {
if (redirected) {
return;
}
standardOut = System.out;
System.setOut(this);
}

public void standardOutput() {
if (!redirected) {
return;
}
System.setOut(standardOut);
}
}

0 comments on commit 3f90ebb

Please sign in to comment.