Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring logging functions / make the error messages more closely match gccs output #249

Merged
merged 9 commits into from
Oct 5, 2024
1 change: 1 addition & 0 deletions extras/lambda-buffers-utils/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake ..#dev-utils
jaredponn marked this conversation as resolved.
Show resolved Hide resolved
39 changes: 39 additions & 0 deletions extras/lambda-buffers-utils/build.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{ inputs, ... }:
{
perSystem = { config, pkgs, system, ... }:
let
hsFlake = inputs.flake-lang.lib.${system}.haskellFlake {
src = ./.;

name = "lambda-buffers-utils";

inherit (config.settings.haskell) index-state compiler-nix-name;

dependencies = [
];

devShellTools = config.settings.shell.tools;
devShellHook = config.settings.shell.hook;
};
in

{
devShells.dev-utils = hsFlake.devShell;

packages = {

lambda-buffers-utils-src = pkgs.stdenv.mkDerivation {
name = "lambda-buffers-utils-src";
src = ./.;
phases = "installPhase";
installPhase = "ln -s $src $out";
};

lambda-buffers-utils-lib = hsFlake.packages."lambda-buffers-utils:lib:lambda-buffers-utils";
lambda-buffers-utils-tests = hsFlake.packages."lambda-buffers-utils:test:tests";
};

inherit (hsFlake) checks;

};
}
6 changes: 6 additions & 0 deletions extras/lambda-buffers-utils/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
packages: ./.

tests: true

-- TODO(bladyjoker): Remove once freer-simple fixes the template-haskell bounds
allow-newer: freer-simple:template-haskell
jaredponn marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions extras/lambda-buffers-utils/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cradle:
cabal:
102 changes: 102 additions & 0 deletions extras/lambda-buffers-utils/lambda-buffers-utils.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
cabal-version: 3.0
name: lambda-buffers-utils
version: 0.1.0.0
synopsis: Lambda Buffers utils
data-files:
extra-source-files:
author: MLabs LTD
maintainer: [email protected]

flag dev
description: Enable non-strict compilation for development
manual: True

common common-language
ghc-options:
-Wall -Wcompat -fprint-explicit-foralls -fprint-explicit-kinds
-fwarn-missing-import-lists -Weverything -Wno-unsafe
-Wno-missing-safe-haskell-mode -Wno-implicit-prelude
-Wno-missed-specialisations -Wno-all-missed-specialisations
-Wno-missing-kind-signatures

if !flag(dev)
ghc-options: -Werror

default-extensions:
BangPatterns
BinaryLiterals
ConstrainedClassMethods
ConstraintKinds
DataKinds
DeriveAnyClass
DeriveDataTypeable
DeriveFoldable
DeriveFunctor
DeriveGeneric
DeriveLift
DeriveTraversable
DerivingStrategies
DerivingVia
DoAndIfThenElse
DuplicateRecordFields
EmptyCase
EmptyDataDecls
EmptyDataDeriving
ExistentialQuantification
ExplicitForAll
ExplicitNamespaces
FlexibleContexts
FlexibleInstances
ForeignFunctionInterface
GADTSyntax
GeneralizedNewtypeDeriving
HexFloatLiterals
ImportQualifiedPost
InstanceSigs
KindSignatures
LambdaCase
MonomorphismRestriction
MultiParamTypeClasses
NamedFieldPuns
NamedWildCards
NoStarIsType
NumericUnderscores
OverloadedLabels
OverloadedStrings
PartialTypeSignatures
PatternGuards
PolyKinds
PostfixOperators
RankNTypes
RecordWildCards
RelaxedPolyRec
ScopedTypeVariables
StandaloneDeriving
StandaloneKindSignatures
TemplateHaskell
TraditionalRecordSyntax
TupleSections
TypeApplications
TypeFamilies
TypeOperators
TypeSynonymInstances
ViewPatterns

default-language: Haskell2010

library
import: common-language
build-depends: base >=4.16
exposed-modules: LambdaBuffers.Logger
hs-source-dirs: src

test-suite tests
import: common-language
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Test.hs
build-depends:
, base >=4.16
, tasty

other-modules:
41 changes: 41 additions & 0 deletions extras/lambda-buffers-utils/src/LambdaBuffers/Logger.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{- | Module: LambdaBuffers.Logger

Utilities for logging INFO, and ERROR messages.

This follows the GNU error message format:
<https://www.gnu.org/prep/standards/html_node/Errors.html>
-}
module LambdaBuffers.Logger (logInfo, logError, logErrorWithSourceSpan) where
jaredponn marked this conversation as resolved.
Show resolved Hide resolved

import System.IO qualified

{- | Logs an INFO message of the format
> <file>: info: <msg>
if @<file>@ is non empty, otherwise, it prints
> info: <msg>
-}
logInfo :: FilePath -> String -> IO ()
logInfo "" msg = System.IO.hPutStrLn System.IO.stderr $ "info: " <> msg
logInfo fp msg = System.IO.hPutStrLn System.IO.stderr $ fp <> ": info: " <> msg

