Finish log4j implementation
This commit is contained in:
@@ -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"
|
||||
|
||||
Binary file not shown.
17
Project-01/uap25-pro01-tram/resources/log4j2.xml
Normal file
17
Project-01/uap25-pro01-tram/resources/log4j2.xml
Normal 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>
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user