Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Logging.hs @ aa3adf35

History | View | Annotate | Download (3.5 kB)

1 ba4e38e8 Iustin Pop
{-| Implementation of the Ganeti logging functionality.
2 ba4e38e8 Iustin Pop
3 ba4e38e8 Iustin Pop
This currently lacks the following (FIXME):
4 ba4e38e8 Iustin Pop
5 ba4e38e8 Iustin Pop
- syslog logging
6 ba4e38e8 Iustin Pop
- handling of the three-state syslog yes/no/only
7 ba4e38e8 Iustin Pop
- log file reopening
8 ba4e38e8 Iustin Pop
9 ba4e38e8 Iustin Pop
Note that this requires the hslogger library version 1.1 and above.
10 ba4e38e8 Iustin Pop
11 ba4e38e8 Iustin Pop
-}
12 ba4e38e8 Iustin Pop
13 ba4e38e8 Iustin Pop
{-
14 ba4e38e8 Iustin Pop
15 ba4e38e8 Iustin Pop
Copyright (C) 2011 Google Inc.
16 ba4e38e8 Iustin Pop
17 ba4e38e8 Iustin Pop
This program is free software; you can redistribute it and/or modify
18 ba4e38e8 Iustin Pop
it under the terms of the GNU General Public License as published by
19 ba4e38e8 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
20 ba4e38e8 Iustin Pop
(at your option) any later version.
21 ba4e38e8 Iustin Pop
22 ba4e38e8 Iustin Pop
This program is distributed in the hope that it will be useful, but
23 ba4e38e8 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
24 ba4e38e8 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25 ba4e38e8 Iustin Pop
General Public License for more details.
26 ba4e38e8 Iustin Pop
27 ba4e38e8 Iustin Pop
You should have received a copy of the GNU General Public License
28 ba4e38e8 Iustin Pop
along with this program; if not, write to the Free Software
29 ba4e38e8 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 ba4e38e8 Iustin Pop
02110-1301, USA.
31 ba4e38e8 Iustin Pop
32 ba4e38e8 Iustin Pop
-}
33 ba4e38e8 Iustin Pop
34 ba4e38e8 Iustin Pop
module Ganeti.Logging
35 ba4e38e8 Iustin Pop
  ( setupLogging
36 ba4e38e8 Iustin Pop
  , logDebug
37 ba4e38e8 Iustin Pop
  , logInfo
38 ba4e38e8 Iustin Pop
  , logNotice
39 ba4e38e8 Iustin Pop
  , logWarning
40 ba4e38e8 Iustin Pop
  , logError
41 ba4e38e8 Iustin Pop
  , logCritical
42 ba4e38e8 Iustin Pop
  , logAlert
43 ba4e38e8 Iustin Pop
  , logEmergency
44 ba4e38e8 Iustin Pop
  ) where
45 ba4e38e8 Iustin Pop
46 ba4e38e8 Iustin Pop
import System.Log.Logger
47 ba4e38e8 Iustin Pop
import System.Log.Handler.Simple
48 ba4e38e8 Iustin Pop
import System.Log.Handler (setFormatter)
49 ba4e38e8 Iustin Pop
import System.Log.Formatter
50 ba4e38e8 Iustin Pop
import System.IO
51 ba4e38e8 Iustin Pop
52 ba4e38e8 Iustin Pop
import qualified Ganeti.Constants as C
53 ba4e38e8 Iustin Pop
54 ba4e38e8 Iustin Pop
-- | Builds the log formatter.
55 ba4e38e8 Iustin Pop
logFormatter :: String  -- ^ Program
56 ba4e38e8 Iustin Pop
             -> Bool    -- ^ Multithreaded
57 ba4e38e8 Iustin Pop
             -> Bool    -- ^ Syslog
58 ba4e38e8 Iustin Pop
             -> LogFormatter a
59 ba4e38e8 Iustin Pop
logFormatter prog mt syslog =
60 ba4e38e8 Iustin Pop
  let parts = [ if syslog
61 ba4e38e8 Iustin Pop
                  then "[$pid]:"
62 ba4e38e8 Iustin Pop
                  else "$time: " ++ prog ++ " pid=$pid"
63 ba4e38e8 Iustin Pop
              , if mt then if syslog then " ($tid)" else "/$tid"
64 ba4e38e8 Iustin Pop
                  else ""
65 ba4e38e8 Iustin Pop
              , " $prio $msg"
66 ba4e38e8 Iustin Pop
              ]
