Skip to content

Commit ff570a7

Browse files
committed
Aligns the CLI logging with GNU standards
1 parent ee1bb80 commit ff570a7

File tree

11 files changed

+89
-82
lines changed

11 files changed

+89
-82
lines changed

lambda-buffers-codegen/app/LambdaBuffers/Codegen/Cli/Gen.hs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ data GenOpts = GenOpts
4242

4343
makeLenses ''GenOpts
4444

45-
logInfo :: String -> IO ()
46-
logInfo msg = putStrLn $ "[lbg][INFO] " <> msg <> "."
45+
logInfo :: FilePath -> String -> IO ()
46+
logInfo "" msg = putStrLn $ msg <> " [INFO]"
47+
logInfo fp msg = putStrLn $ fp <> ": " <> msg <> " [INFO]"
4748

48-
logError :: String -> IO ()
49-
logError msg = putStrLn $ "[lbg][ERROR] " <> msg <> "."
49+
logError :: FilePath -> String -> IO ()
50+
logError "" msg = putStrLn $ msg <> " [ERROR]"
51+
logError fp msg = putStrLn $ fp <> ": " <> msg <> " [ERROR]"
5052

5153
data Generated = Generated
5254
{ _generatedFilePath :: FilePath
@@ -61,8 +63,8 @@ type Handler = (PC.CodegenInput -> Map (PC.InfoLess PC.ModuleName) (Either P.Err
6163

6264
gen :: GenOpts -> Handler -> IO ()
6365
gen opts cont = do
64-
logInfo $ "Codegen Input at " <> opts ^. inputFile
65-
when (opts ^. debug) $ logInfo $ "Options received: " <> show opts
66+
logInfo "" $ "Reading Codegen Input at " <> opts ^. inputFile
67+
when (opts ^. debug) $ logInfo "" $ "Options received: " <> show opts
6668
ci <- readCodegenInput (opts ^. inputFile)
6769
ci' <- runFromProto (opts ^. outputFile) ci
6870
ci'' <- filterToRequestedClasses' opts ci'
@@ -73,11 +75,11 @@ gen opts cont = do
7375
then do
7476
writeCodegenResult (opts ^. outputFile)
7577
writePackageDeps (opts ^. genDir </> "build.json") allDeps
76-
logInfo "Code generation successful"
78+
logInfo (opts ^. inputFile) "Code generation successful"
7779
else do
7880
writeCodegenError (opts ^. outputFile) allErrors
79-
logError "Code generation reported errors"
80-
logInfo $ "Codegen Output at " <> opts ^. outputFile
81+
logError (opts ^. inputFile) "Code generation failed"
82+
logInfo "" $ "Writing Codegen Output at " <> opts ^. outputFile
8183

8284
instance MonadFail (Either String) where
8385
fail = Left
@@ -90,7 +92,7 @@ filterToRequestedClasses' opts ci = do
9092
( \cl -> do
9193
case Config.qClassNameFromText . Text.pack $ cl of
9294
Left err -> do
93-
logError err
95+
logError "" err
9496
exitFailure
9597
Right qcn -> return qcn
9698
)
@@ -104,10 +106,10 @@ filterToRequestedClasses reqCls ci =
104106
requestedClasses' = PC.classClosure ciClassRels reqCls
105107
in
106108
do
107-
logInfo $ "Computed class closure: " <> unwords (Text.unpack . Config.qClassNameToText <$> toList reqCls)
109+
logInfo "" $ "Computed class closure: " <> unwords (Text.unpack . Config.qClassNameToText <$> toList reqCls)
108110
unless (null (reqCls `Set.difference` ciQClassNames)) $ do
109-
logError $
110-
"Requested to print classes that are not available in the provided context."
111+
logError "" $
112+
"Requested to print implementations for classes that are not available in the provided context (HINT: Import the module where the type class is defined)."
111113
<> "\nClasses requested: "
112114
<> unwords (Text.unpack . Config.qClassNameToText <$> toList reqCls)
113115
<> "\nClasses available: "
@@ -130,13 +132,13 @@ collectErrorsAndDeps opts res = do
130132
( \(mn, errOrPrint) (errs, deps) -> do
131133
case errOrPrint of
132134
Left err -> do
133-
logInfo $
135+
logInfo (opts ^. inputFile) $
134136
"Code generation failed for module "
135137
<> PC.withInfoLess mn (show . PC.prettyModuleName)
136138
return (err : errs, deps)
137139
Right gend -> do
138140
writeFileAndCreate (opts ^. genDir </> (gend ^. generatedFilePath)) (gend ^. generatedCode)
139-
logInfo $
141+
logInfo (opts ^. inputFile) $
140142
"Code generation succeeded for module "
141143
<> PC.withInfoLess mn (show . PC.prettyModuleName)
142144
<> " at file path "
@@ -150,7 +152,7 @@ runFromProto :: FilePath -> P.Input -> IO PC.CodegenInput
150152
runFromProto ofp ci = case PC.codegenInputFromProto ci of
151153
Left err -> do
152154
writeCodegenError ofp [err]
153-
logError $ "Code generation failed due to problems with the input file, inspect the error in " <> ofp <> " to find out the details"
155+
logError "" $ "Code generation failed due to problems with the input file, inspect the error in " <> ofp <> " to find out the details"
154156
exitFailure
155157
Right ci' -> return ci'
156158

@@ -177,7 +179,7 @@ readCodegenInput fp = do
177179
content <- LText.readFile fp
178180
return $ PbText.readMessageOrDie content
179181
_ -> do
180-
logError $ "Unknown Codegen Input format (wanted .pb or .textproto) " <> ext
182+
logError "" $ "Unknown Codegen Input format, wanted .pb or .textproto but got " <> ext <> " (" <> fp <> ")"
181183
exitFailure
182184

183185
writeCodegenError :: FilePath -> [P.Error] -> IO ()
@@ -193,7 +195,7 @@ writeCodegenOutput fp cr = do
193195
".pb" -> BS.writeFile fp (Pb.encodeMessage cr)
194196
".textproto" -> Text.writeFile fp (Text.pack . show $ PbText.pprintMessage cr)
195197
_ -> do
196-
logError $ "Unknown Codegen Output format (wanted .pb or .textproto) " <> ext
198+
logError "" $ "Unknown Codegen Output format, wanted .pb or .textproto but got " <> ext <> " (" <> fp <> ")"
197199
exitFailure
198200

199201
writePackageDeps :: FilePath -> Set Text -> IO ()

lambda-buffers-codegen/app/LambdaBuffers/Codegen/Cli/GenHaskell.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ readHaskellConfig f = do
3838
unless
3939
fExists
4040
( do
41-
logError $ "Provided Haskell Codegen configuration file doesn't exists: " <> f
41+
logError "" $ "Provided Haskell Codegen configuration file doesn't exists: " <> f
4242
exitFailure
4343
)
4444
mayCfg <- decodeFileStrict' f
4545
case mayCfg of
4646
Nothing -> do
47-
logError $ "Invalid Haskell configuration file " <> f
47+
logError "" $ "Invalid Haskell configuration file " <> f
4848
exitFailure
4949
Just cfg -> return cfg

lambda-buffers-codegen/app/LambdaBuffers/Codegen/Cli/GenPlutarch.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ gen :: GenOpts -> IO ()
2121
gen opts = do
2222
cfg <- case opts ^. config of
2323
[] -> do
24-
logError "No Plutarch configuration file given"
24+
logError "" "No Plutarch configuration file given"
2525
exitFailure
2626
fps -> do
2727
cfgs <- traverse readPlutarchConfig fps
@@ -37,12 +37,12 @@ readPlutarchConfig f = do
3737
unless
3838
fExists
3939
( do
40-
logError $ "Provided Plutarch Codegen configuration file doesn't exists: " <> f
40+
logError "" $ "Provided Plutarch Codegen configuration file doesn't exists: " <> f
4141
exitFailure
4242
)
4343
mayCfg <- decodeFileStrict' f
4444
case mayCfg of
4545
Nothing -> do
46-
logError $ "Invalid Plutarch configuration file " <> f
46+
logError "" $ "Invalid Plutarch configuration file " <> f
4747
exitFailure
4848
Just cfg -> return cfg

lambda-buffers-codegen/app/LambdaBuffers/Codegen/Cli/GenPurescript.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ readPurescriptConfig f = do
3838
unless
3939
fExists
4040
( do
41-
logError $ "Provided Purescript Codegen configuration file doesn't exists: " <> f
41+
logError "" $ "Provided Purescript Codegen configuration file doesn't exists: " <> f
4242
exitFailure
4343
)
4444
mayCfg <- decodeFileStrict f
4545
case mayCfg of
4646
Nothing -> do
47-
logError $ "Invalid Purescript configuration file " <> f
47+
logError "" $ "Invalid Purescript configuration file " <> f
4848
exitFailure
4949
Just cfg -> return cfg

lambda-buffers-compiler/app/LambdaBuffers/Compiler/Cli/Compile.hs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,26 @@ data CompileOpts = CompileOpts
2020

2121
makeLenses ''CompileOpts
2222

23-
logInfo :: String -> IO ()
24-
logInfo msg = putStrLn $ "[lbc][INFO] " <> msg
23+
logInfo :: FilePath -> String -> IO ()
24+
logInfo "" msg = putStrLn $ msg <> " [INFO]"
25+
logInfo fp msg = putStrLn $ fp <> ": " <> msg <> " [INFO]"
2526

26-
logError :: String -> IO ()
27-
logError msg = putStrLn $ "[lbc][ERROR] " <> msg
27+
logError :: FilePath -> String -> IO ()
28+
logError "" msg = putStrLn $ msg <> " [ERROR]"
29+
logError fp msg = putStrLn $ fp <> ": " <> msg <> " [ERROR]"
2830

2931
-- | Compile LambdaBuffers modules
3032
compile :: CompileOpts -> IO ()
3133
compile opts = do
32-
logInfo $ "Compiler input at " <> opts ^. input
34+
logInfo "" $ "Reading Compiler Input from " <> (opts ^. input)
3335
compInp <- readCompilerInput (opts ^. input)
3436
let compOut = runCompiler compInp
3537
case compOut ^. maybe'error of
3638
Nothing -> do
37-
logInfo "Compilation succeeded"
39+
logInfo (opts ^. input) "Compilation succeeded"
3840
Just _ -> do
39-
logError "Compilation failed"
40-
logInfo $ "Compiler output at " <> opts ^. output
41+
logError (opts ^. input) "Compilation failed"
42+
logInfo "" $ "Writing Compiler Output at " <> (opts ^. output)
4143
writeCompilerOutput (opts ^. output) compOut
4244

4345
readCompilerInput :: FilePath -> IO Input
@@ -51,7 +53,7 @@ readCompilerInput fp = do
5153
content <- Text.readFile fp
5254
return $ PbText.readMessageOrDie content
5355
_ -> do
54-
logError $ "Unknown Compiler Input format (wanted .pb or .textproto) " <> ext
56+
logError "" $ "Unknown Compiler Input format, wanted .pb or .textproto but got " <> ext <> " (" <> fp <> ")"
5557
exitFailure
5658

5759
writeCompilerOutput :: FilePath -> Output -> IO ()
@@ -61,5 +63,5 @@ writeCompilerOutput fp cr = do
6163
".pb" -> BS.writeFile fp (Pb.encodeMessage cr)
6264
".textproto" -> Text.writeFile fp (Text.pack . show $ PbText.pprintMessage cr)
6365
_ -> do
64-
logError $ "Unknown Codegen Output format (wanted .pb or .textproto) " <> ext
66+
logError "" $ "Unknown Codegen Input format, wanted .pb or .textproto but got " <> ext <> " (" <> fp <> ")"
6567
exitFailure

lambda-buffers-frontend/app/LambdaBuffers/Frontend/Cli/Build.hs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ build opts = do
6565
mods <-
6666
either
6767
( \e -> do
68-
logError $ "Failed building Proto API modules: " <> show e
68+
logError "" $ "Failed building Proto API modules: " <> show e
6969
exitFailure
7070
)
7171
return
@@ -75,9 +75,9 @@ build opts = do
7575
( \tempDir -> do
7676
workDir <- getWorkDir opts tempDir
7777
_compRes <- callCompiler opts workDir (defMessage & Compiler.modules .~ mods)
78-
logInfo "Compilation OK"
78+
logInfo "" "Compilation OK"
7979
_cdgRes <- callCodegen opts workDir (Frontend.fres'requested res) (defMessage & Codegen.modules .~ mods)
80-
logInfo "Codegen OK"
80+
logInfo "" "Codegen OK"
8181
)
8282

8383
getWorkDir :: BuildOpts -> FilePath -> IO FilePath
@@ -87,7 +87,7 @@ getWorkDir opts tempDir = do
8787
unless
8888
exists
8989
( do
90-
logError $ "Provided working directory " <> workDir <> " doesn't exist (did you forget to create it first?)"
90+
logError "" $ "Provided working directory " <> workDir <> " doesn't exist (did you forget to create it first?)"
9191
exitFailure
9292
)
9393
return workDir
@@ -112,7 +112,7 @@ callCompiler opts workDir compInp = do
112112
then return $ compOut ^. Compiler.result
113113
else do
114114
let serrs = CompilerErrors.showErrors (compOut ^. Compiler.error)
115-
for_ serrs logCompilerError
115+
for_ serrs (logCompilerError lbcFp)
116116
exitFailure
117117

118118
writeCompilerInput :: FilePath -> Compiler.Input -> IO ()
@@ -122,7 +122,7 @@ writeCompilerInput fp compInp = do
122122
".pb" -> BS.writeFile fp (Pb.encodeMessage compInp)
123123
".textproto" -> Text.writeFile fp (Text.pack . show $ PbText.pprintMessage compInp)
124124
_ -> do
125-
logError $ "Unknown Compiler Input format (wanted .pb or .textproto) " <> ext
125+
logError fp $ "Unknown Compiler Input format (wanted .pb or .textproto) " <> ext
126126
exitFailure
127127

128128
readCompilerOutput :: FilePath -> IO Compiler.Output
@@ -133,30 +133,30 @@ readCompilerOutput fp = do
133133
content <- BS.readFile fp
134134
case Pb.decodeMessage content of
135135
Left err -> do
136-
logError $ "Failed decoding the Compiler Output\n" <> err
136+
logError fp $ "Failed decoding the Compiler Output\n" <> err
137137
exitFailure
138138
Right res -> return res
139139
".textproto" -> do
140140
content <- LText.readFile fp
141141
return $ PbText.readMessageOrDie content
142142
_ -> do
143-
logError $ "Unknown Compiler Output format (wanted .pb or .textproto) " <> ext
143+
logError fp $ "Unknown Compiler Output format (wanted .pb or .textproto) " <> ext
144144
exitFailure
145145

146146
call :: Bool -> FilePath -> [String] -> IO ()
147147
call dbg cliFp cliArgs = do
148-
when dbg $ logInfo $ "Calling: " <> showCommandForUser cliFp cliArgs
148+
when dbg $ logInfo "" $ "Calling: " <> showCommandForUser cliFp cliArgs
149149
(exitCode, stdOut, stdErr) <- readProcessWithExitCode cliFp cliArgs ""
150150
case exitCode of
151151
(ExitFailure _) -> do
152-
logError $ "Error from:" <> showCommandForUser cliFp cliArgs
153-
logError stdErr
154-
logError stdOut
152+
logError cliFp stdErr
153+
logError cliFp stdOut
154+
logError "" $ "Error from:" <> showCommandForUser cliFp cliArgs
155155
exitFailure
156156
_ -> do
157-
when dbg $ logInfo stdOut
158-
when dbg $ logInfo stdErr
159-
logInfo $ "Success from: " <> showCommandForUser cliFp cliArgs
157+
when (dbg && stdOut /= "") $ logInfo cliFp stdOut
158+
when (dbg && stdErr /= "") $ logInfo cliFp stdErr
159+
logInfo "" $ "Success from: " <> showCommandForUser cliFp cliArgs
160160
return ()
161161

162162
callCodegen :: BuildOpts -> FilePath -> [Frontend.ModuleName ()] -> Codegen.Input -> IO Codegen.Result
@@ -184,7 +184,7 @@ callCodegen opts workDir requestedModules compInp = do
184184
then return $ compOut ^. Codegen.result
185185
else do
186186
let serrs = CodegenErrors.showErrors (compOut ^. Codegen.error)
187-
for_ serrs logCodegenError
187+
for_ serrs (logCodegenError lbgFp)
188188
exitFailure
189189

190190
writeCodegenInput :: FilePath -> Codegen.Input -> IO ()
@@ -194,7 +194,7 @@ writeCodegenInput fp compInp = do
194194
".pb" -> BS.writeFile fp (Pb.encodeMessage compInp)
195195
".textproto" -> Text.writeFile fp (Text.pack . show $ PbText.pprintMessage compInp)
196196
_ -> do
197-
logError $ "Unknown Codegen Input format (wanted .pb or .textproto) " <> ext
197+
logError fp $ "Unknown Codegen Input format (wanted .pb or .textproto) " <> ext
198198
exitFailure
199199

200200
readCodegenOutput :: FilePath -> IO Codegen.Output
@@ -205,12 +205,12 @@ readCodegenOutput fp = do
205205
content <- BS.readFile fp
206206
case Pb.decodeMessage content of
207207
Left err -> do
208-
logError $ "Failed decoding the Codegen Output\n" <> err
208+
logError fp $ "Failed decoding the Codegen Output\n" <> err
209209
exitFailure
210210
Right res -> return res
211211
".textproto" -> do
212212
content <- LText.readFile fp
213213
return $ PbText.readMessageOrDie content
214214
_ -> do
215-
logError $ "Unknown Codegen Output format (wanted .pb or .textproto) " <> ext
215+
logError fp $ "Unknown Codegen Output format (wanted .pb or .textproto) " <> ext
216216
exitFailure

lambda-buffers-frontend/app/LambdaBuffers/Frontend/Cli/Env.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ getLbcFromEnvironment = do
1515
mayLbc <- lookupEnv lbcVar
1616
maybe
1717
( do
18-
logError $ lbcVar <> " environment variable is missing"
18+
logError "" $ lbcVar <> " environment variable is missing"
1919
exitFailure
2020
)
2121
return
@@ -26,7 +26,7 @@ getLbgFromEnvironment = do
2626
mayLbg <- lookupEnv lbgVar
2727
maybe
2828
( do
29-
logError $ lbgVar <> " environment variable is missing"
29+
logError "" $ lbgVar <> " environment variable is missing"
3030
exitFailure
3131
)
3232
return

0 commit comments

Comments
 (0)