Skip to content

Commit c391d6b

Browse files
geo2agithub-actions
andauthored
Add JSON logging to Kore (#3824)
Fixes #3817 Fixes #3815 * the `Entry` type class gets a new `oneLineJson` method. Log entries will now need to implement this method if they are to be logged as JSON. The default implementation if just the type constructor name. * The `DebugAttemptEquation` log entry gets an a `oneLineJson` implementation that serialised the successful/failed equation applications in the same form as the Booster, i.e. following the RPC log format. The terms are not included to keep the log size manageable. * The `LogSomeAction` constructor of the `LogType` (a filed of `KoreLogOptions`) is repurposed. Instead of holding one log action, it now holds two, plus a predicate that specifies which log entries to log using which action. Then, the `makeKoreLogger1` function uses the selector function to filter the logs, render them with the appropriate rendered (json or standard) and log them using the appropriate log action. * `booster/Server.hs` is modified to initialize `LogSomeAction` with the appropriate log actions. If `-l SimplifyJson` log level is activated, we enable `DebugAttemptEquation` and `DebugApplyEquation` (as before). If `--simplification-log-file` is passed too, then Kore's simplification will be redirected there, otherwise they will end-up on stderr. Booster's simplilfications behave in the same way. * Finally, `booster/Proxy.hs` does not modify the request parameters when falling back to Kore or making internal simplify requests. We do not output logs received form Kore at `-l SimplifJson` anymore: Kore will now do it itself. ## Example Put simplification logs into a file ``` kore-rpc-booster def.kore --module DEF --server-port 12341 -l SimplifyJson --simplification-log-file logs.json ``` Or simply to `stderr`: ``` kore-rpc-booster def.kore --module DEF --server-port 12341 -l SimplifyJson ``` where there will be prepended with `[SimplifyJson]` --------- Co-authored-by: github-actions <[email protected]>
1 parent 0fa22f7 commit c391d6b

File tree

13 files changed

+33815
-10000
lines changed

13 files changed

+33815
-10000
lines changed

booster/test/rpc-integration/test-log-simplify-json/simplify-log.txt.golden

Lines changed: 33498 additions & 9882 deletions
Large diffs are not rendered by default.

booster/tools/booster/Proxy.hs

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Data.Bifunctor (second)
2525
import Data.Either (partitionEithers)
2626
import Data.List.NonEmpty qualified as NonEmpty
2727
import Data.Map qualified as Map
28-
import Data.Maybe (catMaybes, fromJust, fromMaybe, isJust, isNothing, mapMaybe)
28+
import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing, mapMaybe)
2929
import Data.Text (Text)
3030
import Data.Text qualified as Text
3131
import Data.Text.Lazy (toStrict)
@@ -42,7 +42,6 @@ import Kore.Internal.TermLike (TermLike, VariableName)
4242
import Kore.JsonRpc qualified as Kore (ServerState)
4343
import Kore.JsonRpc.Types
4444
import Kore.JsonRpc.Types qualified as ExecuteRequest (ExecuteRequest (..))
45-
import Kore.JsonRpc.Types qualified as ImpliesRequest (ImpliesRequest (..))
4645
import Kore.JsonRpc.Types qualified as SimplifyRequest (SimplifyRequest (..))
4746
import Kore.JsonRpc.Types.Log qualified as RPCLog
4847
import Kore.Log qualified
@@ -106,26 +105,7 @@ respondEither cfg@ProxyConfig{statsVar, boosterState} booster kore req = case re
106105
Map.lookup m bState.definitions
107106
handleExecute logSettings def start execReq
108107
Implies impliesReq -> do
109-
koreResult <-
110-
loggedKore
111-
ImpliesM
112-
( Implies
113-
impliesReq
114-
{ ImpliesRequest.logSuccessfulSimplifications =
115-
Just $ Log.LevelOther "SimplifyJson" `elem` cfg.customLogLevels
116-
}
117-
)
118-
case koreResult of
119-
Right (Implies koreRes) -> do
120-
-- Kore may have produced simplification logs during rewriting. If so,
121-
-- output them Kore at "SimplifyJson" level. Erase terms from the traces.
122-
when (isJust koreRes.logs) $ do
123-
outputLogsAtLevel (Log.LevelOther "SimplifyJson")
124-
. map RPCLog.logEntryEraseTerms
125-
. fromJust
126-
$ koreRes.logs
127-
_ -> pure ()
128-
pure koreResult
108+
loggedKore ImpliesM (Implies impliesReq)
129109
Simplify simplifyReq ->
130110
liftIO (getTime Monotonic) >>= handleSimplify simplifyReq . Just
131111
AddModule _ -> do
@@ -176,8 +156,6 @@ respondEither cfg@ProxyConfig{statsVar, boosterState} booster kore req = case re
176156
Simplify
177157
simplifyReq
178158
{ SimplifyRequest.state = boosterRes.state
179-
, SimplifyRequest.logSuccessfulSimplifications =
180-
Just $ Log.LevelOther "SimplifyJson" `elem` cfg.customLogLevels
181159
}
182160
(koreResult, koreTime) <- Stats.timed $ kore koreReq
183161
case koreResult of
@@ -195,13 +173,6 @@ respondEither cfg@ProxyConfig{statsVar, boosterState} booster kore req = case re
195173
diffBy def boosterRes.state.term koreRes.state.term
196174
in Text.pack ("Kore simplification: Diff (< before - > after)\n" <> diff)
197175
stop <- liftIO $ getTime Monotonic
198-
-- output simplification traces returned by Kore at "SimplifyJson" level, effectively
199-
-- appending them to Booster's traces. Erase terms from the traces.
200-
when (isJust koreRes.logs) $ do
201-
outputLogsAtLevel (Log.LevelOther "SimplifyJson")
202-
. map RPCLog.logEntryEraseTerms
203-
. fromJust
204-
$ koreRes.logs
205176
let timing
206177
| Just start <- mbStart
207178
, fromMaybe False simplifyReq.logTiming =
@@ -290,7 +261,7 @@ respondEither cfg@ProxyConfig{statsVar, boosterState} booster kore req = case re
290261
other -> other
291262