67 ba4e38e8 Iustin Pop
  in simpleLogFormatter $ concat parts
68 ba4e38e8 Iustin Pop
69 ba4e38e8 Iustin Pop
-- | Sets up the logging configuration.
70 ba4e38e8 Iustin Pop
setupLogging :: String    -- ^ Log file
71 ba4e38e8 Iustin Pop
             -> String    -- ^ Program name
72 ba4e38e8 Iustin Pop
             -> Bool      -- ^ Debug level
73 ba4e38e8 Iustin Pop
             -> Bool      -- ^ Log to stderr
74 ba4e38e8 Iustin Pop
             -> Bool      -- ^ Log to console
75 ba4e38e8 Iustin Pop
             -> IO ()
76 ba4e38e8 Iustin Pop
setupLogging logf program debug stderr_logging console = do
77 ba4e38e8 Iustin Pop
  let level = if debug then DEBUG else INFO
78 ba4e38e8 Iustin Pop
      destf = if console then C.devConsole else logf
79 ba4e38e8 Iustin Pop
      fmt = logFormatter program False False
80 ba4e38e8 Iustin Pop
81 ba4e38e8 Iustin Pop
  updateGlobalLogger rootLoggerName (setLevel level)
82 ba4e38e8 Iustin Pop
83 ba4e38e8 Iustin Pop
  stderr_handlers <-  if stderr_logging
84 ba4e38e8 Iustin Pop
                        then do
85 ba4e38e8 Iustin Pop
                          stderr_handler <- streamHandler stderr level
86 ba4e38e8 Iustin Pop
                          return [setFormatter stderr_handler fmt]
87 ba4e38e8 Iustin Pop
                        else return []
88 ba4e38e8 Iustin Pop
  file_handler <- fileHandler destf level
89 ba4e38e8 Iustin Pop
  let handlers = setFormatter file_handler fmt:stderr_handlers
90 ba4e38e8 Iustin Pop
  updateGlobalLogger rootLoggerName $ setHandlers handlers
91 ba4e38e8 Iustin Pop
92 ba4e38e8 Iustin Pop
-- * Logging function aliases
93 ba4e38e8 Iustin Pop
94 ba4e38e8 Iustin Pop
-- | Log at debug level.
95 ba4e38e8 Iustin Pop
logDebug :: String -> IO ()
96 ba4e38e8 Iustin Pop
logDebug = debugM rootLoggerName
97 ba4e38e8 Iustin Pop
98 ba4e38e8 Iustin Pop
-- | Log at info level.
99 ba4e38e8 Iustin Pop
logInfo :: String -> IO ()
100 ba4e38e8 Iustin Pop
logInfo = infoM rootLoggerName
101 ba4e38e8 Iustin Pop
102 ba4e38e8 Iustin Pop
-- | Log at notice level.
103 ba4e38e8 Iustin Pop
logNotice :: String -> IO ()
104 ba4e38e8 Iustin Pop
logNotice = noticeM rootLoggerName
105 ba4e38e8 Iustin Pop
106 ba4e38e8 Iustin Pop
-- | Log at warning level.
107 ba4e38e8 Iustin Pop
logWarning :: String -> IO ()
108 ba4e38e8 Iustin Pop
logWarning = warningM rootLoggerName
109 ba4e38e8 Iustin Pop
110 ba4e38e8 Iustin Pop
-- | Log at error level.
111 ba4e38e8 Iustin Pop
logError :: String -> IO ()
112 ba4e38e8 Iustin Pop
logError = errorM rootLoggerName
113 ba4e38e8 Iustin Pop
114 ba4e38e8 Iustin Pop
-- | Log at critical level.
115 ba4e38e8 Iustin Pop
logCritical :: String -> IO ()
116 ba4e38e8 Iustin Pop
logCritical = criticalM rootLoggerName
117 ba4e38e8 Iustin Pop
118 ba4e38e8 Iustin Pop
-- | Log at alert level.
119 ba4e38e8 Iustin Pop
logAlert :: String -> IO ()
120 ba4e38e8 Iustin Pop
logAlert = alertM rootLoggerName
121 ba4e38e8 Iustin Pop
122 ba4e38e8 Iustin Pop
-- | Log at emergency level.
123 ba4e38e8 Iustin Pop
logEmergency :: String -> IO ()
124 ba4e38e8 Iustin Pop
logEmergency = emergencyM rootLoggerName