summaryrefslogtreecommitdiff
path: root/src/main/java/testing/LogCapture.java
blob: 97bb1274b958159ac61fb54e2eebe75127da37df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package testing;

import simulator.VSLogging;
import simulator.VSSimulatorVisualization;
import core.VSInternalProcess;
import java.util.*;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Custom logging implementation that captures all log messages during
 * headless simulation execution for later verification.
 */
public class LogCapture extends VSLogging {
    private final List<LogEntry> capturedLogs;
    private final Map<Integer, List<LogEntry>> processLogs;
    private final List<LogListener> listeners;
    private boolean printLogs = false;
    private String logPrefix = "[LOG] ";
    
    public LogCapture() {
        super();
        this.capturedLogs = new CopyOnWriteArrayList<>();
        this.processLogs = new ConcurrentHashMap<>();
        this.listeners = new CopyOnWriteArrayList<>();
    }
    
    public void setPrintLogs(boolean printLogs) {
        this.printLogs = printLogs;
    }
    
    public void setLogPrefix(String prefix) {
        this.logPrefix = prefix;
    }
    
    @Override
    public synchronized void log(String message) {
        // Call parent to maintain compatibility
        super.log(message);
        
        long time = 0;
        if (getSimulatorVisualization() != null) {
            time = getSimulatorVisualization().getTime();
        }
        
        LogEntry entry = new LogEntry(time, message, LogType.GLOBAL, -1);
        capturedLogs.add(entry);
        notifyListeners(entry);
        
        if (printLogs) {
            System.out.println(logPrefix + entry);
        }
    }
    
    @Override
    public synchronized void log(String message, long time) {
        super.log(message, time);
        
        LogEntry entry = new LogEntry(time, message, LogType.GLOBAL, -1);
        capturedLogs.add(entry);
        notifyListeners(entry);
        
        if (printLogs) {
            System.out.println(String.format("[%5d] %s", time, message));
        }
    }
    
    /**
     * Log a message from a specific process.
     * Note: This method is called by protocols and events.
     */
    public synchronized void log(VSInternalProcess process, String message) {
        // Create formatted message for parent
        String formattedMessage = "Process " + process.getProcessNum() + 
                                ": " + message;
        super.log(formattedMessage, process.getTime());
        
        LogEntry entry = new LogEntry(
            process.getTime(),
            message,
            LogType.PROCESS,
            process.getProcessNum()
        );
        
        capturedLogs.add(entry);
        processLogs.computeIfAbsent(process.getProcessNum(), 
                                   k -> new CopyOnWriteArrayList<>())
                   .add(entry);
        notifyListeners(entry);
        
        if (printLogs) {
            System.out.println(String.format("[%5d] Process %d: %s", 
                process.getTime(), process.getProcessNum(), message));
        }
    }
    
    private void notifyListeners(LogEntry entry) {
        for (LogListener listener : listeners) {
            try {
                listener.onLogEntry(entry);
            } catch (Exception e) {
                System.err.println("Error notifying log listener: " + e.getMessage());
            }
        }
    }
    
    /**
     * Get the simulator visualization reference.
     */
    private VSSimulatorVisualization getSimulatorVisualization() {
        try {
            Field field = VSLogging.class.getDeclaredField("simulatorVisualization");
            field.setAccessible(true);
            return (VSSimulatorVisualization) field.get(this);
        } catch (Exception e) {
            return null;
        }
    }
    
    public List<LogEntry> getCapturedLogs() {
        return new ArrayList<>(capturedLogs);
    }
    
    public Map<Integer, List<LogEntry>> getProcessLogs() {
        Map<Integer, List<LogEntry>> result = new HashMap<>();
        for (Map.Entry<Integer, List<LogEntry>> entry : processLogs.entrySet()) {
            result.put(entry.getKey(), new ArrayList<>(entry.getValue()));
        }
        return result;
    }
    
    public int getTotalLogCount() {
        return capturedLogs.size();
    }
    
    public Map<Integer, Integer> getProcessMessageCounts() {
        Map<Integer, Integer> counts = new HashMap<>();
        for (Map.Entry<Integer, List<LogEntry>> entry : processLogs.entrySet()) {
            counts.put(entry.getKey(), entry.getValue().size());
        }
        return counts;
    }
    
    public void addListener(LogListener listener) {
        listeners.add(listener);
    }
    
    public void removeListener(LogListener listener) {
        listeners.remove(listener);
    }
    
    @Override
    public synchronized void clear() {
        super.clear();
        capturedLogs.clear();
        processLogs.clear();
    }
}