affection/src/Affection/Logging.hs

55 lines
1.6 KiB
Haskell
Raw Permalink Normal View History

2017-12-12 12:10:00 +00:00
{-# LANGUAGE CPP #-}
2018-09-25 14:10:36 +00:00
-- | This module defines the logging capability of Affection, whis is derived
2018-09-25 05:02:33 +00:00
-- from "Debug.Trace".
2018-09-25 14:10:36 +00:00
module Affection.Logging where
2017-12-12 12:10:00 +00:00
2019-10-28 16:11:27 +00:00
import qualified Data.Text as T
2017-12-12 12:10:00 +00:00
import Debug.Trace
2018-09-25 05:02:33 +00:00
-- | The log level definition
2017-12-12 12:10:00 +00:00
data LogLevel
2018-09-25 05:02:33 +00:00
= Verbose -- ^ Log everything
| Debug -- ^ Log Debug messages and above
| Warn -- ^ Log only Warnings and errors
| Error -- ^ Log only errors
2017-12-12 12:10:00 +00:00
2018-09-25 05:02:33 +00:00
-- | Pure logging function
log
:: LogLevel -- ^ Log level to log to
2019-10-28 16:11:27 +00:00
-> T.Text -- ^ The message string
2018-09-25 05:02:33 +00:00
-> a -- ^ Arbitrary datatype to return
-> a -- ^ Returned data
2017-12-22 05:28:58 +00:00
#if defined(VERBOSE)
2019-10-28 16:11:27 +00:00
log Verbose s = trace ("VERBOSE: " ++ T.unpack s)
2017-12-22 05:28:58 +00:00
#endif
#if defined(DEBUG) || defined(VERBOSE)
2019-10-28 16:11:27 +00:00
log Debug s = trace ("DEBUG: " ++ T.unpack s)
2017-12-12 12:10:00 +00:00
#endif
2017-12-22 05:28:58 +00:00
#if defined(WARN) || defined(DEBUG) || defined(VERBOSE)
2019-10-28 16:11:27 +00:00
log Warn s = trace ("WARN: " ++ T.unpack s)
2017-12-12 12:10:00 +00:00
#endif
2017-12-22 05:28:58 +00:00
#if defined(ERROR) || defined(WARN) || defined(DEBUG) || defined(VERBOSE)
2019-10-28 16:11:27 +00:00
log Error s = trace ("ERROR: " ++ T.unpack s)
2017-12-12 12:10:00 +00:00
#endif
log _ _ = id
2018-09-25 05:02:33 +00:00
-- | Manadic logging function residing in the 'IO' Monad
logIO
:: LogLevel -- ^ Log level to log to
2019-10-28 16:11:27 +00:00
-> T.Text -- ^ The message string
2018-09-25 05:02:33 +00:00
-> IO ()
2017-12-22 05:28:58 +00:00
#if defined(VERBOSE)
2019-10-28 16:11:27 +00:00
logIO Verbose s = traceIO ("VERBOSE: " ++ T.unpack s)
2017-12-22 05:28:58 +00:00
#endif
#if defined(DEBUG) || defined(VERBOSE)
2019-10-28 16:11:27 +00:00
logIO Debug s = traceIO ("DEBUG: " ++ T.unpack s)
2017-12-12 12:10:00 +00:00
#endif
2017-12-22 05:28:58 +00:00
#if defined(WARN) || defined(DEBUG) || defined(VERBOSE)
2019-10-28 16:11:27 +00:00
logIO Warn s = traceIO ("WARN: " ++ T.unpack s)
2017-12-12 12:10:00 +00:00
#endif
2017-12-22 05:28:58 +00:00
#if defined(ERROR) || defined(WARN) || defined(DEBUG) || defined(VERBOSE)
2019-10-28 16:11:27 +00:00
logIO Error s = traceIO ("ERROR: " ++ T.unpack s)
2017-12-12 12:10:00 +00:00
#endif
logIO _ _ = return ()