Commit 2bc76025 authored by Bjørnar Hansen's avatar Bjørnar Hansen Committed by Bjørnar Hansen
Browse files

Open source release.

parents
Copyright (c) 2015, Tingtun AS, http://tingtun.no
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
name: simple-logging
version: 0.1.0.0
cabal-version: >=1.10
build-type: Simple
license: BSD3
license-file: LICENSE
copyright: 2015 Tingtun AS
maintainer: contact@tingtun.no
homepage: http://tingtun.no
author: Various
library
exposed-modules:
System.Log.Logging
build-depends:
base >=4.7.0.2,
directory >=1.2.1,
filepath >=1.3.0.2,
hslogger >=1.2.8,
old-locale >=1.0.0.6,
time >=1.4.2
default-language: Haskell2010
hs-source-dirs: src
ghc-prof-options: -fprof-auto
other-extensions: CPP
{-# LANGUAGE CPP #-}
module System.Log.Logging
( withLogging
, logDebug,logInfo,logWarn,logErr
) where
import Control.Applicative ((<$>))
import Control.Concurrent (forkIO)
import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
import Control.Exception
import Control.Monad (forever)
import Data.Time (formatTime, getCurrentTime,
getZonedTime)
import Prelude hiding (log)
import System.Directory (createDirectoryIfMissing)
import System.Environment (getProgName)
import System.FilePath
import System.IO
import System.IO.Unsafe
#if MIN_VERSION_time(1,5,0)
import Data.Time (defaultTimeLocale)
#else
import System.Locale (defaultTimeLocale)
#endif
import System.Log.Formatter
import System.Log.Handler (setFormatter)
import System.Log.Handler.Simple
import System.Log.Handler.Syslog
import System.Log.Logger
-- Wrap your main function with 'withLogging', like this:
-- > main = withLogging "path/to/output.log" $ do
-- > ...
--
-- Now you can use the various log* functions in the IO Monad:
-- > logDebug "This is a debug message."
-- > logInfo "Just some info."
withLogging :: String -> IO a -> IO a
withLogging logFile a = do
let logdir = fst $ splitFileName logFile
createDirectoryIfMissing True logdir
f <- fileHandler logFile DEBUG
let fmt = adjustingFormatter "%F %X" "$time :$prio - [$loggername] $msg"
fileLogHandler = setFormatter f fmt
updateGlobalLogger rootLoggerName removeHandler
updateGlobalLogger rootLoggerName (addHandler fileLogHandler)
updateGlobalLogger rootLoggerName (setLevel DEBUG)
forkIO . forever $ do
(f, name, msg) <- readChan logChan
f name msg
a `finally` removeAllHandlers
adjustingFormatter :: String -> String -> LogFormatter a
adjustingFormatter timeFormat format h (prio,msg) =
varFormatter vars format h (prio,msg)
where vars = [("time", formatTime defaultTimeLocale timeFormat <$> getZonedTime)
,("prio", return paddedPrio)
]
paddedPrio = show prio ++
case prio of
INFO -> " "
ALERT -> " "
DEBUG -> " "
ERROR -> " "
NOTICE -> " "
WARNING -> " "
CRITICAL -> " "
EMERGENCY -> " "
-- This is to avoid performing the actual output of log messages in the calling
-- thread. That would incur an unnecessary performance penalty.
--
-- What we do is to create a mutable global variable (ugh) of the type Chan. We
-- can then post messages to it using the log* functions defined below. At its
-- convenience, the logging thread reads from the Chan and performs the
-- IO activity associated with logging.
logChan :: Chan (String -> String -> IO (), String, String)
{-# NOINLINE logChan #-}
logChan = unsafePerformIO newChan
logInfo, logDebug, logWarn, logErr :: String -> IO ()
logInfo = log infoM
logDebug = log debugM
logWarn = log warningM
logErr = log errorM
log :: (String -> String -> IO ()) -> String -> IO ()
log f str = do
appName <- getProgName
writeChan logChan (f, appName, str)
main :: IO ()
main = withLogging "test.log" $ do
logDebug "This is a debug message"
logWarn "This is a warning message"
logInfo "This is an info message"
logErr "This is an error message"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment