Merge branch 'master' of ssh://lausser,shinken@shinken.git.sourceforge.net/gitroot...
[shinken.git] / shinken / log.py
blob31a6c2c5d81bee64abafe778ee6e67a9f793607b
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 import time
25 from brok import Brok
27 obj = None
28 name = None
29 local_log = None
31 class Log:
32 # We load the object where we will put log broks
33 # with the 'add' method
34 def load_obj(self, object, name_ = None):
35 global obj
36 global name
37 obj = object
38 name = name_
40 # We enter a log message, we format it, and we add the log brok
41 def log(self, message, format = None):
42 global obj
43 global name
44 global local_log
46 # If the daemon is launch with a non UTF8 shell
47 # we can habe problem in printing
48 try:
49 print message
50 except UnicodeEncodeError:
51 print message.encode('ascii', 'ignore')
53 if format is None:
54 if name is None:
55 # We format the log in UTF-8
56 #message.decode('UTF-8', 'replace')
57 s = u'[%d] %s\n' % (int(time.time()), message)
58 else:
59 s = u'[%d] [%s] %s\n' % (int(time.time()), name, message)
60 else:
61 s = format % message
63 # We create and add the brok
64 b = Brok('log', {'log' : s})
65 obj.add(b)
67 # If we want a local log write, do it
68 if local_log is not None:
69 local_log.write(s)
70 local_log.flush()
73 # The log can also write to a local file if need
74 def register_local_log(self, path):
75 global local_log
76 local_log = open(path, 'a')
79 # Clsoe the local log file at program exit
80 def quit(self):
81 global local_log
82 if local_log:
83 local_log.close()
86 logger = Log()