-- cabal-helper: Simple interface to Cabal's configuration state
-- Copyright (C) 2018  Daniel Gröber <cabal-helper@dxld.at>
--
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0

{-|
Module      : CabalHelper.Compiletime.Log
Description : Logging utilities
License     : Apache-2.0
-}

module CabalHelper.Compiletime.Log where

import Control.Monad.IO.Class
import System.IO
import System.IO.Error

import CabalHelper.Compiletime.Types

logIOError :: Verbose => String -> IO (Maybe a) -> IO (Maybe a)
logIOError :: String -> IO (Maybe a) -> IO (Maybe a)
logIOError String
label IO (Maybe a)
a = do
  IO (Maybe a)
a IO (Maybe a) -> (IOError -> IO (Maybe a)) -> IO (Maybe a)
forall a. IO a -> (IOError -> IO a) -> IO a
`catchIOError` \IOError
ex -> do
      String -> IO ()
forall (m :: * -> *). (MonadIO m, Verbose) => String -> m ()
vLog (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
label String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ IOError -> String
forall a. Show a => a -> String
show IOError
ex
      Maybe a -> IO (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing

vLog :: (MonadIO m, Verbose) => String -> m ()
vLog :: String -> m ()
vLog String
msg
    | Verbose
Word -> Bool
?verbose Word
0 = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Handle -> String -> IO ()
hPutStrLn Handle
stderr String
msg
    | Bool
otherwise = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()