292263
postProcessLogs :: [RPCLog.LogEntry] -> [RPCLog.LogEntry]
293-
postProcessLogs !logs = map RPCLog.logEntryEraseTerms . filter (not . isSimplificationLogEntry) $ logs
264+
postProcessLogs !logs = map RPCLog.logEntryEraseTerms logs
294265

295266
executionLoop ::
296267
LogSettings ->
@@ -373,8 +344,6 @@ respondEither cfg@ProxyConfig{statsVar, boosterState} booster kore req = case re
373344
{ state = execStateToKoreJson simplifiedBoosterState
374345
, maxDepth = Just $ Depth 1
375346
, assumeStateDefined = Just True
376-
, ExecuteRequest.logSuccessfulSimplifications =
377-
Just $ Log.LevelOther "SimplifyJson" `elem` cfg.customLogLevels
378347
}
379348
)
380349
when (isJust statsVar) $ do
@@ -420,16 +389,6 @@ respondEither cfg@ProxyConfig{statsVar, boosterState} booster kore req = case re
420389
Log.logOtherNS "proxy" (Log.LevelOther "Aborts") $
421390
"kore confirms result " <> Text.pack (show bRes)
422391

423-
-- Kore may have produced simplification logs during rewriting. If so,
424-
-- output them Kore at "SimplifyJson" level, effectively
425-
-- appending them to Booster's traces. Erase terms from the traces.
426-
when (isJust koreResult.logs) $ do
427-
outputLogsAtLevel (Log.LevelOther "SimplifyJson")
428-
. map RPCLog.logEntryEraseTerms
429-
. filter isSimplificationLogEntry
430-
. fromJust
431-
$ koreResult.logs
432-
433392
case koreResult.reason of
434393
DepthBound -> do
435394
-- if we made one step, add the number of
@@ -648,15 +607,6 @@ combineLogs logSources
648607
| all isNothing logSources = Nothing
649608
| otherwise = Just $ concat $ catMaybes logSources
650609

