diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-21 20:34:33 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-21 20:34:33 +0300 |
| commit | ce82046a11521b0537ac2150a07a4de54aec883a (patch) | |
| tree | 73b420d63f6eadc006a75f7129ebe04762721c3f | |
| parent | f0e31d4de0c8e206260467420257a6f04ffc8e90 (diff) | |
Improve verbose logging for protocol tests
- Enable real-time log output during simulation execution
- Fix HeadlessProtocolRunner to properly show logs as they happen
- Update LogCapture to format timestamps clearly
- Add test-verbose.sh script for demonstrating logging
- Remove duplicate log printing in verbose mode
Now when running tests with -Dds.sim.verbose=true or option 3 in
test-protocols.sh, users can see protocol actions as they occur.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
| -rw-r--r-- | src/main/java/testing/HeadlessProtocolRunner.java | 20 | ||||
| -rw-r--r-- | src/main/java/testing/LogCapture.java | 5 | ||||
| -rw-r--r-- | src/main/java/testing/TestVerboseLogging.java | 27 | ||||
| -rwxr-xr-x | test-verbose.sh | 54 |
4 files changed, 95 insertions, 11 deletions
diff --git a/src/main/java/testing/HeadlessProtocolRunner.java b/src/main/java/testing/HeadlessProtocolRunner.java index 69d398f..d68d743 100644 --- a/src/main/java/testing/HeadlessProtocolRunner.java +++ b/src/main/java/testing/HeadlessProtocolRunner.java @@ -29,25 +29,27 @@ public class HeadlessProtocolRunner { System.out.println("-".repeat(50)); HeadlessSimulationRunner runner = new HeadlessSimulationRunner(); - runner.setPrintLogs(verbose); + runner.setPrintLogs(verbose); // This will print logs in real-time during simulation + + if (verbose) { + System.out.println("\n--- Real-Time Simulation Log Output ---"); + System.out.println("(Logs show protocol actions as they happen)\n"); + } try { long startTime = System.currentTimeMillis(); SimulationResult result = runner.runSimulation(simFile, 5000); // 5 second timeout long duration = System.currentTimeMillis() - startTime; + if (verbose) { + System.out.println("--- End of Simulation ---\n"); + } + System.out.println("✓ Completed in " + duration + "ms"); System.out.println(" Processes: " + result.getMetrics().getNumProcesses()); System.out.println(" Log entries: " + result.getMetrics().getTotalLogCount()); System.out.println(" Messages per process: " + result.getMetrics().getProcessMessageCounts()); - if (verbose) { - System.out.println("\n--- Log Output ---"); - for (LogEntry log : result.getAllLogs()) { - System.out.println(log.toString()); - } - } - System.out.println(); } catch (Exception e) { System.err.println("✗ FAILED: " + e.getMessage()); @@ -81,7 +83,7 @@ public class HeadlessProtocolRunner { System.out.println("-".repeat(50)); HeadlessSimulationRunner runner = new HeadlessSimulationRunner(); - runner.setPrintLogs(false); // Don't print logs when running all tests + runner.setPrintLogs(verbose); // Print logs if verbose mode is enabled try { long startTime = System.currentTimeMillis(); diff --git a/src/main/java/testing/LogCapture.java b/src/main/java/testing/LogCapture.java index 59f7ede..97bb127 100644 --- a/src/main/java/testing/LogCapture.java +++ b/src/main/java/testing/LogCapture.java @@ -62,7 +62,7 @@ public class LogCapture extends VSLogging { notifyListeners(entry); if (printLogs) { - System.out.println(logPrefix + entry); + System.out.println(String.format("[%5d] %s", time, message)); } } @@ -90,7 +90,8 @@ public class LogCapture extends VSLogging { notifyListeners(entry); if (printLogs) { - System.out.println(logPrefix + "[P" + process.getProcessNum() + "] " + message); + System.out.println(String.format("[%5d] Process %d: %s", + process.getTime(), process.getProcessNum(), message)); } } diff --git a/src/main/java/testing/TestVerboseLogging.java b/src/main/java/testing/TestVerboseLogging.java new file mode 100644 index 0000000..9833d84 --- /dev/null +++ b/src/main/java/testing/TestVerboseLogging.java @@ -0,0 +1,27 @@ +package testing; + +/** + * Simple test to demonstrate verbose logging during simulation. + */ +public class TestVerboseLogging { + + public static void main(String[] args) throws Exception { + System.out.println("=== Testing Verbose Logging ===\n"); + + // Run a short simulation with verbose logging + HeadlessSimulationRunner runner = new HeadlessSimulationRunner(); + runner.setPrintLogs(true); // Enable real-time log output + + System.out.println("Starting simulation with real-time log output...\n"); + + try { + SimulationResult result = runner.runSimulation("saved-simulations/ping-pong.dat", 1000); + + System.out.println("\n=== Simulation Complete ==="); + System.out.println("Total logs captured: " + result.getAllLogs().size()); + System.out.println("Processes: " + result.getMetrics().getNumProcesses()); + } finally { + runner.shutdown(); + } + } +}
\ No newline at end of file diff --git a/test-verbose.sh b/test-verbose.sh new file mode 100755 index 0000000..b8fbbe3 --- /dev/null +++ b/test-verbose.sh @@ -0,0 +1,54 @@ +#!/bin/bash +# +# Test verbose logging for DS-Sim protocols +# + +echo "DS-Sim Verbose Logging Test" +echo "==========================" +echo +echo "This demonstrates real-time logging during protocol simulation." +echo + +# Compile if needed +if [ ! -d "target/classes" ]; then + echo "Building project..." + mvn compile -q || { echo "Build failed!"; exit 1; } +fi + +# Run ping-pong for just 2 seconds with verbose output +echo "Running ping-pong protocol for 2 seconds with verbose output..." +echo + +# Create a simple test that runs for a limited time +cat > /tmp/TestVerbose.java << 'EOF' +import testing.*; + +public class TestVerbose { + public static void main(String[] args) throws Exception { + String simFile = args.length > 0 ? args[0] : "saved-simulations/ping-pong.dat"; + int duration = args.length > 1 ? Integer.parseInt(args[1]) : 2000; + + System.out.println("Loading: " + simFile); + System.out.println("Duration: " + duration + "ms"); + System.out.println("\n--- Real-Time Log Output ---\n"); + + HeadlessSimulationRunner runner = new HeadlessSimulationRunner(); + runner.setPrintLogs(true); + + try { + SimulationResult result = runner.runSimulation(simFile, duration); + System.out.println("\n--- Simulation Complete ---"); + System.out.println("Total events: " + result.getAllLogs().size()); + } finally { + runner.shutdown(); + } + } +} +EOF + +# Compile and run the test +javac -cp target/classes /tmp/TestVerbose.java -d /tmp +java -cp /tmp:target/classes:target/test-classes -Djava.awt.headless=true TestVerbose "$@" + +# Clean up +rm -f /tmp/TestVerbose.java /tmp/TestVerbose.class
\ No newline at end of file |
