Browse Source

Added a logger to ScriptingEngine to catch a few extra errors

Jared 5 years ago
parent
commit
71844ec536
No account linked to committer's email address

+ 34
- 0
JavaIntegration/src/main/java/org/openzen/zencode/java/EmptyLogger.java View File

@@ -0,0 +1,34 @@
1
+package org.openzen.zencode.java;
2
+
3
+public class EmptyLogger implements IZSLogger {
4
+    
5
+    @Override
6
+    public void info(String message) {
7
+    
8
+    }
9
+    
10
+    @Override
11
+    public void debug(String message) {
12
+    
13
+    }
14
+    
15
+    @Override
16
+    public void warning(String message) {
17
+    
18
+    }
19
+    
20
+    @Override
21
+    public void error(String message) {
22
+    
23
+    }
24
+    
25
+    @Override
26
+    public void throwingErr(String message, Throwable throwable) {
27
+    
28
+    }
29
+    
30
+    @Override
31
+    public void throwingWarn(String message, Throwable throwable) {
32
+    
33
+    }
34
+}

+ 16
- 0
JavaIntegration/src/main/java/org/openzen/zencode/java/IZSLogger.java View File

@@ -0,0 +1,16 @@
1
+package org.openzen.zencode.java;
2
+
3
+public interface IZSLogger {
4
+    
5
+    void info(String message);
6
+    
7
+    void debug(String message);
8
+    
9
+    void warning(String message);
10
+    
11
+    void error(String message);
12
+    
13
+    void throwingErr(String message, Throwable throwable);
14
+    
15
+    void throwingWarn(String message, Throwable throwable);
16
+}

+ 16
- 2
JavaIntegration/src/main/java/org/openzen/zencode/java/ScriptingEngine.java View File

@@ -18,6 +18,7 @@ import org.openzen.zenscript.parser.*;
18 18
 import org.openzen.zenscript.validator.*;
19 19
 
20 20
 import java.io.*;
21
+import java.lang.reflect.InvocationTargetException;
21 22
 import java.util.*;
22 23
 import java.util.function.*;
23 24
 
@@ -36,6 +37,8 @@ public class ScriptingEngine {
36 37
 	
37 38
 	public boolean debug = false;
38 39
 	
40
+	public IZSLogger logger;
41
+	
39 42
 	public ScriptingEngine() {
40 43
 		space = new ModuleSpace(registry, new ArrayList<>(), StorageType.getStandard());
41 44
 		
@@ -47,6 +50,12 @@ public class ScriptingEngine {
47 50
 		} catch (IOException | CompileException | ParseException ex) {
48 51
 			throw new RuntimeException(ex);
49 52
 		}
53
+		this.logger = new EmptyLogger();
54
+    }
55
+    
56
+    public ScriptingEngine(IZSLogger logger) {
57
+        this();
58
+        this.logger = logger;
50 59
     }
51 60
 	
52 61
 	public JavaNativeModule createNativeModule(String name, String basePackage, JavaNativeModule... dependencies) {
@@ -145,8 +154,13 @@ public class ScriptingEngine {
145 154
 			runUnit.add(compiler.compile(compiled.name, compiled, javaSpace));
146 155
 		if (debug)
147 156
 			runUnit.dump(new File("classes"));
148
-		runUnit.run(arguments, parentClassLoader);
149
-	}
157
+        
158
+        try {
159
+            runUnit.run(arguments, parentClassLoader);
160
+        } catch(ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
161
+            logger.throwingErr(e.getCause().getMessage(), e.getCause());
162
+        }
163
+    }
150 164
 	
151 165
 	public List<JavaNativeModule> getNativeModules() {
152 166
 		return Collections.unmodifiableList(this.nativeModules);

Loading…
Cancel
Save