001package jmri.jmrit.logixng.tools.debugger;
002
003import jmri.InstanceManager;
004import jmri.jmrit.logixng.*;
005import jmri.jmrit.logixng.implementation.AbstractMaleSocket;
006
007/**
008 * Abstract debugger male socket
009 * @author Daniel Bergqvist 2020
010 */
011public abstract class AbstractDebuggerMaleSocket extends AbstractMaleSocket {
012
013    private final Debugger _debugger = InstanceManager.getDefault(Debugger.class);
014
015    private boolean _breakpointBefore = false;
016    private boolean _breakpointAfter = false;
017
018    private boolean _stepInto = true;
019    private boolean _lastDoBreak = true;
020
021    private boolean _logBefore = false;
022    private boolean _logAfter = false;
023
024
025    public AbstractDebuggerMaleSocket(BaseManager<? extends MaleSocket> manager, MaleSocket maleSocket) {
026        super(manager, maleSocket);
027    }
028
029    /**
030     * Get information about this action/expression before it is executed or
031     * evaluated.
032     * @return an information string
033     */
034    public abstract String getBeforeInfo();
035
036    /**
037     * Get information about this action/expression after it is executed or
038     * evaluated.
039     * @return an information string
040     */
041    public abstract String getAfterInfo();
042
043    protected boolean isLogAllBefore() {
044        return InstanceManager.getDefault(LogixNGPreferences.class).getLogAllBefore();
045    }
046
047    protected boolean isLogAllAfter() {
048        return InstanceManager.getDefault(LogixNGPreferences.class).getLogAllAfter();
049    }
050
051    protected boolean isDebuggerActive() {
052        return _debugger.isDebuggerActive()
053                && (_debugger.getDebugConditionalNG() == this.getConditionalNG());
054    }
055
056    protected void before() {
057        if (isLogAllBefore() || _logBefore) {
058            ConditionalNG cng = getConditionalNG();
059            String info = getBeforeInfo();
060            if (!info.isBlank()) {
061                info = " --- " + info;
062            }
063            log.warn("LogixNG Before: {}, {}: {}{}", cng.getLogixNG().getSystemName(), cng.getSystemName(), getLongDescription(), info);
064        }
065        _lastDoBreak = _debugger.getBreak();
066        if (isDebuggerActive() && (_debugger.getBreak() || _breakpointBefore)) {
067//            System.out.format("Before: %s%n", getLongDescription());
068            _debugger.firePropertyChange(Debugger.STEP_BEFORE, null, this);
069            _debugger.setBreak(_stepInto);
070        }
071    }
072
073    protected void after() {
074        if (isDebuggerActive()) {
075            _debugger.setBreak(_lastDoBreak);
076            if (_debugger.getBreak() || _breakpointAfter) {
077//                System.out.format("After: %s%n", getLongDescription());
078                _debugger.firePropertyChange(Debugger.STEP_AFTER, null, this);
079            }
080        }
081        if (isLogAllAfter() || _logAfter) {
082            ConditionalNG cng = getConditionalNG();
083            String info = getAfterInfo();
084            if (!info.isBlank()) {
085                info = " --- " + info;
086            }
087            log.warn("LogixNG  After: {}, {}: {}{}", cng.getLogixNG().getSystemName(), cng.getSystemName(), getLongDescription(), info);
088        }
089    }
090
091    public void setStepInto(boolean value) {
092        _stepInto = value;
093    }
094
095    public void setBreakpointBefore(boolean value) {
096        _breakpointBefore = value;
097    }
098
099    public boolean getBreakpointBefore() {
100        return _breakpointBefore;
101    }
102
103    public void setBreakpointAfter(boolean value) {
104        _breakpointAfter = value;
105    }
106
107    public boolean getBreakpointAfter() {
108        return _breakpointAfter;
109    }
110
111    public void setLogBefore(boolean value) {
112        _logBefore = value;
113    }
114
115    public boolean getLogBefore() {
116        return _logBefore;
117    }
118
119    public void setLogAfter(boolean value) {
120        _logAfter = value;
121    }
122
123    public boolean getLogAfter() {
124        return _logAfter;
125    }
126
127    @Override
128    protected final void registerListenersForThisClass() {
129        ((MaleSocket)getObject()).registerListeners();
130    }
131
132    @Override
133    protected final void unregisterListenersForThisClass() {
134        ((MaleSocket)getObject()).unregisterListeners();
135    }
136
137    @Override
138    protected final void disposeMe() {
139        ((MaleSocket)getObject()).dispose();
140    }
141
142    @Override
143    public final void setEnabled(boolean enable) {
144        ((MaleSocket)getObject()).setEnabled(enable);
145    }
146
147    @Override
148    public void setEnabledFlag(boolean enable) {
149        ((MaleSocket)getObject()).setEnabledFlag(enable);
150    }
151
152    @Override
153    public final boolean isEnabled() {
154        return ((MaleSocket)getObject()).isEnabled();
155    }
156
157    @Override
158    public final void setDebugConfig(DebugConfig config) {
159        ((MaleSocket)getObject()).setDebugConfig(config);
160    }
161
162    @Override
163    public final DebugConfig getDebugConfig() {
164        return ((MaleSocket)getObject()).getDebugConfig();
165    }
166
167    @Override
168    public final DebugConfig createDebugConfig() {
169        return ((MaleSocket)getObject()).createDebugConfig();
170    }
171
172    @Override
173    public final String getComment() {
174        return ((MaleSocket)getObject()).getComment();
175    }
176
177    @Override
178    public final void setComment(String comment) {
179        ((MaleSocket)getObject()).setComment(comment);
180    }
181
182    @Override
183    public void setParent(Base parent) {
184        super.setParent(parent);
185        ((MaleSocket)getObject()).setParent(this);
186    }
187
188    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractDebuggerMaleSocket.class);
189}