
module Igor2.Logging.Log (
    Priority(..), LogEntry(..), Log, 
    emptyLog, newLog,
    verb2Int, int2Verb,
    

)where

import Prelude hiding ((<$>))

import Igor2.Ppr
import Data.List
import Data.Monoid (Monoid(..))
import Data.Function (on)

data Priority = ERROR       -- ^General Errors
              | WARNING     -- ^General Warnings
              | NOTICE      -- ^Normal runtime conditions
              | INFO        -- ^Information
              | DEBUG       -- ^Debug messages
    deriving (Ord, Show, Eq)
    
verb2Int :: Priority -> Integer
verb2Int DEBUG   = 4
verb2Int INFO    = 3
verb2Int NOTICE  = 2
verb2Int WARNING = 1
verb2Int ERROR   = 0
    
int2Verb :: Integer ->  Priority
int2Verb 4 = DEBUG
int2Verb 3 = INFO
int2Verb 2 = NOTICE
int2Verb 1 = WARNING
int2Verb 0 = ERROR


data LogEntry = LE Priority Doc deriving (Show)

instance Pretty LogEntry where
    pretty (LE p m) = m -- (text (show p)) <+> text l <+> ( text ": " $$ m)
    prettyList l   =  foldl' (flip ((<$>).pretty)) empty     l

newtype Log = Log {unLog :: [LogEntry]} deriving(Show)
emptyLog = Log []
newLog = Log . (:[])

instance Monoid Log where
    mempty  = emptyLog
-- #ifdef NOLOG    
--    mappend = const . const $  emptyLog
-- #else   
    mappend = ((Log . ) . (flip (++))) `on` unLog -- const . const $  emptyLog
-- #endif
      
instance Pretty Log where
    pretty = pretty . unLog