summaryrefslogtreecommitdiff
path: root/src/Propellor/Debug.hs
diff options
context:
space:
mode:
authorJoey Hess2015-11-01 11:30:36 -0400
committerJoey Hess2015-11-01 11:30:36 -0400
commit046d7d82b4b309ade5e3508817f1b9b684e57b94 (patch)
treeb1e6cc3f2d959c7726e3da0c67551927d6a321c8 /src/Propellor/Debug.hs
parent082bfc9f301adc59d7cd26954d8cdc0caf80ec7e (diff)
parentb218820da0b069e826507150cba118f0fa69d409 (diff)
Merge branch 'joeyconfig'
Diffstat (limited to 'src/Propellor/Debug.hs')
-rw-r--r--src/Propellor/Debug.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/Propellor/Debug.hs b/src/Propellor/Debug.hs
new file mode 100644
index 00000000..ac4a56cc
--- /dev/null
+++ b/src/Propellor/Debug.hs
@@ -0,0 +1,36 @@
+module Propellor.Debug where
+
+import Control.Applicative
+import Control.Monad.IfElse
+import System.IO
+import System.Directory
+import System.Log.Logger
+import System.Log.Formatter
+import System.Log.Handler (setFormatter)
+import System.Log.Handler.Simple
+
+import Utility.Monad
+import Utility.Env
+import Utility.Exception
+import Utility.Process
+
+debug :: [String] -> IO ()
+debug = debugM "propellor" . unwords
+
+checkDebugMode :: IO ()
+checkDebugMode = go =<< getEnv "PROPELLOR_DEBUG"
+ where
+ go (Just "1") = enableDebugMode
+ go (Just _) = noop
+ go Nothing = whenM (doesDirectoryExist ".git") $
+ whenM (elem "1" . lines <$> getgitconfig) enableDebugMode
+ getgitconfig = catchDefaultIO "" $
+ readProcess "git" ["config", "propellor.debug"]
+
+enableDebugMode :: IO ()
+enableDebugMode = do
+ f <- setFormatter
+ <$> streamHandler stderr DEBUG
+ <*> pure (simpleLogFormatter "[$time] $msg")
+ updateGlobalLogger rootLoggerName $
+ setLevel DEBUG . setHandlers [f]