    module System.Log.FastLogger.FileIO where

    import Foreign.Ptr (Ptr)
    import GHC.IO.Device (close)
    import GHC.IO.FD (openFile, stderr, stdout, writeRawBufferPtr)
    import qualified GHC.IO.FD as POSIX (FD (..))
    import GHC.IO.IOMode (IOMode (..))

    import System.Log.FastLogger.Imports

    type FD = POSIX.FD

    closeFD :: FD -> IO ()
    closeFD :: FD -> IO ()
    closeFD = FD -> IO ()
    forall a. IODevice a => a -> IO ()
    close

    openFileFD :: FilePath -> IO FD
    openFileFD :: FilePath -> IO FD
    openFileFD FilePath
    f = (FD, IODeviceType) -> FD
    forall a b. (a, b) -> a
    fst ((FD, IODeviceType) -> FD) -> IO (FD, IODeviceType) -> IO FD
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> FilePath -> IOMode -> Bool -> IO (FD, IODeviceType)
    openFile FilePath
    f IOMode
    AppendMode Bool
    False

    getStderrFD :: IO FD
    getStderrFD :: IO FD
    getStderrFD = FD -> IO FD
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return FD
    stderr

    getStdoutFD :: IO FD
    getStdoutFD :: IO FD
    getStdoutFD = FD -> IO FD
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return FD
    stdout

    writeRawBufferPtr2FD :: IORef FD -> Ptr Word8 -> Int -> IO Int
    writeRawBufferPtr2FD :: IORef FD -> Ptr Word8 -> Int -> IO Int
    writeRawBufferPtr2FD IORef FD
    fdref Ptr Word8
    bf Int
    len = do
        fd <- IORef FD -> IO FD
    forall a. IORef a -> IO a
    readIORef IORef FD
    fdref
        if isFDValid fd
            then
                fromIntegral <$> writeRawBufferPtr "write" fd bf 0 (fromIntegral len)
            else
                return (-1)

    invalidFD :: POSIX.FD
    invalidFD :: FD
    invalidFD = FD
    stdout{POSIX.fdFD = -1}

    isFDValid :: POSIX.FD -> Bool
    isFDValid :: FD -> Bool
    isFDValid FD
    fd = FD -> CInt
    POSIX.fdFD FD
    fd CInt -> CInt -> Bool
    forall a. Eq a => a -> a -> Bool
    /= -CInt
    1