Add logging jars

This commit is contained in:
Jan-Niclas Loosen
2025-10-25 20:23:58 +02:00
parent a22a2f3ea6
commit 81e1f84fd9
15 changed files with 116 additions and 22 deletions

1
.gitignore vendored
View File

@@ -15,7 +15,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -3,31 +3,116 @@ package de.unitrier.st.uap.w25.tram;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
final class Main {
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public final class Main {
private Main() {}
static void main(String[] argv) {
// Configurator.setRootLevel(Level.DEBUG);
Configurator.setRootLevel(Level.INFO);
public static void main(String[] argv) {
if (argv.length < 1) {
System.err.println("Usage:");
System.err.println(" java -jar tram.jar <tramFile> [--debug [logFile]] [--stack [v1,v2,...]]");
System.exit(1);
}
Instruction[] code = Assembler.readTRAMCode(
//"tramcode/square.tram"
//"tramcode/wrapper.tram"
//"tramcode/example1.tram"
//"tramcode/example2.tram"
//"tramcode/example3.tram"
"tramcode/test.tram"
);
String tramPath = argv[0];
boolean debug = false;
String logFile = null;
String stackArg = null;
// int lineNr = 0;
// for (Instruction instr : code) {
// if (instr != null) {
// System.out.println(String.format("%03d", lineNr) + "| " + instr);
// lineNr++;
// }
// }
// Parse arguments
for (int i = 1; i < argv.length; i++) {
switch (argv[i]) {
case "--debug" -> {
debug = true;
if (i + 1 < argv.length && !argv[i + 1].startsWith("--")) {
logFile = argv[++i];
}
}
case "--stack" -> {
if (i + 1 < argv.length) {
stackArg = argv[++i];
} else {
System.err.println("Error: missing stack argument after --stack");
System.exit(2);
}
}
default -> {
System.err.println("Unknown argument: " + argv[i]);
System.exit(3);
}
}
}
LoggedMachine tram = new LoggedMachine(code, new Integer[]{}); // empty stack or predefined args
// Verify .tram file
Path tramFile = Path.of(tramPath);
if (!Files.exists(tramFile)) {
System.err.println("Error: file not found -> " + tramPath);
System.exit(4);
}
// Configure logging
if (debug) {
Configurator.setRootLevel(Level.DEBUG);
if (logFile != null) {
Path logPath = Path.of(logFile);
try {
Files.createDirectories(logPath.getParent() != null ? logPath.getParent() : Path.of("."));
if (!Files.exists(logPath)) Files.createFile(logPath);
} catch (IOException e) {
System.err.println("Error: cannot create log file -> " + logPath);
e.printStackTrace();
System.exit(5);
}
LoggedMachine.toFile(logPath.toString());
System.out.println("Debug logging to file: " + logPath);
} else {
System.out.println("Debug logging to console.");
}
} else {
Configurator.setRootLevel(Level.INFO);
}
// Parse optional stack
Integer[] stack = parseStack(stackArg);
// Load TRAM code and execute
Instruction[] code = Assembler.readTRAMCode(tramPath);
LoggedMachine tram = new LoggedMachine(code, stack);
tram.execute();
System.out.println("Execution complete.");
}
/**
* Parses a stack argument given in Python list notation, e.g. "[1,2,3]"
*/
private static Integer[] parseStack(String stackArg) {
if (stackArg == null || stackArg.isBlank()) {
return new Integer[] {};
}
String trimmed = stackArg.trim();
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
trimmed = trimmed.substring(1, trimmed.length() - 1);
}
if (trimmed.isBlank()) {
return new Integer[] {};
}
String[] parts = trimmed.split(",");
List<Integer> list = new ArrayList<>();
for (String p : parts) {
try {
list.add(Integer.parseInt(p.trim()));
} catch (NumberFormatException e) {
System.err.println("Invalid stack element: " + p);
System.exit(6);
}
}
return list.toArray(new Integer[0]);
}
}

View File

@@ -36,10 +36,11 @@
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/log4j-core-2.24.1.jar!/" />
<root url="file://$MODULE_DIR$/lib" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
</library>
</orderEntry>
<orderEntry type="module-library">
@@ -51,5 +52,14 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/log4j-core-2.24.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>