{- | This is the main API module for the logger. You will generally want to import this directly.

Usage suggestions:


 -}
module LuminescentDreams.Logger (
    module X
  , LogConfig(..), LogLevel(..), Logger(..)
  , withLogger
  )
  where

import           LuminescentDreams.Logger.Internal
import           LuminescentDreams.Logger.JSON        as X
import           LuminescentDreams.Logger.Standard    as X

import           Data.Monoid
import           Data.IORef                           (IORef, modifyIORef)
import qualified Data.Text.Lazy                       as T
import           System.IO                            (IOMode(..), hPutStrLn, hFlush, withFile, stdout)

{- | Ordinary configurations for a file-based logger. -}
data LogConfig = Stdout LogLevel
               | Buffer (IORef T.Text) LogLevel
               | FileLog FilePath LogLevel


{- | Run an IO action with a log safely available. Logs will be properly closed in the case of an exception. Logging is meant to happen to a file, specified in LogConfig, so this may not be suitable if you want to log to a different resource. -}
withLogger :: LogConfig -> (Logger -> IO a) -> IO a
withLogger (Stdout level) act =
  act (Logger (\m -> putStrLn (T.unpack m) >> hFlush stdout) level)
withLogger (Buffer ref level) act =
  act (Logger (\m -> modifyIORef ref (\buf -> buf <> m <> T.pack "\n")) level)
withLogger (FileLog path level) act =
  withFile path AppendMode $ \h ->
    act (Logger (\m -> hPutStrLn h (T.unpack m) >> hFlush h) level)