651-
-- | Log a list of RPCLog items at a certain level
652-
outputLogsAtLevel :: Log.MonadLogger m => Log.LogLevel -> [RPCLog.LogEntry] -> m ()
653-
outputLogsAtLevel level = mapM_ (Log.logOtherNS "proxy" level . RPCLog.encodeLogEntryText)
654-
655-
isSimplificationLogEntry :: RPCLog.LogEntry -> Bool
656-
isSimplificationLogEntry = \case
657-
RPCLog.Simplification{} -> True
658-
_ -> False
659-
660610
makeVacuous :: Maybe [RPCLog.LogEntry] -> ExecuteResult -> ExecuteResult
661611
makeVacuous newLogs execState =
662612
execState

booster/tools/booster/Server.hs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Control.Monad.Logger (
2424
runNoLoggingT,
2525
)
2626
import Control.Monad.Logger qualified as Logger
27+
import Data.ByteString qualified as BS
2728
import Data.Conduit.Network (serverSettings)
2829
import Data.IORef (writeIORef)
2930
import Data.InternedText (globalInternedTextCache)
@@ -33,7 +34,7 @@ import Data.Map qualified as Map
3334
import Data.Maybe (fromMaybe, isJust, mapMaybe)
3435
import Data.Set qualified as Set
3536
import Data.Text qualified as Text
36-
import Data.Text.Encoding qualified as Text (decodeUtf8)
37+
import Data.Text.Encoding qualified as Text (decodeUtf8, encodeUtf8)
3738
import Options.Applicative
3839
import System.Clock (
3940
Clock (..),
@@ -69,8 +70,9 @@ import Kore.JsonRpc.Types (API, HaltReason (..), ReqOrRes (Req, Res))
6970
import Kore.JsonRpc.Types.Depth (Depth (..))
7071
import Kore.Log (
7172
ExeName (..),
72-
KoreLogType (LogSomeAction),
73+
KoreLogType (..),
7374
LogAction (LogAction),
75+
LogSomeActionData (..),
7476
TimestampsSwitch (TimestampsDisable),
7577
defaultKoreLogOptions,
7678
swappableLogger,
@@ -124,8 +126,9 @@ main = do
124126
koreSolverOptions = translateSMTOpts smtOptions
125127

126128
Booster.withLogFile simplificationLogFile $ \mLogFileHandle -> do
127-
let logLevelToHandle = \case
128-
Logger.LevelOther "SimplifyJson" -> fromMaybe IO.stderr mLogFileHandle
129+
let simplificationLogHandle = fromMaybe IO.stderr mLogFileHandle
130+
logLevelToHandle = \case
131+
Logger.LevelOther "SimplifyJson" -> simplificationLogHandle
129132
_ -> IO.stderr
130133

131134
Booster.runHandleLoggingT logLevelToHandle . Logger.filterLogger levelFilter $ do
@@ -141,7 +144,16 @@ main = do
141144

142145
monadLogger <- askLoggerIO
143146

144-
liftIO $ void $ withBugReport (ExeName "kore-rpc-booster") BugReportOnError $ \reportDirectory -> withMDLib llvmLibraryFile $ \mdl -> do
147+
koreLogEntriesAsJsonSelector <-
148+
case Map.lookup (Logger.LevelOther "SimplifyJson") logLevelToKoreLogEntryMap of
149+
Nothing -> do
150+
Logger.logWarnNS
151+
"proxy"
152+
"Could not find out which Kore log entries correspond to the SimplifyJson level"
153+
pure (const False)
154+
Just es -> pure (`elem` es)
155+
156+
liftIO $ void $ withBugReport (ExeName "kore-rpc-booster") BugReportOnError $ \_reportDirectory -> withMDLib llvmLibraryFile $ \mdl -> do
145157
let coLogLevel = fromMaybe Log.Info $ toSeverity logLevel
146158
koreLogOptions =
147159
(defaultKoreLogOptions (ExeName "") startTime)
@@ -150,11 +162,29 @@ main = do
150162
, Log.timestampsSwitch = TimestampsDisable
151163
, Log.debugSolverOptions =
152164
Log.DebugSolverOptions . fmap (<> ".kore") $ smtOptions >>= (.transcript)
153-
, Log.logType = LogSomeAction $ LogAction $ \txt -> liftIO $ monadLogger defaultLoc "kore" logLevel $ toLogStr txt
165+
, Log.logType =
166+
LogSomeAction $
167+
LogSomeActionData
168+
{ entrySelector = koreLogEntriesAsJsonSelector
169+
, standardLogAction =
170+
(LogAction $ \txt -> liftIO $ monadLogger defaultLoc "kore" logLevel $ toLogStr txt)
171+
, jsonLogAction =
172+
( LogAction $ \txt ->
173+
let bytes =
174+
Text.encodeUtf8 $
175+
if simplificationLogHandle == IO.stderr
176+
then "[SimplifyJson] " <> txt <> "\n"
177+
else txt <> "\n"
178+
in liftIO $ do
179+
BS.hPutStr simplificationLogHandle bytes
180+
IO.hFlush simplificationLogHandle
181+
)
182+
}
183+
, Log.logFormat = Log.Standard
154184
}
155185
srvSettings = serverSettings port "*"
156186

157-
withLogger reportDirectory koreLogOptions $ \actualLogAction -> do
187+
withLogger koreLogOptions $ \actualLogAction -> do
158188
mLlvmLibrary <- maybe (pure Nothing) (fmap Just . mkAPI) mdl
159189
definitionsWithCeilSummaries <-
160190
liftIO $
@@ -261,16 +291,22 @@ toSeverity LevelOther{} = Nothing
261291

262292
koreExtraLogs :: Map.Map LogLevel Log.EntryTypes
263293
koreExtraLogs =
264-
Map.map (Set.fromList . mapMaybe (`Map.lookup` Log.textToType Log.registry)) $
265-
Map.fromList
266-
[ (LevelOther "SimplifyKore", ["DebugAttemptEquation", "DebugApplyEquation"])
267-
,
268-
( LevelOther "RewriteKore"
269-
, ["DebugAttemptedRewriteRules", "DebugAppliedLabeledRewriteRule", "DebugAppliedRewriteRules"]
270-
)
271-
, (LevelOther "SimplifySuccess", ["DebugApplyEquation"])
272-
, (LevelOther "RewriteSuccess", ["DebugAppliedRewriteRules"])
273-
]
294+
Map.map
295+
(Set.fromList . mapMaybe (`Map.lookup` Log.textToType Log.registry))
296+
logLevelToKoreLogEntryMap
297+
298+
logLevelToKoreLogEntryMap :: Map.Map LogLevel [Text.Text]
299+
logLevelToKoreLogEntryMap =
300+
Map.fromList
301+
[ (LevelOther "SimplifyKore", ["DebugAttemptEquation", "DebugApplyEquation"])
302+
, (LevelOther "SimplifyJson", ["DebugAttemptEquation"])
303+
,
304+
( LevelOther "RewriteKore"
305+
, ["DebugAttemptedRewriteRules", "DebugAppliedLabeledRewriteRule", "DebugAppliedRewriteRules"]
306+
)
307+
, (LevelOther "SimplifySuccess", ["DebugApplyEquation"])
308+
, (LevelOther "RewriteSuccess", ["DebugAppliedRewriteRules"])
309+
]
274310

275311
data CLProxyOptions = CLProxyOptions
276312
{ clOptions :: CLOptions
@@ -378,7 +414,8 @@ translateSMTOpts = \case
378414
translateSExpr (SMT.Atom (SMT.SMTId x)) = KoreSMT.Atom (Text.decodeUtf8 x)
379415
translateSExpr (SMT.List ss) = KoreSMT.List $ map translateSExpr ss
380416

381-
mkKoreServer :: Log.LoggerEnv IO -> CLOptions -> KoreSolverOptions -> IO KoreServer
417+
mkKoreServer ::
418+
Log.LoggerEnv IO -> CLOptions -> KoreSolverOptions -> IO KoreServer
382419
mkKoreServer loggerEnv@Log.LoggerEnv{logAction} CLOptions{definitionFile, mainModuleName} koreSolverOptions =
383420
flip Log.runLoggerT logAction $ do
384421
sd@GlobalMain.SerializedDefinition{internedTextCache} <-

dev-tools/kore-rpc-dev/Server.hs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Control.Monad.Logger (
2424
import Control.Monad.Logger qualified as Log
2525
import Control.Monad.Logger qualified as Logger
2626
import Data.Aeson.Types (Value (..))
27+
import Data.ByteString qualified as BS
2728
import Data.Conduit.Network (serverSettings)
2829
import Data.IORef (writeIORef)
2930
import Data.InternedText (globalInternedTextCache)
@@ -32,7 +33,7 @@ import Data.Maybe (fromMaybe, mapMaybe)
3233
import Data.Set qualified as Set
3334
import Data.Text (Text)
3435
import Data.Text qualified as Text
35-
import Data.Text.Encoding qualified as Text (decodeUtf8)
36+
import Data.Text.Encoding qualified as Text (decodeUtf8, encodeUtf8)
3637
import Network.JSONRPC
3738
import Options.Applicative
3839
import System.Clock (
@@ -41,6 +42,7 @@ import System.Clock (
4142
)
4243
import System.Exit
4344
import System.IO (hPutStrLn, stderr)
45+
import System.IO qualified as IO
4446

4547
import Booster.CLOptions
4648
import Booster.SMT.Base qualified as SMT (SExpr (..), SMTId (..))
@@ -154,6 +156,15 @@ main = do
154156

155157
monadLogger <- askLoggerIO
156158

159+
koreLogEntriesAsJsonSelector <-
160+
case Map.lookup (Logger.LevelOther "SimplifyJson") logLevelToKoreLogEntryMap of
161+
Nothing -> do
162+
Logger.logWarnNS
163+
"proxy"
164+
"Could not find out which Kore log entries correspond to the SimplifyJson level"
165+
pure (const False)
166+
Just es -> pure (`elem` es)
167+
157168
let coLogLevel = fromMaybe Log.Info $ toSeverity logLevel
158169
koreLogOptions =
159170
(defaultKoreLogOptions (ExeName "") startTime)
@@ -162,16 +173,30 @@ main = do
162173
, Log.timestampsSwitch = TimestampsDisable
163174
, Log.debugSolverOptions =
164175
Log.DebugSolverOptions . fmap (<> ".kore") $ smtOptions >>= (.transcript)
165-
, Log.logType = LogSomeAction $ LogAction $ \txt -> liftIO $ monadLogger defaultLoc "kore" logLevel $ toLogStr txt
176+
, Log.logType =
177+
LogSomeAction $
178+
Log.LogSomeActionData
179+
{ entrySelector = koreLogEntriesAsJsonSelector
180+
, standardLogAction =
181+
(LogAction $ \txt -> liftIO $ monadLogger defaultLoc "kore" logLevel $ toLogStr txt)
182+
, jsonLogAction =
183+
( LogAction $ \txt ->
184+
let bytes = Text.encodeUtf8 $ "[SimplifyJson] " <> txt <> "\n"
185+
in liftIO $ do
186+
BS.hPutStr IO.stderr bytes
187+
IO.hFlush IO.stderr
188+
)
189+
}
190+
, Log.logFormat = Log.Standard
166191
}
167192
srvSettings = serverSettings port "*"
168193

169-
liftIO $ void $ withBugReport (ExeName "kore-rpc-dev") BugReportOnError $ \reportDirectory ->
170-
withLogger reportDirectory koreLogOptions $ \actualLogAction -> do
194+
liftIO $ void $ withBugReport (ExeName "kore-rpc-dev") BugReportOnError $ \_reportDirectory ->
195+
withLogger koreLogOptions $ \actualLogAction -> do
171196
mvarLogAction <- newMVar actualLogAction
172197
let logAction = swappableLogger mvarLogAction
173-
174-
kore@KoreServer{runSMT} <- mkKoreServer Log.LoggerEnv{logAction} clOPts koreSolverOptions
198+
kore@KoreServer{runSMT} <-
199+
mkKoreServer Log.LoggerEnv{logAction} clOPts koreSolverOptions
175200
runLoggingT (Logger.logInfoNS "proxy" "Starting RPC server") monadLogger
176201

177202
let koreRespond :: Respond (API 'Req) (LoggingT IO) (API 'Res)
@@ -205,13 +230,22 @@ toSeverity LevelOther{} = Nothing
205230

206231
koreExtraLogs :: Map.Map LogLevel Log.EntryTypes
207232
koreExtraLogs =
208-
Map.map (Set.fromList . mapMaybe (`Map.lookup` Log.textToType Log.registry)) $
209-
Map.fromList
210-
[ (LevelOther "SimplifyKore", ["DebugAttemptEquation", "DebugApplyEquation"])
211-
, (LevelOther "RewriteKore", ["DebugAttemptedRewriteRules", "DebugAppliedRewriteRules"])
212-
, (LevelOther "SimplifySuccess", ["DebugApplyEquation"])
213-
, (LevelOther "RewriteSuccess", ["DebugAppliedRewriteRules"])
214-
]
233+
Map.map
234+
(Set.fromList . mapMaybe (`Map.lookup` Log.textToType Log.registry))
235+
logLevelToKoreLogEntryMap
236+
237+
logLevelToKoreLogEntryMap :: Map.Map LogLevel [Text.Text]
238+
logLevelToKoreLogEntryMap =
239+
Map.fromList
240+
[ (LevelOther "SimplifyKore", ["DebugAttemptEquation", "DebugApplyEquation"])
241+
, (LevelOther "SimplifyJson", ["DebugAttemptEquation"])
242+
,
243+
( LevelOther "RewriteKore"
244+
, ["DebugAttemptedRewriteRules", "DebugAppliedLabeledRewriteRule", "DebugAppliedRewriteRules"]
245+
)
246+
, (LevelOther "SimplifySuccess", ["DebugApplyEquation"])
247+
, (LevelOther "RewriteSuccess", ["DebugAppliedRewriteRules"])
248+
]
215249

216250
newtype CLProxyOptions = CLProxyOptions
217251
{ clOptions :: CLOptions
@@ -253,7 +287,8 @@ translateSMTOpts = \case
253287
translateSExpr (SMT.Atom (SMT.SMTId x)) = KoreSMT.Atom (Text.decodeUtf8 x)
254288
translateSExpr (SMT.List ss) = KoreSMT.List $ map translateSExpr ss
255289

256-
mkKoreServer :: Log.LoggerEnv IO -> CLOptions -> KoreSMT.KoreSolverOptions -> IO KoreServer
290+
mkKoreServer ::
291+
Log.LoggerEnv IO -> CLOptions -> KoreSMT.KoreSolverOptions -> IO KoreServer
257292
mkKoreServer loggerEnv@Log.LoggerEnv{logAction} CLOptions{definitionFile, mainModuleName} koreSolverOptions =
258293
flip Log.runLoggerT logAction $ do
259294
sd@GlobalMain.SerializedDefinition{internedTextCache} <-

dev-tools/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ executables:
176176
dependencies:
177177
- aeson
178178
- base
179+
- bytestring
179180
- clock
180181
- conduit-extra
181182
- containers

0 commit comments

Comments
 (0)