{-| This module provides a convenient way to decode a 'Manager' from yaml string


Here is a yaml string template:

@
  sinks:
    root:
      handlers:
      - console
      - file
    App.Json:
      handlers:
      - file
      propagate: false
    App.Yaml:
      handlers:
      - rotate
      propagate: false

  handlers:
    console:
      type: StreamHandler
      level: DEBUG
      formatter: simple
      stream: stderr
    file:
      type: FileHandler
      level: "Level 100"
      formatter: standard
      file: /etc/my/json.log
      encoding: utf8
    rotate:
      type: RotatingFileHandler
      level: INFO
      formatter: standard
      file: /etc/my/json.log
      encoding: utf8
      maxBytes: 1048576
      backupCount: 10

  formatters:
    simple: "{message}"
    standard: "{asctime:%Y-%m-%dT%H:%M:%S%6Q%z} - {level} - {logger}] {message}"

  disabled: false

  catchUncaughtException: true
@

-}
module Logging.Config.Yaml (getManager, getManagerFile) where


import           Control.Exception
import           Data.ByteString
import           Data.Yaml

import           Logging.Config.Type
import           Logging.Manager


-- | decode a 'Manager' from strict 'ByteString'
--
-- @since 0.4.0
getManager :: ByteString -> IO Manager
getManager bs =
  case decodeEither' bs of
    Left e       -> throw $ ConfigException $ prettyPrintParseException e
    Right config -> createManager config


-- | decode a 'Manager' from a file
--
-- @since 0.4.0
getManagerFile :: FilePath -> IO Manager
getManagerFile path = do
  res <- decodeFileEither path
  case res of
    Left e       -> throw $ ConfigException $ prettyPrintParseException e
    Right config -> createManager config