-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
LogInterceptor.java
62 lines (52 loc) · 1.45 KB
/
LogInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.denizenscript.denizencore.utilities.debugging;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.events.core.ConsoleOutputScriptEvent;
import java.io.PrintStream;
/**
* 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);
}
@Override
public void print(String s) {
if (antiLoop) {
super.print(s);
return;
}
antiLoop = true;
ConsoleOutputScriptEvent event = ConsoleOutputScriptEvent.instance;
event.message = DenizenCore.getImplementation().cleanseLogString(s);
event.fire();
if (!event.cancelled) {
super.print(s);
}
antiLoop = false;
}
@Override
public void print(Object obj) {
print(String.valueOf(obj));
}
@Override
public void print(char[] buf) {
print(new String(buf));
}
private boolean antiLoop = false;
public void redirectOutput() {
if (redirected) {
return;
}
standardOut = System.out;
System.setOut(this);
}
public void standardOutput() {
if (!redirected) {
return;
}
System.setOut(standardOut);
}
}