-
Notifications
You must be signed in to change notification settings - Fork 81
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
Warn when fixities are different from base #1069
Changes from all commits
f00324b
5083410
d35f06d
2469699
2c9e39e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module Ormolu.Logging | ||
( initializeLogging, | ||
logDebug, | ||
logDebugM, | ||
logWarn, | ||
logError, | ||
logErrorM, | ||
) | ||
where | ||
|
||
import Data.Foldable (traverse_) | ||
import Data.IORef (IORef, newIORef, readIORef, writeIORef) | ||
import Ormolu.Config (Config (..)) | ||
import System.IO (hPutStrLn, stderr) | ||
import System.IO.Unsafe (unsafePerformIO) | ||
|
||
data LoggerConfig = LoggerConfig | ||
{ debugEnabled :: Bool | ||
} | ||
|
||
loggerConfig :: IORef LoggerConfig | ||
loggerConfig = unsafePerformIO $ newIORef (error "Logger not configured yet") | ||
{-# NOINLINE loggerConfig #-} | ||
|
||
initializeLogging :: Config region -> IO () | ||
initializeLogging cfg = | ||
writeIORef loggerConfig $ | ||
LoggerConfig | ||
{ debugEnabled = cfgDebug cfg | ||
} | ||
Comment on lines
+21
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is a bit hacky and unprincipled, smuggling the config through the global environment. But I figured this was much neater than storing the config in I call this function immediately in all the entrypoints ( |
||
|
||
-- | Output a debug log to stderr. | ||
-- | ||
-- Requires initializeLogging to be called first. | ||
logDebug :: | ||
-- | Some label to prefix the message with | ||
String -> | ||
-- | The message, ideally on a single line | ||
String -> | ||
a -> | ||
a | ||
logDebug label msg = logToStderr getMessage | ||
where | ||
getMessage = do | ||
cfg <- readIORef loggerConfig | ||
pure $ | ||
if debugEnabled cfg | ||
then | ||
Just . unwords $ | ||
[ "*** " <> label <> " ***", | ||
msg | ||
] | ||
else Nothing | ||
|
||
-- | Output a debug log to stderr. | ||
-- | ||
-- Requires initializeLogging to be called first. | ||
logDebugM :: | ||
(Monad m) => | ||
-- | Some label to prefix the message with | ||
String -> | ||
-- | The message, ideally on a single line | ||
String -> | ||
m () | ||
logDebugM label msg = logDebug label msg $ pure () | ||
|
||
logWarn :: String -> a -> a | ||
logWarn = logDebug "WARNING" | ||
|
||
logError :: String -> a -> a | ||
logError = logToStderr . pure . Just | ||
|
||
logErrorM :: (Monad m) => String -> m () | ||
logErrorM msg = logError msg $ pure () | ||
|
||
logToStderr :: IO (Maybe String) -> a -> a | ||
logToStderr getMessage a = | ||
unsafePerformIO $ do | ||
traverse_ (hPutStrLn stderr) =<< getMessage | ||
pure a |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Main where | ||
|
||
import Ormolu.Config qualified as Ormolu | ||
import Ormolu.Logging (initializeLogging) | ||
import Spec qualified | ||
import Test.Hspec.Runner | ||
|
||
main :: IO () | ||
main = do | ||
initializeLogging Ormolu.defaultConfig | ||
hspec Spec.spec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{-# OPTIONS_GHC -F -pgmF hspec-discover #-} | ||
{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
roots = [ | ||
"^Main.main$", | ||
"^Paths_", | ||
"^Spec.main$", | ||
"^Ormolu.Terminal.QualifiedDo.>>$" # https://github.com/ocharles/weeder/issues/112 | ||
] | ||
type-class-roots = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is technically different behavior than the previous version: if debug is not enabled, the new version still relativizes the cabal file path, even though it's not printed.
I could create a version of
logDebugM
that takes a to-be-computedIO String
message, but I don't think adding such a bespoke version is worth this relatively cheap call.