Skip to content

Commit 8bbd102

Browse files
committed
io-sim: improved the pretty printer
Compute width of time and thread id columns.
1 parent abab0be commit 8bbd102

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

io-sim/src/Control/Monad/IOSim.hs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,23 @@ ppEvents :: [(Time, ThreadId, Maybe ThreadLabel, SimEventType)]
333333
-> String
334334
ppEvents events =
335335
intercalate "\n"
336-
[ ppSimEvent width
336+
[ ppSimEvent timeWidth tidWidth width
337337
SimEvent {seTime, seThreadId, seThreadLabel, seType }
338338
| (seTime, seThreadId, seThreadLabel, seType) <- events
339339
]
340340
where
341-
width = maximum
342-
[ maybe 0 length threadLabel
343-
| (_, _, threadLabel, _) <- events
344-
]
341+
timeWidth = maximum
342+
[ length (show t)
343+
| (t, _, _, _) <- events
344+
]
345+
tidWidth = maximum
346+
[ length (show tid)
347+
| (_, tid, _, _) <- events
348+
]
349+
width = maximum
350+
[ maybe 0 length threadLabel
351+
| (_, _, threadLabel, _) <- events
352+
]
345353

346354

347355
-- | See 'runSimTraceST' below.

io-sim/src/Control/Monad/IOSim/Types.hs

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import Data.Map.Strict (Map)
9999
import Data.Maybe (fromMaybe)
100100
import Data.Monoid (Endo (..))
101101
import Data.Dynamic (Dynamic, toDyn)
102+
import Data.Semigroup (Max (..))
102103
import Data.Typeable
103104
import Data.STRef.Lazy
104105
import qualified Data.List.Trace as Trace
@@ -593,33 +594,35 @@ data SimEvent
593594
deriving Generic
594595
deriving Show via Quiet SimEvent
595596

596-
seThreadLabel' :: SimEvent -> Maybe ThreadLabel
597-
seThreadLabel' SimEvent {seThreadLabel} = seThreadLabel
598-
seThreadLabel' SimPOREvent {seThreadLabel} = seThreadLabel
599-
seThreadLabel' SimRacesFound {} = Nothing
600597

601-
ppSimEvent :: Int -- ^ width of thread label
598+
ppSimEvent :: Int -- ^ width of the time
599+
-> Int -- ^ width of thread id
600+
-> Int -- ^ width of thread label
602601
-> SimEvent
603602
-> String
604-
ppSimEvent d SimEvent {seTime, seThreadId, seThreadLabel, seType} =
605-
printf "%-24s - %-13s %-*s - %s"
603+
ppSimEvent timeWidth tidWidth tLabelWidth SimEvent {seTime, seThreadId, seThreadLabel, seType} =
604+
printf "%-*s - %-*s %-*s - %s"
605+
timeWidth
606606
(show seTime)
607+
tidWidth
607608
(show seThreadId)
608-
d
609+
tLabelWidth
609610
threadLabel
610611
(show seType)
611612
where
612613
threadLabel = fromMaybe "" seThreadLabel
613-
ppSimEvent d SimPOREvent {seTime, seThreadId, seStep, seThreadLabel, seType} =
614-
printf "%-24s - %-13s %-*s - %s"
614+
ppSimEvent timeWidth tidWidth tLableWidth SimPOREvent {seTime, seThreadId, seStep, seThreadLabel, seType} =
615+
printf "%-*s - %-*s %-*s - %s"
616+
timeWidth
615617
(show seTime)
618+
tidWidth
616619
(show (seThreadId, seStep))
617-
d
620+
tLableWidth
618621
threadLabel
619622
(show seType)
620623
where
621624
threadLabel = fromMaybe "" seThreadLabel
622-
ppSimEvent _ (SimRacesFound controls) =
625+
ppSimEvent _ _ _ (SimRacesFound controls) =
623626
"RacesFound "++show controls
624627

625628
data SimResult a
@@ -637,16 +640,55 @@ type SimTrace a = Trace.Trace (SimResult a) SimEvent
637640
ppTrace :: Show a => SimTrace a -> String
638641
ppTrace tr = Trace.ppTrace
639642
show
640-
(ppSimEvent (bimaximum (bimap (const 0) (maybe 0 length . seThreadLabel') tr)))
643+
(ppSimEvent timeWidth tidWith labelWidth)
641644
tr
645+
where
646+
(Max timeWidth, Max tidWith, Max labelWidth) =
647+
bimaximum
648+
. bimap (const (Max 0, Max 0, Max 0))
649+
(\a -> case a of
650+
SimEvent {seTime, seThreadId, seThreadLabel} ->
651+
( Max (length (show seTime))
652+
, Max (length (show (seThreadId)))
653+
, Max (length seThreadLabel)
654+
)
655+
SimPOREvent {seTime, seThreadId, seThreadLabel} ->
656+
( Max (length (show seTime))
657+
, Max (length (show (seThreadId)))
658+
, Max (length seThreadLabel)
659+
)
660+
SimRacesFound {} ->
661+
(Max 0, Max 0, Max 0)
662+
)
663+
$ tr
664+
642665

643666
-- | Like 'ppTrace' but does not show the result value.
644667
--
645668
ppTrace_ :: SimTrace a -> String
646669
ppTrace_ tr = Trace.ppTrace
647670
(const "")
648-
(ppSimEvent (bimaximum (bimap (const 0) (maybe 0 length . seThreadLabel') tr)))
671+
(ppSimEvent timeWidth tidWith labelWidth)
649672
tr
673+
where
674+
(Max timeWidth, Max tidWith, Max labelWidth) =
675+
bimaximum
676+
. bimap (const (Max 0, Max 0, Max 0))
677+
(\a -> case a of
678+
SimEvent {seTime, seThreadId, seThreadLabel} ->
679+
( Max (length (show seTime))
680+
, Max (length (show (seThreadId)))
681+
, Max (length seThreadLabel)
682+
)
683+
SimPOREvent {seTime, seThreadId, seThreadLabel} ->
684+
( Max (length (show seTime))
685+
, Max (length (show (seThreadId)))
686+
, Max (length seThreadLabel)
687+
)
688+
SimRacesFound {} ->
689+
(Max 0, Max 0, Max 0)
690+
)
691+
$ tr
650692

651693
-- | Trace each event using 'Debug.trace'; this is useful when a trace ends with
652694
-- a pure error, e.g. an assertion.

0 commit comments

Comments
 (0)