Finish log4j implementation

This commit is contained in:
Jan-Niclas Loosen
2025-10-28 15:32:34 +01:00
parent 3795ab9d29
commit f578691c25
6 changed files with 55 additions and 35 deletions

View File

@@ -25,13 +25,18 @@ if [ -d "$LIB" ]; then
done done
fi 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" 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" . jar cfm "$OUT/$JAR_NAME" "$TMP/MANIFEST.MF" -C "$TMP" .
# 5. Clean temporary files # 6. Clean temporary files
rm -rf "$TMP" rm -rf "$TMP"
echo "Built: $OUT/$JAR_NAME" echo "Built: $OUT/$JAR_NAME"

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="de.unitrier.st.uap.w25.tram" level="TRACE" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Root level="TRACE">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@@ -29,10 +29,10 @@ public class AbstractMachine {
while (!isHalted()) { while (!isHalted()) {
executeStep(); executeStep();
} }
confStr();
} }
// Executes single step and returns configuration
// Executes a single step and returns configuration
public void executeStep() { public void executeStep() {
try { try {
Instruction inst = this.prog.get(this.PC); Instruction inst = this.prog.get(this.PC);
@@ -212,16 +212,8 @@ public class AbstractMachine {
// Returns the machine's current configuration as string // Returns the machine's current configuration as string
public String confStr() { public String confStr() {
StringBuilder sb = new StringBuilder(); 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());
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();
} }
// Returns the machine's current stack as string // Returns the machine's current stack as string

View File

@@ -5,23 +5,28 @@ import org.apache.logging.log4j.Logger;
public class LoggedMachine extends AbstractMachine { public class LoggedMachine extends AbstractMachine {
private static final Logger logger = LogManager.getLogger(LoggedMachine.class); 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) { super(prog); }
public LoggedMachine(Instruction[] prog, Integer[] stack) { super(prog, stack); } public LoggedMachine(Instruction[] prog, Integer[] stack) { super(prog, stack); }
@Override @Override
public void execute() { public void execute() {
logger.info("Executing..."); logger.trace(stepStr());
logger.debug("0: {}", confStr()); while (!isHalted()) {
while (!isHalted()) executeStep(); executeStep();
logger.info("Finished with STACK: {}", stackStr()); }
} }
@Override @Override
public void executeStep() { public void executeStep() {
int step = stepCounter++;
super.executeStep(); 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());
} }
} }

View File

@@ -1,6 +1,8 @@
package de.unitrier.st.uap.w25.tram; package de.unitrier.st.uap.w25.tram;
import org.apache.logging.log4j.Level; 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 org.apache.logging.log4j.core.config.Configurator;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@@ -8,6 +10,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public final class Main { public final class Main {
private static final Logger log = LogManager.getLogger(Main.class);
private Main() {} private Main() {}
static void main(String[] argv) { static void main(String[] argv) {
@@ -29,7 +33,7 @@ public final class Main {
if (i + 1 < argv.length) { if (i + 1 < argv.length) {
stackArg = argv[++i]; stackArg = argv[++i];
} else { } else {
System.err.println("Error: missing stack argument after --stack"); System.err.println("Missing stack argument after --stack");
System.exit(2); System.exit(2);
} }
} }
@@ -43,26 +47,23 @@ public final class Main {
// Verify .tram file // Verify .tram file
Path tramFile = Path.of(tramPath); Path tramFile = Path.of(tramPath);
if (!Files.exists(tramFile)) { if (!Files.exists(tramFile)) {
System.err.println("Error: file not found -> " + tramPath); System.err.println("File not found: " + tramPath);
System.exit(4); System.exit(4);
} }
// Configure console logging only
if (debug) {
Configurator.setRootLevel(Level.DEBUG);
} else {
Configurator.setRootLevel(Level.INFO);
}
// Parse optional stack // Parse optional stack
Integer[] stack = parseStack(stackArg); Integer[] stack = parseStack(stackArg);
// Load TRAM code and execute // Load TRAM code
Instruction[] code = Assembler.readTRAMCode(tramPath); 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" // Parse "[1,2,3]" or "1,2,3"