From c82325a1bd35d2ba65784d233d7c5cbca21bf050 Mon Sep 17 00:00:00 2001 From: nek0 Date: Tue, 12 Dec 2017 13:10:00 +0100 Subject: [PATCH] logging module --- affection.cabal | 22 ++++++++++++++++++++++ src/Affection/Logging.hs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/Affection/Logging.hs diff --git a/affection.cabal b/affection.cabal index 018d1ff..97fc92d 100644 --- a/affection.cabal +++ b/affection.cabal @@ -30,12 +30,34 @@ source-repository head type: git location: https://github.com/nek0/affection +flag debug + description: Enable debug messages + default: False + manual: True + +flag warn + description: Enable warning messages + default: False + manual: True + +flag error + description: Enable error messages + default: False + manual: True + flag examples description: Build example programs default: False library + if flag(debug) + cpp-options: -DDEBUG + if flag(warn) + cpp-options: -DWARN + if flag(error) + cpp-options: -DERROR exposed-modules: Affection + , Affection.Logging , Affection.Types , Affection.StateMachine , Affection.MouseInteractable diff --git a/src/Affection/Logging.hs b/src/Affection/Logging.hs new file mode 100644 index 0000000..0cf3032 --- /dev/null +++ b/src/Affection/Logging.hs @@ -0,0 +1,33 @@ +{-# LANGUAGE CPP #-} +module Affection.Logging where + +import Debug.Trace + +data LogLevel + = Debug + | Warn + | Error + +log :: LogLevel -> String -> a -> a +#if defined(DEBUG) +log Debug s = trace ("DEBUG: " ++ s) +#endif +#if defined(WARN) || defined(DEBUG) +log Warn s = trace ("WARN: " ++ s) +#endif +#if defined(ERROR) || defined(WARN) || defined(DEBUG) +log Error s = trace ("ERROR: " ++ s) +#endif +log _ _ = id + +logIO :: LogLevel -> String -> IO () +#if defined(DEBUG) +logIO Debug s = traceIO ("DEBUG: " ++ s) +#endif +#if defined(WARN) || defined(DEBUG) +logIO Warn s = traceIO ("WARN: " ++ s) +#endif +#if defined(ERROR) || defined(WARN) || defined(DEBUG) +logIO Error s = traceIO ("ERROR: " ++ s) +#endif +logIO _ _ = return ()