Kaynağa Gözat

Improve main-Class and add FormattedSnuviLogger for nicer output.

Niklas Seyfarth 6 yıl önce
ebeveyn
işleme
f4ad45b299

+ 1 - 0
error.snuvi

@@ -0,0 +1 @@
+assert(false);

+ 48 - 0
src/main/java/me/hammerle/snuviscript/FormattedSnuviLogger.java

@@ -0,0 +1,48 @@
+package me.hammerle.snuviscript;
+
+import java.util.Date;
+import me.hammerle.code.ISnuviLogger;
+import me.hammerle.code.Script;
+
+public class FormattedSnuviLogger implements ISnuviLogger {
+
+	private final Date date = new Date();
+
+	public FormattedSnuviLogger() {
+	}
+
+	@Override
+	public void printException(Exception thrown, String function, Script script, int line) {
+		System.err.println(createMessage("ERROR", codeLocation(script.getName(), function, line), thrown));
+	}
+
+	@Override
+	public void printException(Exception thrown, String function, String scriptName, int line) {
+		System.err.println(createMessage("ERROR", codeLocation(scriptName, function, line), thrown));
+	}
+
+	private static String codeLocation(String scriptName, String function, int line) {
+		return scriptName + ":" + function + ":" + line;
+	}
+
+	@Override
+	public void printWarning(String message) {
+		System.err.println(createMessage("WARNING", message));
+	}
+
+	@Override
+	public void printInfo(String message) {
+		System.out.println(createMessage("INFO", message));
+	}
+
+	private String createMessage(String level, String message, Exception thrown) {
+		return String.format("%1$s%n\t%2$s", createMessage(level, message), thrown);
+	}
+
+	private String createMessage(String level, String message) {
+		date.setTime(System.currentTimeMillis());
+		return String.format("[%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS] [SnuviScript|%2$s]: %3$s",
+							 date, level, message);
+	}
+
+}

+ 2 - 2
src/main/java/me/hammerle/snuviscript/SnuviTerminalLogger.java → src/main/java/me/hammerle/snuviscript/SimpleSnuviLogger.java

@@ -3,9 +3,9 @@ package me.hammerle.snuviscript;
 import me.hammerle.code.ISnuviLogger;
 import me.hammerle.code.Script;
 
-public class SnuviTerminalLogger implements ISnuviLogger {
+public class SimpleSnuviLogger implements ISnuviLogger {
 
-	public SnuviTerminalLogger() {
+	public SimpleSnuviLogger() {
 	}
 
 	@Override

+ 22 - 24
src/main/java/me/hammerle/snuviscript/SnuviScript.java

@@ -13,10 +13,7 @@ import me.hammerle.exceptions.PreScriptException;
 
 public class SnuviScript {
 
-	private static ISnuviLogger LOGGER;
-	private static SnuviParser PARSER;
-
-	public static void main(String[] args) throws IOException {
+	public static void main(String[] args) {
 		Executable executable;
 		switch (args.length) {
 			case 0:
@@ -30,23 +27,10 @@ public class SnuviScript {
 				System.err.println("Illegal number of arguments.");
 				return;
 		}
-		createLogger();
-		createParser();
-		registerTerminalMethods();
-		executeScript(executable);
-	}
-
-	private static void createLogger() {
-		LOGGER = new SnuviTerminalLogger();
-	}
-
-	private static void createParser() {
-		PARSER = new SnuviParser(LOGGER, new SnuviSimpleScheduler());
-	}
-
-	private static void registerTerminalMethods() {
-		PARSER.registerConsumer("print", (o, sc) -> System.out.println(ScriptUtils.connect(o, " ", 0)));
-		PARSER.registerFunction("read", (o, sc) -> readLine());
+		ISnuviLogger logger = createLogger();
+		SnuviParser  parser = createParser(logger);
+		registerTerminalMethods(logger, parser);
+		executeScript(executable, logger, parser);
 	}
 
 	private static String readLine() throws UncheckedIOException {
@@ -69,11 +53,25 @@ public class SnuviScript {
 		return new Executable(name, code);
 	}
 
-	private static void executeScript(Executable executable) {
+	private static ISnuviLogger createLogger() {
+		return new FormattedSnuviLogger();
+	}
+
+	private static SnuviParser createParser(ISnuviLogger logger) {
+		return new SnuviParser(logger, new SnuviSimpleScheduler());
+	}
+
+	private static void registerTerminalMethods(ISnuviLogger logger, SnuviParser parser) {
+		parser.registerConsumer("print", (o, sc) -> System.out.println(ScriptUtils.connect(o, " ", 0)));
+		parser.registerFunction("read", (o, sc) -> readLine());
+		parser.registerConsumer("log", (o, sc) -> logger.printInfo(ScriptUtils.connect(o, " ", 0)));
+	}
+
+	private static void executeScript(Executable executable, ISnuviLogger logger, SnuviParser parser) {
 		try {
-			PARSER.startScript(executable.getName(), executable.getCode());
+			parser.startScript(executable.getName(), executable.getCode());
 		} catch (PreScriptException ex) {
-			LOGGER.printException(ex, "none", executable.getName(), ex.getLine());
+			logger.printException(ex, "none", executable.getName(), ex.getLine());
 		}
 	}
 }