{- | Logs an ERROR message of the format
> <file>: error: <msg>
if @<file>@ is non empty, otherwise, it prints
> error: <msg>
-}
logError :: FilePath -> String -> IO ()
logError "" msg = System.IO.hPutStrLn System.IO.stderr $ "error: " <> msg
logError fp msg = System.IO.hPutStrLn System.IO.stderr $ fp <> ": error: " <> msg

{- | Logs an ERROR message of the format
> <file>:<line1>.<column1>-<line2>.<column2>: error: <msg>
or
> <file>:<line1>.<column1>: error: <msg>
if <line1>, <line2> are the same and <column1>, <column2> are the same.
-}
logErrorWithSourceSpan :: FilePath -> (Int, Int) -> (Int, Int) -> String -> IO ()
logErrorWithSourceSpan fp pos1 pos2 msg =
System.IO.hPutStrLn System.IO.stderr $ fp <> ":" <> showLineCol pos1 <> (if pos1 == pos2 then "" else "-" <> showLineCol pos2) <> ": " <> "error: " <> msg
where
showLineCol :: (Int, Int) -> String
showLineCol (l, c) = show l <> "." <> show c
10 changes: 10 additions & 0 deletions extras/lambda-buffers-utils/test/Test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Main (main) where

import Test.Tasty (defaultMain, testGroup)

main :: IO ()
main =
defaultMain $
testGroup
"Utils test"
[]
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
./extras/build.nix
./extras/lbf-nix/build.nix
./extras/dev-shells/build.nix
./extras/lambda-buffers-utils/build.nix
./libs/build.nix
./api/build.nix
./lambda-buffers-compiler/build.nix
Expand Down
27 changes: 10 additions & 17 deletions lambda-buffers-codegen/app/LambdaBuffers/Codegen/Cli/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Data.Text.IO qualified as Text
import Data.Text.Lazy.IO qualified as LText
import Data.Traversable (for)
import LambdaBuffers.Codegen.Config qualified as Config
import LambdaBuffers.Logger (logError, logInfo)
import LambdaBuffers.ProtoCompat qualified as PC
import Proto.Codegen qualified as P
import Proto.Codegen_Fields qualified as P
Expand All @@ -42,14 +43,6 @@ data GenOpts = GenOpts

makeLenses ''GenOpts

logInfo :: FilePath -> String -> IO ()
logInfo "" msg = putStrLn $ msg <> " [INFO]"
logInfo fp msg = putStrLn $ fp <> ": " <> msg <> " [INFO]"

logError :: FilePath -> String -> IO ()
logError "" msg = putStrLn $ msg <> " [ERROR]"
logError fp msg = putStrLn $ fp <> ": " <> msg <> " [ERROR]"

