1010
1111package com .digitalpetri .fsm ;
1212
13- import com .digitalpetri .fsm .FsmLogging .Level ;
1413import com .digitalpetri .fsm .dsl .ActionContext ;
1514import com .digitalpetri .fsm .dsl .ActionProxy ;
1615import com .digitalpetri .fsm .dsl .Transition ;
2625import java .util .concurrent .locks .ReentrantReadWriteLock ;
2726import java .util .function .Consumer ;
2827import java .util .function .Function ;
28+ import org .slf4j .Logger ;
29+ import org .slf4j .LoggerFactory ;
30+ import org .slf4j .MDC ;
2931
3032public class StrictMachine <S , E > implements Fsm <S , E > {
3133
@@ -38,23 +40,29 @@ public class StrictMachine<S, E> implements Fsm<S, E> {
3840 private final Map <FsmContext .Key <?>, Object > contextValues = new ConcurrentHashMap <>();
3941 private final AtomicReference <S > state = new AtomicReference <>();
4042
41- private final Object context ;
43+ private final Logger logger ;
44+ private final Map <String , String > mdc ;
4245 private final Executor executor ;
46+ private final Object userContext ;
4347 private final ActionProxy <S , E > actionProxy ;
4448 private final List <Transition <S , E >> transitions ;
4549 private final List <TransitionAction <S , E >> transitionActions ;
4650
4751 public StrictMachine (
48- Object context ,
52+ String loggerName ,
53+ Map <String , String > mdc ,
4954 Executor executor ,
55+ Object userContext ,
5056 ActionProxy <S , E > actionProxy ,
5157 S initialState ,
5258 List <Transition <S , E >> transitions ,
5359 List <TransitionAction <S , E >> transitionActions
5460 ) {
5561
56- this .context = context ;
62+ this .logger = LoggerFactory .getLogger (loggerName );
63+ this .mdc = mdc ;
5764 this .executor = executor ;
65+ this .userContext = userContext ;
5866 this .actionProxy = actionProxy ;
5967 this .transitions = transitions ;
6068 this .transitionActions = transitionActions ;
@@ -171,14 +179,18 @@ public void run() {
171179
172180 state .set (nextState );
173181
174- if (Log .isLevelEnabled (Level .DEBUG )) {
175- Log .debug (
176- context ,
177- "%s x %s = %s" ,
178- padRight (String .format ("S(%s)" , currState )),
179- padRight (String .format ("E(%s)" , event )),
180- padRight (String .format ("S'(%s)" , nextState ))
181- );
182+ if (logger .isDebugEnabled ()) {
183+ mdc .forEach (MDC ::put );
184+ try {
185+ logger .debug (
186+ "{} x {} = {}" ,
187+ padRight (String .format ("S(%s)" , currState )),
188+ padRight (String .format ("E(%s)" , event )),
189+ padRight (String .format ("S'(%s)" , nextState ))
190+ );
191+ } finally {
192+ mdc .keySet ().forEach (MDC ::remove );
193+ }
182194 }
183195
184196 var actionContext = new ActionContextImpl (
@@ -195,27 +207,49 @@ public void run() {
195207 }
196208 }
197209
198- Log .trace (context , "found %d matching TransitionActions" , matchingActions .size ());
210+ if (logger .isTraceEnabled ()) {
211+ mdc .forEach (MDC ::put );
212+ try {
213+ logger .trace ("found {} matching TransitionActions" , matchingActions .size ());
214+ } finally {
215+ mdc .keySet ().forEach (MDC ::remove );
216+ }
217+ }
199218
200219 matchingActions .forEach (transitionAction -> {
201220 try {
202221 if (actionProxy == null ) {
203- Log .trace (context , "executing TransitionAction: %s" , transitionAction );
222+ if (logger .isTraceEnabled ()) {
223+ mdc .forEach (MDC ::put );
224+ try {
225+ logger .trace ("executing TransitionAction: {}" , transitionAction );
226+ } finally {
227+ mdc .keySet ().forEach (MDC ::remove );
228+ }
229+ }
204230
205231 transitionAction .execute (actionContext );
206232 } else {
207- Log .trace (
208- context ,
209- "executing (via proxy) TransitionAction: %s" , transitionAction
210- );
233+ if (logger .isTraceEnabled ()) {
234+ mdc .forEach (MDC ::put );
235+ try {
236+ logger .trace ("executing (via proxy) TransitionAction: {}" , transitionAction );
237+ } finally {
238+ mdc .keySet ().forEach (MDC ::remove );
239+ }
240+ }
211241
212242 actionProxy .execute (actionContext , transitionAction ::execute );
213243 }
214244 } catch (Throwable ex ) {
215- Log .warn (
216- context ,
217- "Uncaught Throwable executing TransitionAction: %s\n %s" , transitionAction , ex
218- );
245+
246+ mdc .forEach (MDC ::put );
247+ try {
248+ logger .warn ("uncaught Throwable executing TransitionAction: {}" ,
249+ transitionAction , ex );
250+ } finally {
251+ mdc .keySet ().forEach (MDC ::remove );
252+ }
219253 }
220254 });
221255
@@ -318,8 +352,8 @@ public void set(Key<?> key, Object value) {
318352 }
319353
320354 @ Override
321- public Object getContext () {
322- return context ;
355+ public Object getUserContext () {
356+ return userContext ;
323357 }
324358
325359 }
0 commit comments