diff --git a/Project-01/uap25-pro01-tram/build.sh b/Project-01/uap25-pro01-tram/build.sh index ea10227..9b0785f 100644 --- a/Project-01/uap25-pro01-tram/build.sh +++ b/Project-01/uap25-pro01-tram/build.sh @@ -25,13 +25,18 @@ if [ -d "$LIB" ]; then done fi -# 3. Create manifest +# 3. Copy resources (including log4j2.xml) into the JAR +if [ -d "resources" ]; then + cp -r resources/* "$TMP/" +fi + +# 4. Create manifest echo "Main-Class: $MAIN_CLASS" > "$TMP/MANIFEST.MF" -# 4. Package into single jar +# 5. Package into single jar jar cfm "$OUT/$JAR_NAME" "$TMP/MANIFEST.MF" -C "$TMP" . -# 5. Clean temporary files +# 6. Clean temporary files rm -rf "$TMP" echo "Built: $OUT/$JAR_NAME" diff --git a/Project-01/uap25-pro01-tram/jar/tram.jar b/Project-01/uap25-pro01-tram/jar/tram.jar index 9261f40..749f9bd 100644 Binary files a/Project-01/uap25-pro01-tram/jar/tram.jar and b/Project-01/uap25-pro01-tram/jar/tram.jar differ diff --git a/Project-01/uap25-pro01-tram/resources/log4j2.xml b/Project-01/uap25-pro01-tram/resources/log4j2.xml new file mode 100644 index 0000000..c20b519 --- /dev/null +++ b/Project-01/uap25-pro01-tram/resources/log4j2.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/AbstractMachine.java b/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/AbstractMachine.java index e68bcd2..c75ac35 100644 --- a/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/AbstractMachine.java +++ b/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/AbstractMachine.java @@ -29,10 +29,10 @@ public class AbstractMachine { while (!isHalted()) { executeStep(); } - confStr(); } - // Executes single step and returns configuration + + // Executes a single step and returns configuration public void executeStep() { try { Instruction inst = this.prog.get(this.PC); @@ -212,16 +212,8 @@ public class AbstractMachine { // Returns the machine's current configuration as string public String confStr() { - StringBuilder sb = new StringBuilder(); - - sb.append("TOP=").append(this.TOP) - .append(" PC=").append(this.PC) - .append(" PP=").append(this.PP) - .append(" FP=").append(this.FP) - .append("\nSTACK: ") - .append(stackStr()); - - return sb.toString(); + String format = "TOP=%3d | PC=%3d | PP=%3d | FP=%3d | STACK=%s"; + return String.format(format, this.TOP, this.PC, this.PP, this.FP, stackStr()); } // Returns the machine's current stack as string diff --git a/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/LoggedMachine.java b/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/LoggedMachine.java index c6de517..06d2077 100644 --- a/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/LoggedMachine.java +++ b/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/LoggedMachine.java @@ -5,23 +5,28 @@ import org.apache.logging.log4j.Logger; public class LoggedMachine extends AbstractMachine { private static final Logger logger = LogManager.getLogger(LoggedMachine.class); - private int stepCounter = 0; + private int stepCounter = -1; public LoggedMachine(Instruction[] prog) { super(prog); } public LoggedMachine(Instruction[] prog, Integer[] stack) { super(prog, stack); } @Override public void execute() { - logger.info("Executing..."); - logger.debug("0: {}", confStr()); - while (!isHalted()) executeStep(); - logger.info("Finished with STACK: {}", stackStr()); + logger.trace(stepStr()); + while (!isHalted()) { + executeStep(); + } } @Override public void executeStep() { - int step = stepCounter++; super.executeStep(); - logger.debug("{}: {}", step, confStr()); + logger.trace(stepStr()); + } + + public String stepStr() { + int step = ++stepCounter; + String format = "Step %3d | TOP=%3d | PC=%3d | PP=%3d | FP=%3d | STACK=%s"; + return String.format(format, step, this.TOP, this.PC, this.PP, this.FP, stackStr()); } } diff --git a/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/Main.java b/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/Main.java index 35a810c..c75b52c 100644 --- a/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/Main.java +++ b/Project-01/uap25-pro01-tram/src/de/unitrier/st/uap/w25/tram/Main.java @@ -1,6 +1,8 @@ package de.unitrier.st.uap.w25.tram; import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.Configurator; import java.nio.file.Files; import java.nio.file.Path; @@ -8,6 +10,8 @@ import java.util.ArrayList; import java.util.List; public final class Main { + private static final Logger log = LogManager.getLogger(Main.class); + private Main() {} static void main(String[] argv) { @@ -29,7 +33,7 @@ public final class Main { if (i + 1 < argv.length) { stackArg = argv[++i]; } else { - System.err.println("Error: missing stack argument after --stack"); + System.err.println("Missing stack argument after --stack"); System.exit(2); } } @@ -43,26 +47,23 @@ public final class Main { // Verify .tram file Path tramFile = Path.of(tramPath); if (!Files.exists(tramFile)) { - System.err.println("Error: file not found -> " + tramPath); + System.err.println("File not found: " + tramPath); System.exit(4); } - // Configure console logging only - if (debug) { - Configurator.setRootLevel(Level.DEBUG); - } else { - Configurator.setRootLevel(Level.INFO); - } - // Parse optional stack Integer[] stack = parseStack(stackArg); - // Load TRAM code and execute + // Load TRAM code Instruction[] code = Assembler.readTRAMCode(tramPath); - LoggedMachine tram = new LoggedMachine(code, stack); - tram.execute(); - System.out.println("Execution complete."); + // Execute and print console output + Logger logger = LogManager.getLogger(Main.class); + AbstractMachine tram = debug ? new LoggedMachine(code, stack) : new AbstractMachine(code, stack); + logger.trace("Start execution..."); + tram.execute(); + logger.trace("Finished execution."); + logger.trace("Final STACK: {}", tram.stackStr()); } // Parse "[1,2,3]" or "1,2,3"