data Generated = Generated
{ _generatedFilePath :: !FilePath
, _generatedCode :: !Text
Expand All @@ -63,8 +56,8 @@ type Handler = (PC.CodegenInput -> Map (PC.InfoLess PC.ModuleName) (Either P.Err

gen :: GenOpts -> Handler -> IO ()
gen opts cont = do
logInfo "" $ "Reading Codegen Input at " <> opts ^. inputFile
when (opts ^. debug) $ logInfo "" $ "Options received: " <> show opts
logInfo (opts ^. inputFile) $ "reading Codegen Input at " <> opts ^. inputFile
when (opts ^. debug) $ logInfo "" $ "options received: " <> show opts
ci <- readCodegenInput (opts ^. inputFile)
ci' <- runFromProto (opts ^. outputFile) ci
ci'' <- filterToRequestedClasses' opts ci'
Expand All @@ -75,11 +68,11 @@ gen opts cont = do
then do
writeCodegenResult (opts ^. outputFile)
writePackageDeps (opts ^. genDir </> "build.json") allDeps
logInfo (opts ^. inputFile) "Code generation successful"
logInfo (opts ^. inputFile) "code generation successful"
else do
writeCodegenError (opts ^. outputFile) allErrors
logError (opts ^. inputFile) "Code generation failed"
logInfo "" $ "Writing Codegen Output at " <> opts ^. outputFile
logError (opts ^. inputFile) "code generation failed"
logInfo (opts ^. outputFile) $ "writing Codegen Output at " <> opts ^. outputFile

instance MonadFail (Either String) where
fail = Left
Expand All @@ -106,10 +99,10 @@ filterToRequestedClasses reqCls ci =
requestedClasses' = PC.classClosure ciClassRels reqCls
in
do
logInfo "" $ "Computed class closure: " <> unwords (Text.unpack . Config.qClassNameToText <$> toList reqCls)
logInfo "" $ "computed class closure: " <> unwords (Text.unpack . Config.qClassNameToText <$> toList reqCls)
unless (null (reqCls `Set.difference` ciQClassNames)) $ do
logError "" $
"Requested to print implementations for classes that are not available in the provided context (HINT: Import the module where the type class is defined)."
"requested to print implementations for classes that are not available in the provided context (HINT: Import the module where the type class is defined)."
<> "\nClasses requested: "
<> unwords (Text.unpack . Config.qClassNameToText <$> toList reqCls)
<> "\nClasses available: "
Expand All @@ -133,13 +126,13 @@ collectErrorsAndDeps opts res = do
case errOrPrint of
Left err -> do
logInfo (opts ^. inputFile) $
"Code generation failed for module "
"code generation failed for module "
<> PC.withInfoLess mn (show . PC.prettyModuleName)
return (err : errs, deps)
Right gend -> do
writeFileAndCreate (opts ^. genDir </> (gend ^. generatedFilePath)) (gend ^. generatedCode)
logInfo (opts ^. inputFile) $
"Code generation succeeded for module "
"code generation succeeded for module "
<> PC.withInfoLess mn (show . PC.prettyModuleName)
<> " at file path "
<> (opts ^. genDir </> (gend ^. generatedFilePath))
Expand Down
1 change: 1 addition & 0 deletions lambda-buffers-codegen/build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"${config.packages.lambda-buffers-compiler-hs-pb}"
"${config.packages.lambda-buffers-codegen-hs-pb}"
"${config.packages.lambda-buffers-compiler-src}"
"${config.packages.lambda-buffers-utils-src}"
];

devShellTools = config.settings.shell.tools;
Expand Down
1 change: 1 addition & 0 deletions lambda-buffers-codegen/lambda-buffers-codegen.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ executable lbg
, lambda-buffers-codegen
, lambda-buffers-codegen-pb
, lambda-buffers-compiler
, lambda-buffers-utils
, lens
, optparse-applicative
, proto-lens
Expand Down
21 changes: 7 additions & 14 deletions lambda-buffers-compiler/app/LambdaBuffers/Compiler/Cli/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Data.ProtoLens.TextFormat qualified as PbText
import Data.Text.Lazy qualified as Text
import Data.Text.Lazy.IO qualified as Text
import LambdaBuffers.Compiler (runCompiler)
import LambdaBuffers.Logger (logError, logInfo)
import Proto.Compiler (Input, Output)
import Proto.Compiler_Fields (maybe'error)
import System.Exit (exitFailure)
Expand All @@ -20,26 +21,18 @@ data CompileOpts = CompileOpts

makeLenses ''CompileOpts

logInfo :: FilePath -> String -> IO ()
logInfo "" msg = putStrLn $ msg <> " [INFO]"
logInfo fp msg = putStrLn $ fp <> ": " <> msg <> " [INFO]"

logError :: FilePath -> String -> IO ()
logError "" msg = putStrLn $ msg <> " [ERROR]"
logError fp msg = putStrLn $ fp <> ": " <> msg <> " [ERROR]"

-- | Compile LambdaBuffers modules
compile :: CompileOpts -> IO ()
compile opts = do
logInfo "" $ "Reading Compiler Input from " <> (opts ^. input)
logInfo (opts ^. input) $ "reading Compiler Input from " <> (opts ^. input)
compInp <- readCompilerInput (opts ^. input)
let compOut = runCompiler compInp
case compOut ^. maybe'error of
Nothing -> do
logInfo (opts ^. input) "Compilation succeeded"
logInfo (opts ^. input) "compilation succeeded"
Just _ -> do
logError (opts ^. input) "Compilation failed"
logInfo "" $ "Writing Compiler Output at " <> (opts ^. output)
logError (opts ^. input) "compilation failed"
logInfo (opts ^. output) $ "writing Compiler Output at " <> (opts ^. output)
writeCompilerOutput (opts ^. output) compOut

readCompilerInput :: FilePath -> IO Input
Expand All @@ -53,7 +46,7 @@ readCompilerInput fp = do
content <- Text.readFile fp
return $ PbText.readMessageOrDie content
_ -> do
logError "" $ "Unknown Compiler Input format, wanted .pb or .textproto but got " <> ext <> " (" <> fp <> ")"
logError fp $ "unknown Compiler Input format, wanted .pb or .textproto but got " <> ext <> " (" <> fp <> ")"
exitFailure

writeCompilerOutput :: FilePath -> Output -> IO ()
Expand All @@ -63,5 +56,5 @@ writeCompilerOutput fp cr = do
".pb" -> BS.writeFile fp (Pb.encodeMessage cr)
".textproto" -> Text.writeFile fp (Text.pack . show $ PbText.pprintMessage cr)
_ -> do
logError "" $ "Unknown Codegen Input format, wanted .pb or .textproto but got " <> ext <> " (" <> fp <> ")"
logError fp $ "unknown Codegen Input format, wanted .pb or .textproto but got " <> ext <> " (" <> fp <> ")"
exitFailure
1 change: 1 addition & 0 deletions lambda-buffers-compiler/build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"${config.packages.lambda-buffers-lang-hs-pb}"
"${config.packages.lambda-buffers-compiler-hs-pb}"
"${config.packages.lambda-buffers-codegen-hs-pb}"
"${config.packages.lambda-buffers-utils-src}"
];

devShellTools = config.settings.shell.tools;
Expand Down
1 change: 1 addition & 0 deletions lambda-buffers-compiler/lambda-buffers-compiler.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ executable lbc
, bytestring
, lambda-buffers-compiler
, lambda-buffers-compiler-pb
, lambda-buffers-utils
, lens
, optparse-applicative
, proto-lens
Expand Down
Loading